From 56caee09379e5b795ba0405b721aa997f8afa876 Mon Sep 17 00:00:00 2001 From: henrikbjorn Date: Fri, 1 Oct 2010 13:48:52 +0200 Subject: [PATCH] renamed |safe to |raw to avoid unnecesarry confusion about what the tags does. Discussion can be viewed here http://groups.google.com/group/symfony-devs/browse_thread/thread/b927063310c74411. All tests pass. --- CHANGELOG | 1 + doc/02-Twig-for-Template-Designers.markdown | 8 ++++---- doc/03-Twig-for-Developers.markdown | 16 ++++++++-------- lib/Twig/Extension/Escaper.php | 4 ++-- test/Twig/Tests/Fixtures/tags/autoescape/safe.test | 10 ---------- .../tags/autoescape/with_filters_arguments.test | 2 +- test/Twig/Tests/Fixtures/tags/raw.test | 10 ++++++++++ 7 files changed, 26 insertions(+), 25 deletions(-) delete mode 100644 test/Twig/Tests/Fixtures/tags/autoescape/safe.test create mode 100644 test/Twig/Tests/Fixtures/tags/raw.test diff --git a/CHANGELOG b/CHANGELOG index 566662b..47143dc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,7 @@ Backward incompatibilities: * 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) diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index d46c1d6..35a1894 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -434,7 +434,7 @@ template to be escaped or not by using the `autoescape` tag: 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. @@ -1198,14 +1198,14 @@ might contain such characters in HTML. >**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) diff --git a/doc/03-Twig-for-Developers.markdown b/doc/03-Twig-for-Developers.markdown index 665ada6..e973f6e 100644 --- a/doc/03-Twig-for-Developers.markdown +++ b/doc/03-Twig-for-Developers.markdown @@ -299,7 +299,7 @@ registered by default. ### 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: @@ -309,17 +309,17 @@ 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 %} @@ -345,21 +345,21 @@ Twig 0.9.5 and above): [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 diff --git a/lib/Twig/Extension/Escaper.php b/lib/Twig/Extension/Escaper.php index cf43ad6..5476800 100644 --- a/lib/Twig/Extension/Escaper.php +++ b/lib/Twig/Extension/Escaper.php @@ -45,7 +45,7 @@ class Twig_Extension_Escaper extends Twig_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)), ); } @@ -66,7 +66,7 @@ class Twig_Extension_Escaper extends Twig_Extension } // tells the escaper node visitor that the string is safe -function twig_safe_filter($string) +function twig_raw_filter($string) { return $string; } diff --git a/test/Twig/Tests/Fixtures/tags/autoescape/safe.test b/test/Twig/Tests/Fixtures/tags/autoescape/safe.test deleted file mode 100644 index 9c45c0e..0000000 --- a/test/Twig/Tests/Fixtures/tags/autoescape/safe.test +++ /dev/null @@ -1,10 +0,0 @@ ---TEST-- -"autoescape" tag does not escape when safe is used as a filter ---TEMPLATE-- -{% autoescape on %} -{{ var|safe }} -{% endautoescape %} ---DATA-- -return array('var' => '
') ---EXPECT-- -
diff --git a/test/Twig/Tests/Fixtures/tags/autoescape/with_filters_arguments.test b/test/Twig/Tests/Fixtures/tags/autoescape/with_filters_arguments.test index 54cbe99..c7a5aba 100644 --- a/test/Twig/Tests/Fixtures/tags/autoescape/with_filters_arguments.test +++ b/test/Twig/Tests/Fixtures/tags/autoescape/with_filters_arguments.test @@ -5,7 +5,7 @@ {{ var|nl2br("
") }} {{ var|nl2br("
"|escape) }} {{ var|nl2br(sep) }} -{{ var|nl2br(sep|safe) }} +{{ var|nl2br(sep|raw) }} {% endautoescape %} --DATA-- return array('var' => "\nTwig", 'sep' => '
') diff --git a/test/Twig/Tests/Fixtures/tags/raw.test b/test/Twig/Tests/Fixtures/tags/raw.test new file mode 100644 index 0000000..ea19b5e --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/raw.test @@ -0,0 +1,10 @@ +--TEST-- +"autoescape" tag does not escape when raw is used as a filter +--TEMPLATE-- +{% autoescape on %} +{{ var|raw }} +{% endautoescape %} +--DATA-- +return array('var' => '
') +--EXPECT-- +
-- 1.7.2.5