added support for short block tag
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Sun, 18 Oct 2009 22:53:21 +0000 (22:53 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Sun, 18 Oct 2009 22:53:21 +0000 (22:53 +0000)
git-svn-id: http://svn.twig-project.org/trunk@76 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/TokenParser/Block.php
test/fixtures/tags/block/basic.test [new file with mode: 0644]

index a6df2a5..077347f 100644 (file)
@@ -14,24 +14,36 @@ class Twig_TokenParser_Block extends Twig_TokenParser
   public function parse(Twig_Token $token)
   {
     $lineno = $token->getLine();
-    $name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
+    $stream = $this->parser->getStream();
+    $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
     if ($this->parser->hasBlock($name))
     {
       throw new Twig_SyntaxError("The block '$name' has already been defined", $lineno);
     }
     $this->parser->setCurrentBlock($name);
-    $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
-    $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
-    if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE))
+
+    if ($stream->test(Twig_Token::BLOCK_END_TYPE))
     {
-      $value = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
+      $stream->next();
 
-      if ($value != $name)
+      $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
+      if ($stream->test(Twig_Token::NAME_TYPE))
       {
-        throw new Twig_SyntaxError(sprintf("Expected endblock for block '$name' (but %s given)", $value), $lineno);
+        $value = $stream->next()->getValue();
+
+        if ($value != $name)
+        {
+          throw new Twig_SyntaxError(sprintf("Expected endblock for block '$name' (but %s given)", $value), $lineno);
+        }
       }
     }
-    $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+    else
+    {
+      $body = new Twig_NodeList(array(
+        new Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno),
+      ));
+    }
+    $stream->expect(Twig_Token::BLOCK_END_TYPE);
 
     $block = new Twig_Node_Block($name, $body, $lineno);
     $this->parser->setBlock($name, $block);
diff --git a/test/fixtures/tags/block/basic.test b/test/fixtures/tags/block/basic.test
new file mode 100644 (file)
index 0000000..360dcf0
--- /dev/null
@@ -0,0 +1,11 @@
+--TEST--
+"block" tag
+--TEMPLATE--
+{% block title1 %}FOO{% endblock %}
+{% block title2 foo|lower %}
+--TEMPLATE(foo.twig)--
+{% block content %}{% endblock %}
+--DATA--
+return array('foo' => 'bar')
+--EXPECT--
+FOObar