added the empty test

This commit is contained in:
Fabien Potencier
2010-12-18 11:26:16 +01:00
parent 14341443dd
commit 0d424bff0d
4 changed files with 32 additions and 0 deletions
+2
View File
@@ -6,6 +6,8 @@ Backward incompatibilities:
Changes:
* added the "empty" test
* 0.9.10 (2010-12-16)
Backward incompatibilities:
+12
View File
@@ -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
----------
+6
View File
@@ -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;
}
+12
View File
@@ -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