Created
March 20, 2009 05:24
-
-
Save benspaulding/82234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 99ca027e7f09c5a7dfd0e6804cc90325c223ffea Mon Sep 17 00:00:00 2001 | |
From: Ben Spaulding <ben@benspaulding.com> | |
Date: Fri, 20 Mar 2009 00:17:14 -0500 | |
Subject: [PATCH] Created ``or_nothing`` filter in default filters. | |
The same thing can be accomplished with the ``default`` | |
filter and an empty argument, but that feels hacky and | |
it looks dirty, too. | |
--- | |
django/template/defaultfilters.py | 11 +++++++++++ | |
docs/ref/templates/builtins.txt | 15 +++++++++++++++ | |
tests/regressiontests/defaultfilters/tests.py | 9 +++++++++ | |
3 files changed, 35 insertions(+), 0 deletions(-) | |
diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py | |
index ff8d418..59bd0c1 100644 | |
--- a/django/template/defaultfilters.py | |
+++ b/django/template/defaultfilters.py | |
@@ -732,6 +732,16 @@ def divisibleby(value, arg): | |
return int(value) % int(arg) == 0 | |
divisibleby.is_safe = False | |
+def or_nothing(value): | |
+ """ | |
+ Given a value of false or None, return an empty string. | |
+ | |
+ False values include ``False`` and ``0``, as well as ``[]`` | |
+ (empty list), ``()`` (empty tuple) and ``{}`` (empty dictionary). | |
+ """ | |
+ return value or u'' | |
+or_nothing.is_safe = False | |
+ | |
def yesno(value, arg=None): | |
""" | |
Given a string mapping values for true, false and (optionally) None, | |
@@ -877,6 +887,7 @@ register.filter(linenumbers) | |
register.filter(ljust) | |
register.filter(lower) | |
register.filter(make_list) | |
+register.filter(or_nothing) | |
register.filter(phone2numeric) | |
register.filter(pluralize) | |
register.filter(pprint) | |
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt | |
index 17e6e8b..02cf35f 100644 | |
--- a/docs/ref/templates/builtins.txt | |
+++ b/docs/ref/templates/builtins.txt | |
@@ -1240,6 +1240,21 @@ If ``value`` is the string ``"Joel"``, the output would be the list | |
``[u'J', u'o', u'e', u'l']``. If ``value`` is ``123``, the output will be the | |
list ``[1, 2, 3]``. | |
+.. templatefilter:: or_nothing | |
+ | |
+or_nothing | |
+~~~~~~~~~~ | |
+ | |
+If value evaluates to false or None, return an empty string. | |
+ | |
+For example:: | |
+ | |
+ {{ value|or_nothing }} | |
+ | |
+If ``value`` is ``False``, ``0``, ``""`` (empty string), ``[]`` (empty list), | |
+``()`` (empty tuple) or ``{}`` (empty dictionary) the output will be an | |
+empty string. | |
+ | |
.. templatefilter:: phone2numeric | |
phone2numeric | |
diff --git a/tests/regressiontests/defaultfilters/tests.py b/tests/regressiontests/defaultfilters/tests.py | |
index a97596f..a43c2ad 100644 | |
--- a/tests/regressiontests/defaultfilters/tests.py | |
+++ b/tests/regressiontests/defaultfilters/tests.py | |
@@ -419,6 +419,15 @@ True | |
>>> divisibleby(4, 3) | |
False | |
+>>> or_nothing(u"val") | |
+u'val' | |
+ | |
+>>> or_nothing(False) | |
+u'' | |
+ | |
+>>> or_nothing(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