Browse code

made the autoescape tag argument optional (defaults to 'html')

Fabien Potencier authored on 25/04/2012 16:22:04
Showing 5 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 * 1.8.0 (2012-XX-XX)
2 2
 
3
- * simplified usage of the autoescape tag; the only argument is now the escaping strategy or false (with a BC layer)
3
+ * simplified usage of the autoescape tag; the only (optional) argument is now the escaping strategy or false (with a BC layer)
4 4
  * added a way to dynamically change the auto-escaping strategy according to the template "filename"
5 5
  * changed the autoescape option to also accept a supported escaping strategy (for BC, true is equivalent to html)
6 6
  * added an embed tag
... ...
@@ -327,11 +327,13 @@ the ``raw`` filter:
327 327
 
328 328
     {{ article.to_html|raw }}
329 329
 
330
-You can also change the escaping mode locally by using the ``autoescape`` tag:
330
+You can also change the escaping mode locally by using the ``autoescape`` tag
331
+(see the :doc:`autoescape<../tags/autoescape>` doc for the syntax used before
332
+Twig 1.8):
331 333
 
332 334
 .. code-block:: jinja
333 335
 
334
-    {% autoescape true %}
336
+    {% autoescape 'html' %}
335 337
       {{ var }}
336 338
       {{ var|raw }}      {# var won't be escaped #}
337 339
       {{ var|escape }}   {# var won't be double-escaped #}
... ...
@@ -6,34 +6,54 @@ template to be escaped or not by using the ``autoescape`` tag:
6 6
 
7 7
 .. code-block:: jinja
8 8
 
9
-    {% autoescape true %} {# as of Twig 1.8, this is equivalent to {% autoescape 'html' %} #}
9
+    {# The following syntax works as of Twig 1.8 -- see the note below for previous versions #}
10
+
11
+    {% autoescape %}
10 12
         Everything will be automatically escaped in this block
11 13
         using the HTML strategy
12 14
     {% endautoescape %}
13 15
 
14
-    {% autoescape false %}
15
-        Everything will be outputted as is in this block
16
-    {% endautoescape %}
17
-
18
-    {# deprecated as of Twig 1.8 #}
19
-    {% autoescape true js %}
16
+    {% autoescape 'html' %}
20 17
         Everything will be automatically escaped in this block
21
-        using the js escaping strategy
18
+        using the HTML strategy
22 19
     {% endautoescape %}
23 20
 
24
-    {# as of Twig 1.8 #}
25 21
     {% autoescape 'js' %}
26 22
         Everything will be automatically escaped in this block
27 23
         using the js escaping strategy
28 24
     {% endautoescape %}
29 25
 
26
+    {% autoescape false %}
27
+        Everything will be outputted as is in this block
28
+    {% endautoescape %}
29
+
30
+.. note::
31
+
32
+    Before Twig 1.8, the syntax was different:
33
+
34
+    .. code-block:: jinja
35
+
36
+        {% autoescape true %}
37
+            Everything will be automatically escaped in this block
38
+            using the HTML strategy
39
+        {% endautoescape %}
40
+
41
+        {% autoescape false %}
42
+            Everything will be outputted as is in this block
43
+        {% endautoescape %}
44
+
45
+        {% autoescape true js %}
46
+            Everything will be automatically escaped in this block
47
+            using the js escaping strategy
48
+        {% endautoescape %}
49
+
30 50
 When automatic escaping is enabled everything is escaped by default except for
31 51
 values explicitly marked as safe. Those can be marked in the template by using
32 52
 the :doc:`raw<../filters/raw>` filter:
33 53
 
34 54
 .. code-block:: jinja
35 55
 
36
-    {% autoescape true %}
56
+    {% autoescape %}
37 57
         {{ safe_value|raw }}
38 58
     {% endautoescape %}
39 59
 
... ...
@@ -39,24 +39,29 @@ class Twig_TokenParser_AutoEscape extends Twig_TokenParser
39 39
     public function parse(Twig_Token $token)
40 40
     {
41 41
         $lineno = $token->getLine();
42
-        $expr = $this->parser->getExpressionParser()->parseExpression();
43
-        if (!$expr instanceof Twig_Node_Expression_Constant) {
44
-            throw new Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $lineno);
45
-        }
46
-        $value = $expr->getAttribute('value');
47
-
48
-        $compat = true === $value || false === $value;
49 42
 
50
-        if (true === $value) {
43
+        if ($this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) {
51 44
             $value = 'html';
52
-        }
45
+        } else {
46
+            $expr = $this->parser->getExpressionParser()->parseExpression();
47
+            if (!$expr instanceof Twig_Node_Expression_Constant) {
48
+                throw new Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $lineno);
49
+            }
50
+            $value = $expr->getAttribute('value');
53 51
 
54
-        if ($compat && $this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
55
-            if (false === $value) {
56
-                throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $lineno);
52
+            $compat = true === $value || false === $value;
53
+
54
+            if (true === $value) {
55
+                $value = 'html';
57 56
             }
58 57
 
59
-            $value = $this->parser->getStream()->next()->getValue();
58
+            if ($compat && $this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
59
+                if (false === $value) {
60
+                    throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $lineno);
61
+                }
62
+
63
+                $value = $this->parser->getStream()->next()->getValue();
64
+            }
60 65
         }
61 66
 
62 67
         $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
... ...
@@ -1,6 +1,9 @@
1 1
 --TEST--
2 2
 "autoescape" tag applies escaping on its children
3 3
 --TEMPLATE--
4
+{% autoescape %}
5
+{{ var }}<br />
6
+{% endautoescape %}
4 7
 {% autoescape 'html' %}
5 8
 {{ var }}<br />
6 9
 {% endautoescape %}
... ...
@@ -17,6 +20,7 @@
17 20
 return array('var' => '<br />')
18 21
 --EXPECT--
19 22
 &lt;br /&gt;<br />
23
+&lt;br /&gt;<br />
20 24
 <br /><br />
21 25
 &lt;br /&gt;<br />
22 26
 <br /><br />