mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 20:06:31 +00:00
allowed tests to be made of 1 or 2 words
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
* 1.14.2 (2013-XX-XX)
|
||||
|
||||
* n/a
|
||||
* allowed tests to be made of 1 or 2 words (like "same as" or "divisible by")
|
||||
|
||||
* 1.14.1 (2013-10-15)
|
||||
|
||||
|
||||
@@ -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
|
||||
----------
|
||||
|
||||
|
||||
+3
-3
@@ -739,16 +739,16 @@ Tests can accept arguments too:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% if loop.index is divisibleby(3) %}
|
||||
{% if post.status is constant('Post::PUBLISHED') %}
|
||||
|
||||
Tests can be negated by using the ``is not`` operator:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% if loop.index is not divisibleby(3) %}
|
||||
{% if post.status is constant('Post::PUBLISHED') %}
|
||||
|
||||
{# is equivalent to #}
|
||||
{% if not (loop.index is divisibleby(3)) %}
|
||||
{% if not (post.status is constant('Post::PUBLISHED')) %}
|
||||
|
||||
Go to the :doc:`tests<tests/index>` page to learn more about the built-in
|
||||
tests.
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
``divisibleby``
|
||||
===============
|
||||
``divisible by``
|
||||
================
|
||||
|
||||
``divisibleby`` checks if a variable is divisible by a number:
|
||||
.. versionadded:: 1.14.2
|
||||
The ``divisible by`` test was added in Twig 1.14.2 as an alias for
|
||||
``divisibleby``.
|
||||
|
||||
``divisible by`` checks if a variable is divisible by a number:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% if loop.index is divisibleby(3) %}
|
||||
{% if loop.index is divisible by(3) %}
|
||||
...
|
||||
{% endif %}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
``sameas``
|
||||
==========
|
||||
``same as``
|
||||
===========
|
||||
|
||||
``sameas`` checks if a variable points to the same memory address than another
|
||||
variable:
|
||||
.. versionadded:: 1.14.2
|
||||
The ``same as`` test was added in Twig 1.14.2 as an alias for ``sameas``.
|
||||
|
||||
``same as`` checks if a variable points to the same memory address than
|
||||
another variable:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% if foo.attribute is sameas(false) %}
|
||||
{% if foo.attribute is same as(false) %}
|
||||
the foo attribute really is the ``false`` PHP value
|
||||
{% endif %}
|
||||
|
||||
@@ -230,9 +230,11 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
new Twig_SimpleTest('odd', null, array('node_class' => 'Twig_Node_Expression_Test_Odd')),
|
||||
new Twig_SimpleTest('defined', null, array('node_class' => 'Twig_Node_Expression_Test_Defined')),
|
||||
new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
|
||||
new Twig_SimpleTest('same as', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
|
||||
new Twig_SimpleTest('none', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
|
||||
new Twig_SimpleTest('null', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
|
||||
new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
|
||||
new Twig_SimpleTest('divisible by', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
|
||||
new Twig_SimpleTest('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')),
|
||||
new Twig_SimpleTest('empty', 'twig_test_empty'),
|
||||
new Twig_SimpleTest('iterable', 'twig_test_iterable'),
|
||||
@@ -293,13 +295,12 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
{
|
||||
$stream = $parser->getStream();
|
||||
$name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
|
||||
$class = $this->getTestNodeClass($parser, $name, $node->getLine());
|
||||
$arguments = null;
|
||||
if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
|
||||
$arguments = $parser->getExpressionParser()->parseArguments(true);
|
||||
}
|
||||
|
||||
$class = $this->getTestNodeClass($parser, $name, $node->getLine());
|
||||
|
||||
return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
|
||||
}
|
||||
|
||||
@@ -307,7 +308,21 @@ class Twig_Extension_Core extends Twig_Extension
|
||||
{
|
||||
$env = $parser->getEnvironment();
|
||||
$testMap = $env->getTests();
|
||||
if (!isset($testMap[$name])) {
|
||||
$testName = null;
|
||||
if (isset($testMap[$name])) {
|
||||
$testName = $name;
|
||||
} elseif ($parser->getStream()->test(Twig_Token::NAME_TYPE)) {
|
||||
// try 2-words tests
|
||||
$name = $name.' '.$parser->getCurrentToken()->getValue();
|
||||
|
||||
if (isset($testMap[$name])) {
|
||||
$parser->getStream()->next();
|
||||
|
||||
$testName = $name;
|
||||
}
|
||||
}
|
||||
|
||||
if (null === $testName) {
|
||||
$message = sprintf('The test "%s" does not exist', $name);
|
||||
if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
|
||||
$message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
--TEST--
|
||||
Twig supports the "divisible by" operator
|
||||
--TEMPLATE--
|
||||
{{ 8 is divisibleby(2) ? 'OK' }}
|
||||
{{ 8 is not divisibleby(3) ? 'OK' }}
|
||||
{{ 8 is divisible by(2) ? 'OK' }}
|
||||
{{ 8 is not divisible by(3) ? 'OK' }}
|
||||
{{ 8 is divisible by (2) ? 'OK' }}
|
||||
{{ 8 is not
|
||||
divisible
|
||||
by
|
||||
(3) ? 'OK' }}
|
||||
--DATA--
|
||||
return array()
|
||||
--EXPECT--
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
@@ -0,0 +1,21 @@
|
||||
--TEST--
|
||||
Twig supports the "same as" operator
|
||||
--TEMPLATE--
|
||||
{{ 1 is sameas(1) ? 'OK' }}
|
||||
{{ 1 is not sameas(true) ? 'OK' }}
|
||||
{{ 1 is same as(1) ? 'OK' }}
|
||||
{{ 1 is not same as(true) ? 'OK' }}
|
||||
{{ 1 is same as (1) ? 'OK' }}
|
||||
{{ 1 is not
|
||||
same
|
||||
as
|
||||
(true) ? 'OK' }}
|
||||
--DATA--
|
||||
return array()
|
||||
--EXPECT--
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
OK
|
||||
Reference in New Issue
Block a user