From: Fabien Potencier Date: Sat, 28 Sep 2013 17:28:24 +0000 (+0200) Subject: allowed tests to be made of 1 or 2 words X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=52c952c1a663b31daea48291517d574dd13512ab;p=web%2Fkonrad%2Ftwig.git allowed tests to be made of 1 or 2 words --- diff --git a/CHANGELOG b/CHANGELOG index c0fa5d0..44bc6ef 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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) diff --git a/doc/deprecated.rst b/doc/deprecated.rst index ff50f0d..61917a8 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -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 ---------- diff --git a/doc/templates.rst b/doc/templates.rst index 03377ec..2905023 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -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` page to learn more about the built-in tests. diff --git a/doc/tests/divisibleby.rst b/doc/tests/divisibleby.rst index 9b0b964..6c693b2 100644 --- a/doc/tests/divisibleby.rst +++ b/doc/tests/divisibleby.rst @@ -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 %} diff --git a/doc/tests/sameas.rst b/doc/tests/sameas.rst index efb15c3..4e9d6f8 100644 --- a/doc/tests/sameas.rst +++ b/doc/tests/sameas.rst @@ -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 %} diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index b43b57b..32d452d 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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)); diff --git a/test/Twig/Tests/Fixtures/expressions/divisibleby.test b/test/Twig/Tests/Fixtures/expressions/divisibleby.test new file mode 100644 index 0000000..14ee5e4 --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/divisibleby.test @@ -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 diff --git a/test/Twig/Tests/Fixtures/expressions/sameas.test b/test/Twig/Tests/Fixtures/expressions/sameas.test new file mode 100644 index 0000000..9e8514f --- /dev/null +++ b/test/Twig/Tests/Fixtures/expressions/sameas.test @@ -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