From 7a4f63da2c978fe277a1bf7baa3bf0a6a8ace9e6 Mon Sep 17 00:00:00 2001 From: fabien Date: Sun, 18 Oct 2009 22:53:21 +0000 Subject: [PATCH] added support for short block tag git-svn-id: http://svn.twig-project.org/trunk@76 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- lib/Twig/TokenParser/Block.php | 28 ++++++++++++++++++++-------- test/fixtures/tags/block/basic.test | 11 +++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/tags/block/basic.test diff --git a/lib/Twig/TokenParser/Block.php b/lib/Twig/TokenParser/Block.php index a6df2a5..077347f 100644 --- a/lib/Twig/TokenParser/Block.php +++ b/lib/Twig/TokenParser/Block.php @@ -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 index 0000000..360dcf0 --- /dev/null +++ b/test/fixtures/tags/block/basic.test @@ -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 -- 1.7.2.5