* the self special variable has been renamed to _self
* the odd and even filters are now tests:
{{ foo|odd }} must now be written {{ foo is odd }}
+ * the "safe" filter has been renamed to "raw"
* the implementation of template inheritance has been rewritten
(blocks can now be called individually and still work with inheritance)
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
-the `|safe` filter.
+the `|raw` filter.
Functions returning template data (like macros and `parent`) always return
safe markup.
>**NOTE**
>Internally, `escape` uses the PHP `htmlspecialchars` function.
-### `safe`
+### `raw`
-The `safe` filter marks the value as safe which means that in an environment
+The `raw` filter marks the value as safe which means that in an environment
with automatic escaping enabled this variable will not be escaped.
[twig]
{% autoescape on }
- {{ var|safe }} {# var won't be escaped #}
+ {{ var|raw }} {# var won't be escaped #}
{% autoescape off %}
### `constant` (new in Twig 0.9.9)
### Escaper Extension
The `escaper` extension adds automatic output escaping to Twig. It defines a
-new tag, `autoescape`, and a new filter, `safe`.
+new tag, `autoescape`, and a new filter, `raw`.
When creating the escaper extension, you can switch on or off the global
output escaping strategy:
$twig->addExtension($escaper);
If set to `true`, all variables in templates are escaped, except those using
-the `safe` filter:
+the `raw` filter:
[twig]
- {{ article.to_html|safe }}
+ {{ article.to_html|raw }}
You can also change the escaping mode locally by using the `autoescape` tag:
[twig]
{% autoescape on %}
{% var %}
- {% var|safe %} {# var won't be escaped #}
+ {% var|raw %} {# var won't be escaped #}
{% var|escape %} {# var won't be doubled-escaped #}
{% endautoescape %}
[twig]
{{ var|nl2br }} {# is equivalent to {{ var|escape|nl2br }} #}
- * The `safe` filter can be used anywhere in the filter chain:
+ * The `raw` filter can be used anywhere in the filter chain:
[twig]
- {{ var|upper|nl2br|safe }} {# is equivalent to {{ var|safe|upper|nl2br }} #}
+ {{ var|upper|nl2br|raw }} {# is equivalent to {{ var|raw|upper|nl2br }} #}
* Automatic escaping is applied to filter arguments, except for literals:
[twig]
{{ var|foo("bar") }} {# "bar" won't be escaped #}
{{ var|foo(bar) }} {# bar will be escaped #}
- {{ var|foo(bar|safe) }} {# bar won't be escaped #}
+ {{ var|foo(bar|raw) }} {# bar won't be escaped #}
* Automatic escaping is not applied if one of the filters in the chain has the
`is_escaper` option set to `true` (this is the case for the built-in
- `escaper`, `safe`, and `urlencode` filters for instance).
+ `escaper`, `raw`, and `urlencode` filters for instance).
### Sandbox Extension
public function getFilters()
{
return array(
- 'safe' => new Twig_Filter_Function('twig_safe_filter', array('is_escaper' => true)),
+ 'raw' => new Twig_Filter_Function('twig_raw_filter', array('is_escaper' => true)),
);
}
}
// tells the escaper node visitor that the string is safe
-function twig_safe_filter($string)
+function twig_raw_filter($string)
{
return $string;
}