Commit af1ca73a authored by webchick's avatar webchick

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

parent 56d3a638
......@@ -3,7 +3,7 @@
"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"
],
"hash": "06006c1512fb829fcdb498b7e515901d",
"hash": "14b55ea7402f04abe36b677892e442f3",
"packages": [
{
"name": "doctrine/annotations",
......@@ -1877,16 +1877,16 @@
},
{
"name": "twig/twig",
"version": "v1.12.3",
"version": "v1.15.0",
"source": {
"type": "git",
"url": "https://github.com/fabpot/Twig.git",
"reference": "v1.12.3"
"reference": "85e4ff98000157ff753d934b9f13659a953f5666"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/fabpot/Twig/zipball/v1.12.3",
"reference": "v1.12.3",
"url": "https://api.github.com/repos/fabpot/Twig/zipball/85e4ff98000157ff753d934b9f13659a953f5666",
"reference": "85e4ff98000157ff753d934b9f13659a953f5666",
"shasum": ""
},
"require": {
......@@ -1895,7 +1895,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.12-dev"
"dev-master": "1.15-dev"
}
},
"autoload": {
......@@ -1905,7 +1905,7 @@
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3"
"BSD-3-Clause"
],
"authors": [
{
......@@ -1922,7 +1922,7 @@
"keywords": [
"templating"
],
"time": "2013-04-08 12:40:11"
"time": "2013-12-06 07:47:10"
},
{
"name": "zendframework/zend-escaper",
......
......@@ -4,6 +4,7 @@ php:
- 5.2
- 5.3
- 5.4
- 5.5
env:
- TWIG_EXT=no
......
......@@ -4,6 +4,10 @@ Lead Developer:
- Fabien Potencier <fabien.potencier@symfony-project.org>
C Extension Developer:
- Derick Rethans <derick@derickrethans.nl>
Project Founder:
- 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)
* 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
==============================================================
[![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
and documentation).
......@@ -12,6 +10,6 @@ inspired the Twig runtime environment.
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 @@
"description": "Twig, the flexible, fast, and secure template language for PHP",
"keywords": ["templating"],
"homepage": "http://twig.sensiolabs.org",
"license": "BSD-3",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Fabien Potencier",
......@@ -25,7 +25,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.12-dev"
"dev-master": "1.15-dev"
}
}
}
......@@ -177,7 +177,7 @@ argument::
$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
......@@ -191,7 +191,7 @@ environment as the first argument to the filter call::
return str_rot13($string);
}, array('needs_environment' => true));
Context aware Filters
Context-aware Filters
~~~~~~~~~~~~~~~~~~~~~
If you want to access the current context in your filter, set the
......@@ -211,14 +211,14 @@ Automatic Escaping
~~~~~~~~~~~~~~~~~~
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
case, set the ``is_safe`` option::
$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
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
through your 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::
$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
``'foo'|a_path_b()`` will result in the following arguments to be passed to
the filter: ``('a', 'b', 'foo')``.
......@@ -277,12 +277,64 @@ to create an instance of ``Twig_SimpleTest``::
});
$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
----
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
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::
{
public function parse(Twig_Token $token)
{
$lineno = $token->getLine();
$name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
$this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, '=');
$value = $this->parser->getExpressionParser()->parseExpression();
$parser = $this->parser;
$stream = $parser->getStream();
$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()
......@@ -384,9 +437,9 @@ The ``Project_Set_Node`` class itself is rather simple::
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)
......@@ -640,7 +693,7 @@ responsible for parsing the tag and compiling it to PHP.
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::
class Project_Twig_Extension extends Twig_Extension
......@@ -664,7 +717,7 @@ The ``getOperators()`` methods allows to add new operators. Here is how to add
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
{
......@@ -682,16 +735,8 @@ Overloading
-----------
To overload an already defined filter, test, operator, global variable, or
function, define it again **as late as possible**::
$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::
function, re-define it in an extension and register it **as late as
possible** (order matters)::
class MyCoreExtension extends Twig_Extension
{
......@@ -716,6 +761,19 @@ That also works with an extension::
$twig = new Twig_Environment($loader);
$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::
Note that overloading the built-in Twig elements is not recommended as it
......
......@@ -244,14 +244,14 @@ Automatic Escaping
~~~~~~~~~~~~~~~~~~
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
or javascript code), you will want the raw output to be printed. In such a
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
case, set the ``is_safe`` option::
$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
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
through your filter::
......@@ -266,7 +266,7 @@ Dynamic Filters
A filter name containing the special ``*`` character is a dynamic filter as
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)
{
......
......@@ -348,10 +348,10 @@ tag, ``autoescape``, and a filter, ``raw``.
When creating the escaper extension, you can switch on or off the global
output escaping strategy::
$escaper = new Twig_Extension_Escaper(true);
$escaper = new Twig_Extension_Escaper('html');
$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:
.. code-block:: jinja
......@@ -417,15 +417,15 @@ The escaping rules are implemented as follows:
{{ var|upper|raw }} {# won't be escaped #}
* 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
``escaper('html')`` are marked safe for html, ``escaper('js')`` is marked
safe for javascript, ``raw`` is marked safe for everything.
safe for the current context (e.g. ``html`` or ``js``). ``escape`` and
``escape('html')`` are marked safe for HTML, ``escape('js')`` is marked
safe for JavaScript, ``raw`` is marked safe for everything.
.. code-block:: jinja
{% autoescape 'js' %}
{{ var|escape('html') }} {# will be escaped for html and javascript #}
{{ var }} {# will be escaped for javascript #}
{{ var|escape('html') }} {# will be escaped for HTML and JavaScript #}
{{ var }} {# will be escaped for JavaScript #}
{{ var|escape('js') }} {# won't be double-escaped #}
{% endautoescape %}
......@@ -499,16 +499,16 @@ to enable by passing them to the constructor::
Twig supports the following optimizations:
* ``Twig_NodeVisitor_Optimizer::OPTIMIZE_ALL``, enables all optimizations
(this is the default value).
(this is the default value).
* ``Twig_NodeVisitor_Optimizer::OPTIMIZE_NONE``, disables all optimizations.
This reduces the compilation time, but it can increase the execution time
and the consumed memory.
This reduces the compilation time, but it can increase the execution time
and the consumed memory.
* ``Twig_NodeVisitor_Optimizer::OPTIMIZE_FOR``, optimizes the ``for`` tag by
removing the ``loop`` variable creation whenever possible.
removing the ``loop`` variable creation whenever possible.
* ``Twig_NodeVisitor_Optimizer::OPTIMIZE_RAW_FILTER``, removes the ``raw``
filter whenever possible.
filter whenever possible.
* ``Twig_NodeVisitor_Optimizer::OPTIMIZE_VAR_ACCESS``, simplifies the creation
and access of variables in the compiled templates whenever possible.
and access of variables in the compiled templates whenever possible.
Exceptions
----------
......
......@@ -90,7 +90,7 @@ standards:
{% set foo_bar = 'foo' %}
* 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
......
......@@ -77,6 +77,9 @@ Tests
removed in Twig 3.x (use ``Twig_Test`` instead). In Twig 2.x,
``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
----------
......@@ -88,7 +91,9 @@ Interfaces
* ``Twig_NodeInterface`` (use ``Twig_Node`` instead)
* ``Twig_ParserInterface`` (use ``Twig_Parser`` instead)
* ``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
-------
......
......@@ -6,9 +6,9 @@ The ``abs`` filter returns the absolute value.
.. code-block:: jinja
{# number = -5 #}
{{ number|abs }}
{# outputs 5 #}
.. note::
......
......@@ -14,11 +14,11 @@ missing items:
<table>
{% for row in items|batch(3, 'No item') %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
......@@ -27,19 +27,19 @@ The above example will be rendered as:
.. code-block:: jinja
<table>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
<tr>
<td>d</td>
<td>e</td>
<td>f</td>
</tr>
<tr>
<td>g</td>
<td>No item</td>
<td>No item</td>
<tr>
<td>g</td>
<td>No item</td>
<td>No item</td>
</tr>
</table>
......@@ -21,8 +21,8 @@ is the input charset:
Arguments
---------
* ``from``: The input charset
* ``to``: The output charset
* ``from``: The input charset
* ``to``: The output charset
.. _`iconv`: http://php.net/iconv
.. _`mbstring`: http://php.net/mbstring
......@@ -19,6 +19,10 @@ The ``date`` filter formats a date to a given format:
{{ 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
`strtotime`_ function), `DateTime`_ instances, or `DateInterval`_ instances. For
instance, to display the current date, filter the word "now":
......@@ -80,9 +84,11 @@ The default timezone can also be set globally by calling ``setTimezone()``:
Arguments
---------
* ``format``: The date format
* ``timezone``: The date timezone
* ``format``: The date format
* ``timezone``: The date timezone
.. _`strtotime`: http://www.php.net/strtotime
.. _`DateTime`: http://www.php.net/DateTime
.. _`DateInterval`: http://www.php.net/DateInterval
.. _`strtotime`: http://www.php.net/strtotime
.. _`DateTime`: http://www.php.net/DateTime
.. _`DateInterval`: http://www.php.net/DateInterval
.. _`date`: http://www.php.net/date
.. _`DateInterval::format`: http://www.php.net/DateInterval.format
......@@ -17,7 +17,7 @@ it with the :doc:`date<date>` filter for formatting.
Arguments
---------
* ``modifier``: The modifier
* ``modifier``: The modifier
.. _`strtotime`: http://www.php.net/strtotime
.. _`DateTime`: http://www.php.net/DateTime
......@@ -30,4 +30,4 @@ undefined:
Arguments
---------
* ``default``: The default value
* ``default``: The default value
......@@ -5,6 +5,9 @@
The ``css``, ``url``, and ``html_attr`` strategies were added in Twig
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
output. It supports different escaping strategies depending on the template
context.
......@@ -84,10 +87,30 @@ The ``escape`` filter supports the following escaping strategies:
{{ var|escape(strategy)|raw }} {# won't be double-escaped #}
{% 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
---------
* ``strategy``: The escaping strategy
* ``charset``: The string charset
* ``strategy``: The escaping strategy
* ``charset``: The string charset
.. _`htmlspecialchars`: http://php.net/htmlspecialchars
......@@ -2,15 +2,15 @@
==========
The ``format`` filter formats a given string by replacing the placeholders
(placeholders follows the `printf`_ notation):
(placeholders follows the `sprintf`_ notation):
.. code-block:: jinja
{{ "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. #}
.. _`printf`: http://www.php.net/printf
.. _`sprintf`: http://www.php.net/sprintf
.. seealso:: :doc:`replace<replace>`
......@@ -27,6 +27,7 @@ Filters
raw
replace
reverse
round
slice
sort
split
......
......@@ -15,9 +15,9 @@ define it with the optional first parameter:
.. code-block:: jinja
{{ [1, 2, 3]|join('|') }}
{# returns 1|2|3 #}
{# outputs 1|2|3 #}