diff --git a/doc/advanced.rst b/doc/advanced.rst index 360317cb9..f5ae408e9 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -451,7 +451,7 @@ from the token stream (``$this->parser->getStream()``): type/value a syntax error is thrown. Otherwise, if the type and value are correct, the token is returned and the stream moves to the next token. -* ``look()``: Looks a the next token without consuming it. +* ``look()``: Looks at the next token without consuming it. Parsing expressions is done by calling the ``parseExpression()`` like we did for the ``set`` tag. diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index ae17df848..bc85d048e 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -399,7 +399,7 @@ class Twig_Environment */ public function createTemplate($template) { - $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false)); + $name = sprintf('__string_template__%s', hash('sha256', $template, false)); $loader = new Twig_Loader_Chain(array( new Twig_Loader_Array(array($name => $template)), diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 54bbafb19..fd8b61ae9 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -1144,6 +1144,10 @@ function twig_convert_encoding($string, $to, $from) */ function twig_length_filter(Twig_Environment $env, $thing) { + if (null === $thing) { + return 0; + } + if (is_scalar($thing)) { return mb_strlen($thing, $env->getCharset()); } @@ -1152,7 +1156,11 @@ function twig_length_filter(Twig_Environment $env, $thing) return mb_strlen((string) $thing, $env->getCharset()); } - return count($thing); + if ($thing instanceof \Countable || is_array($thing)) { + return count($thing); + } + + return 1; } /** diff --git a/test/Twig/Tests/Fixtures/filters/length.test b/test/Twig/Tests/Fixtures/filters/length.test index 599c3fd52..a7f1e5031 100644 --- a/test/Twig/Tests/Fixtures/filters/length.test +++ b/test/Twig/Tests/Fixtures/filters/length.test @@ -6,7 +6,9 @@ {{ number|length }} {{ to_string_able|length }} {{ countable|length }} +{{ null|length }} {{ magic|length }} +{{ non_countable|length }} --DATA-- return array( 'array' => array(1, 4), @@ -14,7 +16,9 @@ return array( 'number' => 1000, 'to_string_able' => new ToStringStub('foobar'), 'countable' => new CountableStub(42), /* also asserts we do *not* call __toString() */ - 'magic' => new MagicCallStub(), /* used to assert we do *not* call __call */ + 'null' => null, + 'magic' => new MagicCallStub(), /* used to assert we do *not* call __call */ + 'non_countable' => new \StdClass(), ); --EXPECT-- 2 @@ -22,4 +26,6 @@ return array( 4 6 42 +0 +1 1