From: Fabien Potencier Date: Thu, 22 Dec 2011 21:32:27 +0000 (+0100) Subject: added a do tag (closes #446) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=4aba337cda03b0170cbf30b43cd82cb83146f2b4;p=konrad%2Ftwig.git added a do tag (closes #446) --- diff --git a/CHANGELOG b/CHANGELOG index e5dfc1c..bfbab34 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0 + * added a do tag * added a flush tag * added support for dynamically named filters and functions * added a dump function to help debugging templates diff --git a/doc/tags/flush.rst b/doc/tags/flush.rst index c44d990..55ef593 100644 --- a/doc/tags/flush.rst +++ b/doc/tags/flush.rst @@ -2,7 +2,7 @@ ========= .. versionadded:: 1.5 - The use tag was added in Twig 1.5. + The flush tag was added in Twig 1.5. The ``flush`` tag tells Twig to flush the output buffer: diff --git a/doc/tags/index.rst b/doc/tags/index.rst index 23ab6fe..e44e93d 100644 --- a/doc/tags/index.rst +++ b/doc/tags/index.rst @@ -19,3 +19,4 @@ Tags autoescape raw flush + do diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index f60d705..4c326f5 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -57,6 +57,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_TokenParser_Set(), new Twig_TokenParser_Spaceless(), new Twig_TokenParser_Flush(), + new Twig_TokenParser_Do(), ); } diff --git a/lib/Twig/Node/Do.php b/lib/Twig/Node/Do.php new file mode 100644 index 0000000..aa419d9 --- /dev/null +++ b/lib/Twig/Node/Do.php @@ -0,0 +1,39 @@ + + */ +class Twig_Node_Do extends Twig_Node +{ + public function __construct(Twig_Node_Expression $expr, $lineno, $tag = null) + { + parent::__construct(array('expr' => $expr), array(), $lineno, $tag); + } + + /** + * Compiles the node to PHP. + * + * @param Twig_Compiler A Twig_Compiler instance + */ + public function compile(Twig_Compiler $compiler) + { + $compiler + ->addDebugInfo($this) + ->write('') + ->subcompile($this->getNode('expr')) + ->raw(";\n") + ; + } +} diff --git a/lib/Twig/TokenParser/Do.php b/lib/Twig/TokenParser/Do.php new file mode 100644 index 0000000..17211fb --- /dev/null +++ b/lib/Twig/TokenParser/Do.php @@ -0,0 +1,42 @@ +parser->getExpressionParser()->parseExpression(); + + $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); + + return new Twig_Node_Do($expr, $token->getLine(), $this->getTag()); + } + + /** + * Gets the tag name associated with this token parser. + * + * @param string The tag name + */ + public function getTag() + { + return 'do'; + } +} diff --git a/test/Twig/Tests/Node/DoTest.php b/test/Twig/Tests/Node/DoTest.php new file mode 100644 index 0000000..49c2a59 --- /dev/null +++ b/test/Twig/Tests/Node/DoTest.php @@ -0,0 +1,46 @@ +assertEquals($expr, $node->getNode('expr')); + } + + /** + * @covers Twig_Node_Do::compile + * @dataProvider getTests + */ + public function testCompile($node, $source, $environment = null) + { + parent::testCompile($node, $source, $environment); + } + + public function getTests() + { + $tests = array(); + + $expr = new Twig_Node_Expression_Constant('foo', 0); + $node = new Twig_Node_Do($expr, 0); + $tests[] = array($node, '"foo";'); + + return $tests; + } +}