mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
added the empty test
This commit is contained in:
@@ -6,6 +6,8 @@ Backward incompatibilities:
|
||||
|
||||
Changes:
|
||||
|
||||
* added the "empty" test
|
||||
|
||||
* 0.9.10 (2010-12-16)
|
||||
|
||||
Backward incompatibilities:
|
||||
|
||||
@@ -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
|
||||
----------
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user