Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benspaulding/82236 to your computer and use it in GitHub Desktop.
Save benspaulding/82236 to your computer and use it in GitHub Desktop.
From c693dfaa46218537fb66e6e30638ee81f5374fac Mon Sep 17 00:00:00 2001
From: Ben Spaulding <ben@benspaulding.com>
Date: Fri, 20 Mar 2009 00:22:21 -0500
Subject: [PATCH] Created ``yes`` filter in default filters.
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
I cannot count the times that I wanted to output a string if
the value was true, but output nothing otherwise. This leads
to countless bits like so::
{% block body_class %}{{ block.super }}{% if val %} my_class{% endif %}{% endblock %}
Using this filter that redundant sort of line becomes::
{% block body_class %}{{ block.super }}{{ val|yes:" my_class" }}{% endblock %}
It saves some typing (though not a lot) and makes it a little
more readable, IMHO.
This is just a idea. I am not sure how many people would use
it — maybe a lot, maybe very few.
---
django/template/defaultfilters.py | 21 +++++++++++++++++++++
docs/ref/templates/builtins.txt | 16 ++++++++++++++++
tests/regressiontests/defaultfilters/tests.py | 15 +++++++++++++++
3 files changed, 52 insertions(+), 0 deletions(-)
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
index ff8d418..f36717c 100644
--- a/django/template/defaultfilters.py
+++ b/django/template/defaultfilters.py
@@ -732,6 +732,26 @@ def divisibleby(value, arg):
return int(value) % int(arg) == 0
divisibleby.is_safe = False
+def yes(value, arg=None):
+ """
+ Given a string, returns that string if the value is true. Otherwise,
+ returns an empty string.
+
+ ========== ============= ===============
+ Value Argument Outputs
+ ========== ============= ===============
+ ``True`` ``"yeah"`` ``yeah``
+ ``False`` ``"yeah"`` (empty string)
+ ``None`` ``"yeah"`` (empty string)
+ ========== ============= ===============
+ """
+ if arg is None:
+ arg = ugettext('yes')
+ if value:
+ return arg
+ return u''
+yes.is_safe = False
+
def yesno(value, arg=None):
"""
Given a string mapping values for true, false and (optionally) None,
@@ -902,4 +922,5 @@ register.filter(urlize)
register.filter(urlizetrunc)
register.filter(wordcount)
register.filter(wordwrap)
+register.filter(yes)
register.filter(yesno)
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index 17e6e8b..64f341e 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -1609,6 +1609,22 @@ If ``value`` is ``Joel is a slug``, the output would be::
is a
slug
+.. templatefilter:: yes
+
+yes
+~~~
+
+Given a string, returns that string if the value is true. Otherwise,
+returns an empty string.
+
+========== ============= ===============
+Value Argument Outputs
+========== ============= ===============
+``True`` ``"yeah"`` ``yeah``
+``False`` ``"yeah"`` (empty string)
+``None`` ``"yeah"`` (empty string)
+========== ============= ===============
+
.. templatefilter:: yesno
yesno
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py
index a97596f..b49d80e 100644
--- a/tests/regressiontests/defaultfilters/tests.py
+++ b/tests/regressiontests/defaultfilters/tests.py
@@ -419,6 +419,21 @@ True
>>> divisibleby(4, 3)
False
+>>> yes(True)
+u'yes'
+
+>>> yes(False)
+u''
+
+>>> yes(True, u'certainly')
+u'certainly'
+
+>>> yes(False, u'certainly')
+u''
+
+>>> yes(None)
+u''
+
>>> yesno(True)
u'yes'
--
1.6.0.2+GitX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment