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

This commit is contained in:
Fabien Potencier
2012-04-25 18:22:04 +02:00
parent 73136d3583
commit 75d97b4a27
5 changed files with 59 additions and 28 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 1.8.0 (2012-XX-XX)
* simplified usage of the autoescape tag; the only argument is now the escaping strategy or false (with a BC layer)
* simplified usage of the autoescape tag; the only (optional) argument is now the escaping strategy or false (with a BC layer)
* added a way to dynamically change the auto-escaping strategy according to the template "filename"
* changed the autoescape option to also accept a supported escaping strategy (for BC, true is equivalent to html)
* added an embed tag
+4 -2
View File
@@ -327,11 +327,13 @@ the ``raw`` filter:
{{ article.to_html|raw }}
You can also change the escaping mode locally by using the ``autoescape`` tag:
You can also change the escaping mode locally by using the ``autoescape`` tag
(see the :doc:`autoescape<../tags/autoescape>` doc for the syntax used before
Twig 1.8):
.. code-block:: jinja
{% autoescape true %}
{% autoescape 'html' %}
{{ var }}
{{ var|raw }} {# var won't be escaped #}
{{ var|escape }} {# var won't be double-escaped #}
+32 -12
View File
@@ -6,26 +6,46 @@ template to be escaped or not by using the ``autoescape`` tag:
.. code-block:: jinja
{% autoescape true %} {# as of Twig 1.8, this is equivalent to {% autoescape 'html' %} #}
{# The following syntax works as of Twig 1.8 -- see the note below for previous versions #}
{% autoescape %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}
{% autoescape 'html' %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}
{% autoescape 'js' %}
Everything will be automatically escaped in this block
using the js escaping strategy
{% endautoescape %}
{% autoescape false %}
Everything will be outputted as is in this block
{% endautoescape %}
{# deprecated as of Twig 1.8 #}
{% autoescape true js %}
Everything will be automatically escaped in this block
using the js escaping strategy
{% endautoescape %}
.. note::
{# as of Twig 1.8 #}
{% autoescape 'js' %}
Everything will be automatically escaped in this block
using the js escaping strategy
{% endautoescape %}
Before Twig 1.8, the syntax was different:
.. code-block:: jinja
{% autoescape true %}
Everything will be automatically escaped in this block
using the HTML strategy
{% endautoescape %}
{% autoescape false %}
Everything will be outputted as is in this block
{% endautoescape %}
{% autoescape true js %}
Everything will be automatically escaped in this block
using the js escaping strategy
{% endautoescape %}
When automatic escaping is enabled everything is escaped by default except for
values explicitly marked as safe. Those can be marked in the template by using
@@ -33,7 +53,7 @@ the :doc:`raw<../filters/raw>` filter:
.. code-block:: jinja
{% autoescape true %}
{% autoescape %}
{{ safe_value|raw }}
{% endautoescape %}
+18 -13
View File
@@ -39,24 +39,29 @@ class Twig_TokenParser_AutoEscape extends Twig_TokenParser
public function parse(Twig_Token $token)
{
$lineno = $token->getLine();
$expr = $this->parser->getExpressionParser()->parseExpression();
if (!$expr instanceof Twig_Node_Expression_Constant) {
throw new Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $lineno);
}
$value = $expr->getAttribute('value');
$compat = true === $value || false === $value;
if (true === $value) {
if ($this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) {
$value = 'html';
}
} else {
$expr = $this->parser->getExpressionParser()->parseExpression();
if (!$expr instanceof Twig_Node_Expression_Constant) {
throw new Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $lineno);
}
$value = $expr->getAttribute('value');
if ($compat && $this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
if (false === $value) {
throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $lineno);
$compat = true === $value || false === $value;
if (true === $value) {
$value = 'html';
}
$value = $this->parser->getStream()->next()->getValue();
if ($compat && $this->parser->getStream()->test(Twig_Token::NAME_TYPE)) {
if (false === $value) {
throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $lineno);
}
$value = $this->parser->getStream()->next()->getValue();
}
}
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
@@ -1,6 +1,9 @@
--TEST--
"autoescape" tag applies escaping on its children
--TEMPLATE--
{% autoescape %}
{{ var }}<br />
{% endautoescape %}
{% autoescape 'html' %}
{{ var }}<br />
{% endautoescape %}
@@ -17,6 +20,7 @@
return array('var' => '<br />')
--EXPECT--
&lt;br /&gt;<br />
&lt;br /&gt;<br />
<br /><br />
&lt;br /&gt;<br />
<br /><br />