Code

Ticket #4123: 4123.diff

File 4123.diff, 2.2 KB (added by mboersma, 7 years ago)

Patch with docs and tests for handling strings with spaces in the firstof tag.

Line 
1Index: django/template/defaulttags.py
2===================================================================
3--- django/template/defaulttags.py      (revision 6399)
4+++ django/template/defaulttags.py      (working copy)
5@@ -517,8 +517,14 @@
6         {% endif %}{% endif %}{% endif %}
7 
8     but obviously much cleaner!
9+
10+    You can also use a literal string as a fallback value in case all
11+    passed variables are False::
12+
13+        {% firstof var1 var2 var3 "fallback value" %}
14+
15     """
16-    bits = token.contents.split()[1:]
17+    bits = token.split_contents()[1:]
18     if len(bits) < 1:
19         raise TemplateSyntaxError, "'firstof' statement requires at least one argument"
20     return FirstOfNode(bits)
21Index: tests/regressiontests/templates/tests.py
22===================================================================
23--- tests/regressiontests/templates/tests.py    (revision 6399)
24+++ tests/regressiontests/templates/tests.py    (working copy)
25@@ -341,7 +341,10 @@
26             'firstof03': ('{% firstof a b c %}', {'a':0,'b':2,'c':0}, '2'),
27             'firstof04': ('{% firstof a b c %}', {'a':0,'b':0,'c':3}, '3'),
28             'firstof05': ('{% firstof a b c %}', {'a':1,'b':2,'c':3}, '1'),
29-            'firstof06': ('{% firstof %}', {}, template.TemplateSyntaxError),
30+            'firstof06': ('{% firstof a b c %}', {'b':0,'c':3}, '3'),
31+            'firstof07': ('{% firstof a b "c" %}', {'a':0}, 'c'),
32+            'firstof08': ('{% firstof a b "c and d" %}', {'a':0,'b':0}, 'c and d'),
33+            'firstof09': ('{% firstof %}', {}, template.TemplateSyntaxError),
34 
35             ### FOR TAG ###############################################################
36             'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"),
37Index: docs/templates.txt
38===================================================================
39--- docs/templates.txt  (revision 6399)
40+++ docs/templates.txt  (working copy)
41@@ -453,7 +453,12 @@
42     {% else %}{% if var3 %}
43         {{ var3 }}
44     {% endif %}{% endif %}{% endif %}
45+   
46+You can also use a literal string as a fallback value in case all
47+passed variables are False::
48 
49+    {% firstof var1 var2 var3 "fallback value" %}
50+
51 for
52 ~~~
53