From: Fabien Potencier Date: Sat, 18 Dec 2010 10:26:16 +0000 (+0100) Subject: added the empty test X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=0d424bff0db7cbba7ae2e168b49e417a59997170;p=web%2Fkonrad%2Ftwig.git added the empty test --- diff --git a/CHANGELOG b/CHANGELOG index 5f1970f..5626a50 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,8 @@ Backward incompatibilities: Changes: + * added the "empty" test + * 0.9.10 (2010-12-16) Backward incompatibilities: diff --git a/doc/templates.rst b/doc/templates.rst index 47df16b..a1a4073 100644 --- a/doc/templates.rst +++ b/doc/templates.rst @@ -1408,6 +1408,18 @@ useful if you use the ``strict_variables`` option: ... {% endif %} +``empty`` +~~~~~~~~~ + +``empty`` checks if a variable is empty: + +.. code-block:: jinja + + {# evaluates to true if the foo variable is null, false, or the empty string #} + {% if foo is empty %} + ... + {% endif %} + Extensions ---------- diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 96b5192..77bef20 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -99,6 +99,7 @@ class Twig_Extension_Core extends Twig_Extension 'none' => new Twig_Test_Function('twig_test_none'), 'divisibleby' => new Twig_Test_Function('twig_test_divisibleby'), 'constant' => new Twig_Test_Function('twig_test_constant'), + 'empty' => new Twig_Test_Function('twig_test_empty'), ); } @@ -470,3 +471,8 @@ function twig_test_defined($name, $context) { return array_key_exists($name, $context); } + +function twig_test_empty($value) +{ + return null === $value || false === $value || '' === (string) $value; +} diff --git a/test/Twig/Tests/Fixtures/tests/empty.test b/test/Twig/Tests/Fixtures/tests/empty.test new file mode 100644 index 0000000..d183a93 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tests/empty.test @@ -0,0 +1,12 @@ +--TEST-- +"empty" test +--TEMPLATE-- +{{ foo is empty ? 'ok' : 'ko' }} +{{ bar is empty ? 'ok' : 'ko' }} +{{ foobar is empty ? 'ok' : 'ko' }} +--DATA-- +return array('foo' => '', 'bar' => null, 'foobar' => false); +--EXPECT-- +ok +ok +ok