fixed filter tag
authorFabien Potencier <fabien.potencier@gmail.com>
Sat, 5 Feb 2011 22:57:22 +0000 (23:57 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Sat, 5 Feb 2011 22:57:22 +0000 (23:57 +0100)
lib/Twig/ExpressionParser.php
lib/Twig/Node/Expression/BlockReference.php
lib/Twig/TokenParser/Filter.php
test/Twig/Tests/Fixtures/tags/filter/json_encode.test [new file with mode: 0644]

index 774ff54..fbf927e 100644 (file)
@@ -245,7 +245,7 @@ class Twig_ExpressionParser
         }
 
         if ('block' === $node->getAttribute('name')) {
-            return new Twig_Node_Expression_BlockReference($args->getNode(0), $node->getLine());
+            return new Twig_Node_Expression_BlockReference($args->getNode(0), false, $node->getLine());
         }
 
         if (null !== $alias = $this->parser->getImportedFunction($node->getAttribute('name'))) {
index 259967c..592e72e 100644 (file)
@@ -18,9 +18,9 @@
  */
 class Twig_Node_Expression_BlockReference extends Twig_Node_Expression
 {
-    public function __construct(Twig_NodeInterface $name, $lineno, $tag = null)
+    public function __construct(Twig_NodeInterface $name, $asString = false, $lineno, $tag = null)
     {
-        parent::__construct(array('name' => $name), array(), $lineno, $tag);
+        parent::__construct(array('name' => $name), array('as_string' => $asString), $lineno, $tag);
     }
 
     /**
@@ -30,6 +30,10 @@ class Twig_Node_Expression_BlockReference extends Twig_Node_Expression
      */
     public function compile(Twig_Compiler $compiler)
     {
+        if ($this->getAttribute('as_string')) {
+            $compiler->raw('(string) ');
+        }
+
         $compiler
             ->raw("\$this->renderBlock(")
             ->subcompile($this->getNode('name'))
index 4ef8023..f702154 100644 (file)
@@ -20,7 +20,7 @@ class Twig_TokenParser_Filter extends Twig_TokenParser
     public function parse(Twig_Token $token)
     {
         $name = $this->parser->getVarName();
-        $ref = new Twig_Node_Expression_BlockReference(new Twig_Node_Expression_Constant($name, $token->getLine()), $token->getLine(), $this->getTag());
+        $ref = new Twig_Node_Expression_BlockReference(new Twig_Node_Expression_Constant($name, $token->getLine()), true, $token->getLine(), $this->getTag());
 
         $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag());
         $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
diff --git a/test/Twig/Tests/Fixtures/tags/filter/json_encode.test b/test/Twig/Tests/Fixtures/tags/filter/json_encode.test
new file mode 100644 (file)
index 0000000..3e7148b
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+"filter" tag applies a filter on its children
+--TEMPLATE--
+{% filter json_encode|raw %}test{% endfilter %}
+--DATA--
+return array()
+--EXPECT--
+"test"