From: Fabien Potencier Date: Sat, 29 Oct 2011 12:50:10 +0000 (+0200) Subject: fixed in operator for empty strings X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=dfdd555b9a343e35ad4dc6983208fa42adcd3079;p=web%2Fkonrad%2Ftwig.git fixed in operator for empty strings --- diff --git a/CHANGELOG b/CHANGELOG index 1b6a012..d758fa9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.4.0 + * fixed in operator for empty strings * fixed the "defined" test and the "default" filter (now works with more than one call (foo.bar.foo) and for both values of the strict_variables option) * changed the way extensions are loaded (addFilter/addFunction/addGlobal/addTest/addNodeVisitor/addTokenParser/addExtension can now be called in any order) * added Twig_Environment::display() diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 965a9cc..3aae743 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -443,6 +443,9 @@ function twig_in_filter($value, $compare) if (is_array($compare)) { return in_array($value, $compare); } elseif (is_string($compare)) { + if (!$compare) { + return empty($value); + } return false !== strpos($compare, (string) $value); } elseif (is_object($compare) && $compare instanceof Traversable) { return in_array($value, iterator_to_array($compare, false)); diff --git a/test/Twig/Tests/Fixtures/expressions/in.test b/test/Twig/Tests/Fixtures/tests/in.test similarity index 77% rename from test/Twig/Tests/Fixtures/expressions/in.test rename to test/Twig/Tests/Fixtures/tests/in.test index a5cbfb8..ba40f25 100644 --- a/test/Twig/Tests/Fixtures/expressions/in.test +++ b/test/Twig/Tests/Fixtures/tests/in.test @@ -12,9 +12,17 @@ TRUE {% else %} TRUE {% endif %} +{% if '' not in foo %} +TRUE +{% endif %} +{% if '' in '' %} +TRUE +{% endif %} --DATA-- return array('bar' => 'bar', 'foo' => array('bar' => 'bar')) --EXPECT-- TRUE TRUE TRUE +TRUE +TRUE