Django

Code

Ticket #11413: doc-firstof-cycle-escaping.diff.txt

File doc-firstof-cycle-escaping.diff.txt, 2.9 kB (added by krystal, 9 months ago)
Line 
1 Index: django/template/defaulttags.py
2 ===================================================================
3 --- django/template/defaulttags.py      (revision 11154)
4 +++ django/template/defaulttags.py      (working copy)
5 @@ -564,7 +564,7 @@
6  #@register.tag
7  def firstof(parser, token):
8      """
9 -    Outputs the first variable passed that is not False.
10 +    Outputs the first variable passed that is not False, without escaping.
11  
12      Outputs nothing if all the passed variables are False.
13  
14 @@ -575,11 +575,11 @@
15      This is equivalent to::
16  
17          {% if var1 %}
18 -            {{ var1 }}
19 +            {{ var1|safe }}
20          {% else %}{% if var2 %}
21 -            {{ var2 }}
22 +            {{ var2|safe }}
23          {% else %}{% if var3 %}
24 -            {{ var3 }}
25 +            {{ var3|safe }}
26          {% endif %}{% endif %}{% endif %}
27  
28      but obviously much cleaner!
29 @@ -589,6 +589,12 @@
30  
31          {% firstof var1 var2 var3 "fallback value" %}
32  
33 +    If you want to escape the output, use a filter tag::
34 +
35 +        {% filter force_escape %}
36 +            {% firstof var1 var2 var3 "fallback value" %}
37 +       {% endfilter %}
38 +
39      """
40      bits = token.split_contents()[1:]
41      if len(bits) < 1:
42 Index: docs/ref/templates/builtins.txt
43 ===================================================================
44 --- docs/ref/templates/builtins.txt     (revision 11154)
45 +++ docs/ref/templates/builtins.txt     (working copy)
46 @@ -101,6 +101,13 @@
47  Values enclosed in single (``'``) or double quotes (``"``) are treated as
48  string literals, while values without quotes are treated as template variables.
49  
50 +Note that as cycle is a tag, variables will never be escaped unless you
51 +do it explicitly with a filter tag ::
52 +
53 +    {% filter force_escape %}
54 +        {% cycle var1 var2 var3 %}
55 +    {% endfilter %}
56 +
57  For backwards compatibility, the ``{% cycle %}`` tag supports the much inferior
58  old syntax from previous Django versions. You shouldn't use this in any new
59  projects, but for the sake of the people who are still using it, here's what it
60 @@ -160,9 +167,10 @@
61  firstof
62  ~~~~~~~
63  
64 -Outputs the first variable passed that is not False.  Outputs nothing if all the
65 -passed variables are False.
66 +Outputs the first variable passed that is not False, without escaping. 
67  
68 +Outputs nothing if all the passed variables are False.
69 +
70  Sample usage::
71  
72      {% firstof var1 var2 var3 %}
73 @@ -170,11 +178,11 @@
74  This is equivalent to::
75  
76      {% if var1 %}
77 -        {{ var1 }}
78 +        {{ var1|safe }}
79      {% else %}{% if var2 %}
80 -        {{ var2 }}
81 +        {{ var2|safe }}
82      {% else %}{% if var3 %}
83 -        {{ var3 }}
84 +        {{ var3|safe }}
85      {% endif %}{% endif %}{% endif %}
86  
87  You can also use a literal string as a fallback value in case all
88 @@ -182,6 +190,12 @@
89  
90      {% firstof var1 var2 var3 "fallback value" %}
91  
92 +If you want to escape the output, use a filter tag::
93 +
94 +    {% filter force_escape %}
95 +        {% firstof var1 var2 var3 "fallback value" %}
96 +    {% endfilter %}
97 +
98  .. templatetag:: for
99  
100  for