summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-10-20 15:01:31 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-10-20 15:01:31 +0000
commitb678601df302307f0e1fed9b5354765a3b238481 (patch)
tree517b03f9cd388becbdc922cdfd515eb2f679e802
parent8a3cf46e6021e9e9b5640c3f79625564527aa165 (diff)
Fixed #4123 -- Changed the firstof template tag to correctly handle a literal
string as its last argument. Thanks, Wesley Fok and Matt Boersma. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6571 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/template/defaulttags.py8
-rw-r--r--docs/templates.txt5
-rw-r--r--tests/regressiontests/templates/tests.py5
3 files changed, 16 insertions, 2 deletions
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index 4faa334af3..2ba23a0465 100644
--- a/django/template/defaulttags.py
+++ b/django/template/defaulttags.py
@@ -517,8 +517,14 @@ def firstof(parser, token):
{% endif %}{% endif %}{% endif %}
but obviously much cleaner!
+
+ You can also use a literal string as a fallback value in case all
+ passed variables are False::
+
+ {% firstof var1 var2 var3 "fallback value" %}
+
"""
- bits = token.contents.split()[1:]
+ bits = token.split_contents()[1:]
if len(bits) < 1:
raise TemplateSyntaxError, "'firstof' statement requires at least one argument"
return FirstOfNode(bits)
diff --git a/docs/templates.txt b/docs/templates.txt
index eab4b129ce..5d5f657747 100644
--- a/docs/templates.txt
+++ b/docs/templates.txt
@@ -476,6 +476,11 @@ This is equivalent to::
{{ var3 }}
{% endif %}{% endif %}{% endif %}
+You can also use a literal string as a fallback value in case all
+passed variables are False::
+
+ {% firstof var1 var2 var3 "fallback value" %}
+
for
~~~
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 0c851d5f78..a011248210 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -341,7 +341,10 @@ class Templates(unittest.TestCase):
'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'),
'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'),
'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'),
- 'firstof06': ('{% firstof %}', {}, template.TemplateSyntaxError),
+ 'firstof06': ('{% firstof a b c %}', {'b':0,'c':3}, '3'),
+ 'firstof07': ('{% firstof a b "c" %}', {'a':0}, 'c'),
+ 'firstof08': ('{% firstof a b "c and d" %}', {'a':0,'b':0}, 'c and d'),
+ 'firstof09': ('{% firstof %}', {}, template.TemplateSyntaxError),
### FOR TAG ###############################################################
'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"),