Skip to content
Snippets Groups Projects
Commit af1ca73a authored by Angie Byron's avatar Angie Byron
Browse files

Issue #2172235 by joelpittet: Upgrade Twig to 1.15.* from 1.12.*.

parent 56d3a638
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Showing
with 246 additions and 95 deletions
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"This file locks the dependencies of your project to a known state", "This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
], ],
"hash": "06006c1512fb829fcdb498b7e515901d", "hash": "14b55ea7402f04abe36b677892e442f3",
"packages": [ "packages": [
{ {
"name": "doctrine/annotations", "name": "doctrine/annotations",
...@@ -1877,16 +1877,16 @@ ...@@ -1877,16 +1877,16 @@
}, },
{ {
"name": "twig/twig", "name": "twig/twig",
"version": "v1.12.3", "version": "v1.15.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/fabpot/Twig.git", "url": "https://github.com/fabpot/Twig.git",
"reference": "v1.12.3" "reference": "85e4ff98000157ff753d934b9f13659a953f5666"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/fabpot/Twig/zipball/v1.12.3", "url": "https://api.github.com/repos/fabpot/Twig/zipball/85e4ff98000157ff753d934b9f13659a953f5666",
"reference": "v1.12.3", "reference": "85e4ff98000157ff753d934b9f13659a953f5666",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -1895,7 +1895,7 @@ ...@@ -1895,7 +1895,7 @@
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.12-dev" "dev-master": "1.15-dev"
} }
}, },
"autoload": { "autoload": {
...@@ -1905,7 +1905,7 @@ ...@@ -1905,7 +1905,7 @@
}, },
"notification-url": "https://packagist.org/downloads/", "notification-url": "https://packagist.org/downloads/",
"license": [ "license": [
"BSD-3" "BSD-3-Clause"
], ],
"authors": [ "authors": [
{ {
...@@ -1922,7 +1922,7 @@ ...@@ -1922,7 +1922,7 @@
"keywords": [ "keywords": [
"templating" "templating"
], ],
"time": "2013-04-08 12:40:11" "time": "2013-12-06 07:47:10"
}, },
{ {
"name": "zendframework/zend-escaper", "name": "zendframework/zend-escaper",
......
...@@ -4,6 +4,7 @@ php: ...@@ -4,6 +4,7 @@ php:
- 5.2 - 5.2
- 5.3 - 5.3
- 5.4 - 5.4
- 5.5
env: env:
- TWIG_EXT=no - TWIG_EXT=no
......
...@@ -4,6 +4,10 @@ Lead Developer: ...@@ -4,6 +4,10 @@ Lead Developer:
- Fabien Potencier <fabien.potencier@symfony-project.org> - Fabien Potencier <fabien.potencier@symfony-project.org>
C Extension Developer:
- Derick Rethans <derick@derickrethans.nl>
Project Founder: Project Founder:
- Armin Ronacher <armin.ronacher@active-4.com> - Armin Ronacher <armin.ronacher@active-4.com>
* 1.15.0 (2013-12-06)
* made ignoreStrictCheck in Template::getAttribute() works with __call() methods throwing BadMethodCallException
* added min and max functions
* added the round filter
* fixed a bug that prevented the optimizers to be enabled/disabled selectively
* fixed first and last filters for UTF-8 strings
* added a source function to include the content of a template without rendering it
* fixed the C extension sandbox behavior when get or set is prepend to method name
* 1.14.2 (2013-10-30)
* fixed error filename/line when an error occurs in an included file
* allowed operators that contain whitespaces to have more than one whitespace
* allowed tests to be made of 1 or 2 words (like "same as" or "divisible by")
* 1.14.1 (2013-10-15)
* made it possible to use named operators as variables
* fixed the possibility to have a variable named 'matches'
* added support for PHP 5.5 DateTimeInterface
* 1.14.0 (2013-10-03)
* fixed usage of the html_attr escaping strategy to avoid double-escaping with the html strategy
* added new operators: ends with, starts with, and matches
* fixed some compatibility issues with HHVM
* added a way to add custom escaping strategies
* fixed the C extension compilation on Windows
* fixed the batch filter when using a fill argument with an exact match of elements to batch
* fixed the filesystem loader cache when a template name exists in several namespaces
* fixed template_from_string when the template includes or extends other ones
* fixed a crash of the C extension on an edge case
* 1.13.2 (2013-08-03)
* fixed the error line number for an error occurs in and embedded template
* fixed crashes of the C extension on some edge cases
* 1.13.1 (2013-06-06)
* added the possibility to ignore the filesystem constructor argument in Twig_Loader_Filesystem
* fixed Twig_Loader_Chain::exists() for a loader which implements Twig_ExistsLoaderInterface
* adjusted backtrace call to reduce memory usage when an error occurs
* added support for object instances as the second argument of the constant test
* fixed the include function when used in an assignment
* 1.13.0 (2013-05-10)
* fixed getting a numeric-like item on a variable ('09' for instance)
* fixed getting a boolean or float key on an array, so it is consistent with PHP's array access:
`{{ array[false] }}` behaves the same as `echo $array[false];` (equals `$array[0]`)
* made the escape filter 20% faster for happy path (escaping string for html with UTF-8)
* changed ☃ to § in tests
* enforced usage of named arguments after positional ones
* 1.12.3 (2013-04-08) * 1.12.3 (2013-04-08)
* fixed a security issue in the filesystem loader where it was possible to include a template one * fixed a security issue in the filesystem loader where it was possible to include a template one
......
Twig, the flexible, fast, and secure template language for PHP Twig, the flexible, fast, and secure template language for PHP
============================================================== ==============================================================
[![Build Status](https://secure.travis-ci.org/fabpot/Twig.png?branch=master)](http://travis-ci.org/fabpot/Twig)
Twig is a template language for PHP, released under the new BSD license (code Twig is a template language for PHP, released under the new BSD license (code
and documentation). and documentation).
...@@ -12,6 +10,6 @@ inspired the Twig runtime environment. ...@@ -12,6 +10,6 @@ inspired the Twig runtime environment.
More Information More Information
---------------- ----------------
Read the [documentation][1] for more information. Read the `documentation`_ for more information.
[1]: http://twig.sensiolabs.org/documentation .. _documentation: http://twig.sensiolabs.org/documentation
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"description": "Twig, the flexible, fast, and secure template language for PHP", "description": "Twig, the flexible, fast, and secure template language for PHP",
"keywords": ["templating"], "keywords": ["templating"],
"homepage": "http://twig.sensiolabs.org", "homepage": "http://twig.sensiolabs.org",
"license": "BSD-3", "license": "BSD-3-Clause",
"authors": [ "authors": [
{ {
"name": "Fabien Potencier", "name": "Fabien Potencier",
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.12-dev" "dev-master": "1.15-dev"
} }
} }
} }
...@@ -177,7 +177,7 @@ argument:: ...@@ -177,7 +177,7 @@ argument::
$filter = new Twig_SimpleFilter('rot13', 'str_rot13', $options); $filter = new Twig_SimpleFilter('rot13', 'str_rot13', $options);
Environment aware Filters Environment-aware Filters
~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to access the current environment instance in your filter, set the If you want to access the current environment instance in your filter, set the
...@@ -191,7 +191,7 @@ environment as the first argument to the filter call:: ...@@ -191,7 +191,7 @@ environment as the first argument to the filter call::
return str_rot13($string); return str_rot13($string);
}, array('needs_environment' => true)); }, array('needs_environment' => true));
Context aware Filters Context-aware Filters
~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
If you want to access the current context in your filter, set the If you want to access the current context in your filter, set the
...@@ -211,14 +211,14 @@ Automatic Escaping ...@@ -211,14 +211,14 @@ Automatic Escaping
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
If automatic escaping is enabled, the output of the filter may be escaped If automatic escaping is enabled, the output of the filter may be escaped
before printing. If your filter acts as an escaper (or explicitly outputs html before printing. If your filter acts as an escaper (or explicitly outputs HTML
or JavaScript code), you will want the raw output to be printed. In such a or JavaScript code), you will want the raw output to be printed. In such a
case, set the ``is_safe`` option:: case, set the ``is_safe`` option::
$filter = new Twig_SimpleFilter('nl2br', 'nl2br', array('is_safe' => array('html'))); $filter = new Twig_SimpleFilter('nl2br', 'nl2br', array('is_safe' => array('html')));
Some filters may need to work on input that is already escaped or safe, for Some filters may need to work on input that is already escaped or safe, for
example when adding (safe) html tags to originally unsafe output. In such a example when adding (safe) HTML tags to originally unsafe output. In such a
case, set the ``pre_escape`` option to escape the input data before it is run case, set the ``pre_escape`` option to escape the input data before it is run
through your filter:: through your filter::
...@@ -241,11 +241,11 @@ The following filters will be matched by the above defined dynamic filter: ...@@ -241,11 +241,11 @@ The following filters will be matched by the above defined dynamic filter:
A dynamic filter can define more than one dynamic parts:: A dynamic filter can define more than one dynamic parts::
$filter = new Twig_SimpleFilter('*_path', function ($name, $suffix, $arguments) { $filter = new Twig_SimpleFilter('*_path_*', function ($name, $suffix, $arguments) {
// ... // ...
}); });
The filter will receive all dynamic part values before the normal filters The filter will receive all dynamic part values before the normal filter
arguments, but after the environment and the context. For instance, a call to arguments, but after the environment and the context. For instance, a call to
``'foo'|a_path_b()`` will result in the following arguments to be passed to ``'foo'|a_path_b()`` will result in the following arguments to be passed to
the filter: ``('a', 'b', 'foo')``. the filter: ``('a', 'b', 'foo')``.
...@@ -277,12 +277,64 @@ to create an instance of ``Twig_SimpleTest``:: ...@@ -277,12 +277,64 @@ to create an instance of ``Twig_SimpleTest``::
}); });
$twig->addTest($test); $twig->addTest($test);
Tests do not support any options. Tests allow you to create custom application specific logic for evaluating
boolean conditions. As a simple example, let's create a Twig test that checks if
objects are 'red'::
$twig = new Twig_Environment($loader)
$test = new Twig_SimpleTest('red', function ($value) {
if (isset($value->color) && $value->color == 'red') {
return true;
}
if (isset($value->paint) && $value->paint == 'red') {
return true;
}
return false;
});
$twig->addTest($test);
Test functions should always return true/false.
When creating tests you can use the ``node_class`` option to provide custom test
compilation. This is useful if your test can be compiled into PHP primitives.
This is used by many of the tests built into Twig::
$twig = new Twig_Environment($loader)
$test = new Twig_SimpleTest(
'odd',
null,
array('node_class' => 'Twig_Node_Expression_Test_Odd'));
$twig->addTest($test);
class Twig_Node_Expression_Test_Odd extends Twig_Node_Expression_Test
{
public function compile(Twig_Compiler $compiler)
{
$compiler
->raw('(')
->subcompile($this->getNode('node'))
->raw(' % 2 == 1')
->raw(')')
;
}
}
The above example shows how you can create tests that use a node class. The
node class has access to one sub-node called 'node'. This sub-node contains the
value that is being tested. When the ``odd`` filter is used in code such as:
.. code-block:: jinja
{% if my_value is odd %}
The ``node`` sub-node will contain an expression of ``my_value``. Node-based
tests also have access to the ``arguments`` node. This node will contain the
various other arguments that have been provided to your test.
Tags Tags
---- ----
One of the most exciting feature of a template engine like Twig is the One of the most exciting features of a template engine like Twig is the
possibility to define new language constructs. This is also the most complex possibility to define new language constructs. This is also the most complex
feature as you need to understand how Twig's internals work. feature as you need to understand how Twig's internals work.
...@@ -330,14 +382,15 @@ Now, let's see the actual code of this class:: ...@@ -330,14 +382,15 @@ Now, let's see the actual code of this class::
{ {
public function parse(Twig_Token $token) public function parse(Twig_Token $token)
{ {
$lineno = $token->getLine(); $parser = $this->parser;
$name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(); $stream = $parser->getStream();
$this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, '=');
$value = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$stream->expect(Twig_Token::OPERATOR_TYPE, '=');
$value = $parser->getExpressionParser()->parseExpression();
$stream->expect(Twig_Token::BLOCK_END_TYPE);
return new Project_Set_Node($name, $value, $lineno, $this->getTag()); return new Project_Set_Node($name, $value, $token->getLine(), $this->getTag());
} }
public function getTag() public function getTag()
...@@ -384,9 +437,9 @@ The ``Project_Set_Node`` class itself is rather simple:: ...@@ -384,9 +437,9 @@ The ``Project_Set_Node`` class itself is rather simple::
class Project_Set_Node extends Twig_Node class Project_Set_Node extends Twig_Node
{ {
public function __construct($name, Twig_Node_Expression $value, $lineno, $tag = null) public function __construct($name, Twig_Node_Expression $value, $line, $tag = null)
{ {
parent::__construct(array('value' => $value), array('name' => $name), $lineno, $tag); parent::__construct(array('value' => $value), array('name' => $name), $line, $tag);
} }
public function compile(Twig_Compiler $compiler) public function compile(Twig_Compiler $compiler)
...@@ -640,7 +693,7 @@ responsible for parsing the tag and compiling it to PHP. ...@@ -640,7 +693,7 @@ responsible for parsing the tag and compiling it to PHP.
Operators Operators
~~~~~~~~~ ~~~~~~~~~
The ``getOperators()`` methods allows to add new operators. Here is how to add The ``getOperators()`` methods lets you add new operators. Here is how to add
``!``, ``||``, and ``&&`` operators:: ``!``, ``||``, and ``&&`` operators::
class Project_Twig_Extension extends Twig_Extension class Project_Twig_Extension extends Twig_Extension
...@@ -664,7 +717,7 @@ The ``getOperators()`` methods allows to add new operators. Here is how to add ...@@ -664,7 +717,7 @@ The ``getOperators()`` methods allows to add new operators. Here is how to add
Tests Tests
~~~~~ ~~~~~
The ``getTests()`` methods allows to add new test functions:: The ``getTests()`` method lets you add new test functions::
class Project_Twig_Extension extends Twig_Extension class Project_Twig_Extension extends Twig_Extension
{ {
...@@ -682,16 +735,8 @@ Overloading ...@@ -682,16 +735,8 @@ Overloading
----------- -----------
To overload an already defined filter, test, operator, global variable, or To overload an already defined filter, test, operator, global variable, or
function, define it again **as late as possible**:: function, re-define it in an extension and register it **as late as
possible** (order matters)::
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_SimpleFilter('date', function ($timestamp, $format = 'F j, Y H:i') {
// do something different from the built-in date filter
}));
Here, we have overloaded the built-in ``date`` filter with a custom one.
That also works with an extension::
class MyCoreExtension extends Twig_Extension class MyCoreExtension extends Twig_Extension
{ {
...@@ -716,6 +761,19 @@ That also works with an extension:: ...@@ -716,6 +761,19 @@ That also works with an extension::
$twig = new Twig_Environment($loader); $twig = new Twig_Environment($loader);
$twig->addExtension(new MyCoreExtension()); $twig->addExtension(new MyCoreExtension());
Here, we have overloaded the built-in ``date`` filter with a custom one.
If you do the same on the Twig_Environment itself, beware that it takes
precedence over any other registered extensions::
$twig = new Twig_Environment($loader);
$twig->addFilter(new Twig_SimpleFilter('date', function ($timestamp, $format = 'F j, Y H:i') {
// do something different from the built-in date filter
}));
// the date filter will come from the above registration, not
// from the registered extension below
$twig->addExtension(new MyCoreExtension());
.. caution:: .. caution::
Note that overloading the built-in Twig elements is not recommended as it Note that overloading the built-in Twig elements is not recommended as it
......
...@@ -244,14 +244,14 @@ Automatic Escaping ...@@ -244,14 +244,14 @@ Automatic Escaping
~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~
If automatic escaping is enabled, the output of the filter may be escaped If automatic escaping is enabled, the output of the filter may be escaped
before printing. If your filter acts as an escaper (or explicitly outputs html before printing. If your filter acts as an escaper (or explicitly outputs HTML
or javascript code), you will want the raw output to be printed. In such a or JavaScript code), you will want the raw output to be printed. In such a
case, set the ``is_safe`` option:: case, set the ``is_safe`` option::
$filter = new Twig_Filter_Function('nl2br', array('is_safe' => array('html'))); $filter = new Twig_Filter_Function('nl2br', array('is_safe' => array('html')));
Some filters may need to work on input that is already escaped or safe, for Some filters may need to work on input that is already escaped or safe, for
example when adding (safe) html tags to originally unsafe output. In such a example when adding (safe) HTML tags to originally unsafe output. In such a
case, set the ``pre_escape`` option to escape the input data before it is run case, set the ``pre_escape`` option to escape the input data before it is run
through your filter:: through your filter::
...@@ -266,7 +266,7 @@ Dynamic Filters ...@@ -266,7 +266,7 @@ Dynamic Filters
A filter name containing the special ``*`` character is a dynamic filter as A filter name containing the special ``*`` character is a dynamic filter as
the ``*`` can be any string:: the ``*`` can be any string::
$twig->addFilter('*_path', new Twig_Filter_Function('twig_path')); $twig->addFilter('*_path_*', new Twig_Filter_Function('twig_path'));
function twig_path($name, $arguments) function twig_path($name, $arguments)
{ {
......
...@@ -348,10 +348,10 @@ tag, ``autoescape``, and a filter, ``raw``. ...@@ -348,10 +348,10 @@ tag, ``autoescape``, and a filter, ``raw``.
When creating the escaper extension, you can switch on or off the global When creating the escaper extension, you can switch on or off the global
output escaping strategy:: output escaping strategy::
$escaper = new Twig_Extension_Escaper(true); $escaper = new Twig_Extension_Escaper('html');
$twig->addExtension($escaper); $twig->addExtension($escaper);
If set to ``true``, all variables in templates are escaped (using the ``html`` If set to ``html``, all variables in templates are escaped (using the ``html``
escaping strategy), except those using the ``raw`` filter: escaping strategy), except those using the ``raw`` filter:
.. code-block:: jinja .. code-block:: jinja
...@@ -417,15 +417,15 @@ The escaping rules are implemented as follows: ...@@ -417,15 +417,15 @@ The escaping rules are implemented as follows:
{{ var|upper|raw }} {# won't be escaped #} {{ var|upper|raw }} {# won't be escaped #}
* Automatic escaping is not applied if the last filter in the chain is marked * Automatic escaping is not applied if the last filter in the chain is marked
safe for the current context (e.g. ``html`` or ``js``). ``escaper`` and safe for the current context (e.g. ``html`` or ``js``). ``escape`` and
``escaper('html')`` are marked safe for html, ``escaper('js')`` is marked ``escape('html')`` are marked safe for HTML, ``escape('js')`` is marked
safe for javascript, ``raw`` is marked safe for everything. safe for JavaScript, ``raw`` is marked safe for everything.
.. code-block:: jinja .. code-block:: jinja
{% autoescape 'js' %} {% autoescape 'js' %}
{{ var|escape('html') }} {# will be escaped for html and javascript #} {{ var|escape('html') }} {# will be escaped for HTML and JavaScript #}
{{ var }} {# will be escaped for javascript #} {{ var }} {# will be escaped for JavaScript #}
{{ var|escape('js') }} {# won't be double-escaped #} {{ var|escape('js') }} {# won't be double-escaped #}
{% endautoescape %} {% endautoescape %}
......
...@@ -90,7 +90,7 @@ standards: ...@@ -90,7 +90,7 @@ standards:
{% set foo_bar = 'foo' %} {% set foo_bar = 'foo' %}
* Indent your code inside tags (use the same indentation as the one used for * Indent your code inside tags (use the same indentation as the one used for
the main language of the file): the target language of the rendered template):
.. code-block:: jinja .. code-block:: jinja
......
...@@ -77,6 +77,9 @@ Tests ...@@ -77,6 +77,9 @@ Tests
removed in Twig 3.x (use ``Twig_Test`` instead). In Twig 2.x, removed in Twig 3.x (use ``Twig_Test`` instead). In Twig 2.x,
``Twig_SimpleTest`` is just an alias for ``Twig_Test``. ``Twig_SimpleTest`` is just an alias for ``Twig_Test``.
* The ``sameas`` and ``divisibleby`` tests are deprecated in favor of ``same
as`` and ``divisible by`` respectively.
Interfaces Interfaces
---------- ----------
...@@ -88,7 +91,9 @@ Interfaces ...@@ -88,7 +91,9 @@ Interfaces
* ``Twig_NodeInterface`` (use ``Twig_Node`` instead) * ``Twig_NodeInterface`` (use ``Twig_Node`` instead)
* ``Twig_ParserInterface`` (use ``Twig_Parser`` instead) * ``Twig_ParserInterface`` (use ``Twig_Parser`` instead)
* ``Twig_ExistsLoaderInterface`` (merged with ``Twig_LoaderInterface``) * ``Twig_ExistsLoaderInterface`` (merged with ``Twig_LoaderInterface``)
* ``Twig_TemplateInterface`` (use ``Twig_Template`` instead) * ``Twig_TemplateInterface`` (use ``Twig_Template`` instead, and use
those constants Twig_Template::ANY_CALL, Twig_Template::ARRAY_CALL,
Twig_Template::METHOD_CALL)
Globals Globals
------- -------
......
...@@ -19,6 +19,10 @@ The ``date`` filter formats a date to a given format: ...@@ -19,6 +19,10 @@ The ``date`` filter formats a date to a given format:
{{ post.published_at|date("m/d/Y") }} {{ post.published_at|date("m/d/Y") }}
The format specifier is the same as supported by `date`_,
except when the filtered data is of type `DateInterval`_, when the format must conform to
`DateInterval::format`_ instead.
The ``date`` filter accepts strings (it must be in a format supported by the The ``date`` filter accepts strings (it must be in a format supported by the
`strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For `strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
instance, to display the current date, filter the word "now": instance, to display the current date, filter the word "now":
...@@ -86,3 +90,5 @@ Arguments ...@@ -86,3 +90,5 @@ Arguments
.. _`strtotime`: http://www.php.net/strtotime .. _`strtotime`: http://www.php.net/strtotime
.. _`DateTime`: http://www.php.net/DateTime .. _`DateTime`: http://www.php.net/DateTime
.. _`DateInterval`: http://www.php.net/DateInterval .. _`DateInterval`: http://www.php.net/DateInterval
.. _`date`: http://www.php.net/date
.. _`DateInterval::format`: http://www.php.net/DateInterval.format
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
The ``css``, ``url``, and ``html_attr`` strategies were added in Twig The ``css``, ``url``, and ``html_attr`` strategies were added in Twig
1.9.0. 1.9.0.
.. versionadded:: 1.14.0
The ability to define custom escapers was added in Twig 1.14.0.
The ``escape`` filter escapes a string for safe insertion into the final The ``escape`` filter escapes a string for safe insertion into the final
output. It supports different escaping strategies depending on the template output. It supports different escaping strategies depending on the template
context. context.
...@@ -84,6 +87,26 @@ The ``escape`` filter supports the following escaping strategies: ...@@ -84,6 +87,26 @@ The ``escape`` filter supports the following escaping strategies:
{{ var|escape(strategy)|raw }} {# won't be double-escaped #} {{ var|escape(strategy)|raw }} {# won't be double-escaped #}
{% endautoescape %} {% endautoescape %}
Custom Escapers
---------------
You can define custom escapers by calling the ``setEscaper()`` method on the
``core`` extension instance. The first argument is the escaper name (to be
used in the ``escape`` call) and the second one must be a valid PHP callable:
.. code-block:: php
$twig = new Twig_Environment($loader);
$twig->getExtension('core')->setEscaper('csv', 'csv_escaper'));
When called by Twig, the callable receives the Twig environment instance, the
string to escape, and the charset.
.. note::
Built-in escapers cannot be overridden mainly they should be considered as
the final implementation and also for better performance.
Arguments Arguments
--------- ---------
......
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
========== ==========
The ``format`` filter formats a given string by replacing the placeholders The ``format`` filter formats a given string by replacing the placeholders
(placeholders follows the `printf`_ notation): (placeholders follows the `sprintf`_ notation):
.. code-block:: jinja .. code-block:: jinja
{{ "I like %s and %s."|format(foo, "bar") }} {{ "I like %s and %s."|format(foo, "bar") }}
{# returns I like foo and bar {# outputs I like foo and bar
if the foo parameter equals to the foo string. #} if the foo parameter equals to the foo string. #}
.. _`printf`: http://www.php.net/printf .. _`sprintf`: http://www.php.net/sprintf
.. seealso:: :doc:`replace<replace>` .. seealso:: :doc:`replace<replace>`
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment