From 4aba337cda03b0170cbf30b43cd82cb83146f2b4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 22 Dec 2011 22:32:27 +0100 Subject: [PATCH] added a do tag (closes #446) --- CHANGELOG | 1 + doc/tags/flush.rst | 2 +- doc/tags/index.rst | 1 + lib/Twig/Extension/Core.php | 1 + lib/Twig/Node/Do.php | 39 +++++++++++++++++++++++++++++++++ lib/Twig/TokenParser/Do.php | 42 +++++++++++++++++++++++++++++++++++ test/Twig/Tests/Node/DoTest.php | 46 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 131 insertions(+), 1 deletions(-) create mode 100644 lib/Twig/Node/Do.php create mode 100644 lib/Twig/TokenParser/Do.php create mode 100644 test/Twig/Tests/Node/DoTest.php 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; + } +} -- 1.7.2.5