From: fabien Date: Thu, 5 Nov 2009 17:18:08 +0000 (+0000) Subject: added support for expression for the set tag X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=c7e8e88cc973e3b9a13bab9d9164a1b2cf1de2cd;p=konrad%2Ftwig.git added support for expression for the set tag git-svn-id: http://svn.twig-project.org/trunk@112 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index d9de144..6ae9b3c 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -507,6 +507,8 @@ the `set` tag and can have multiple targets: [twig] {% set foo as 'foo' %} + {% set foo as 'foo' ~ 'bar' %} + {% set foo, bar as 'foo', 'bar' %} ### Extends diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 5ccf16d..93ba541 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -420,4 +420,36 @@ class Twig_ExpressionParser return array(true, $targets); } + + public function parseMultitargetExpression() + { + $lineno = $this->parser->getCurrentToken()->getLine(); + $targets = array(); + $is_multitarget = false; + while (true) + { + if (!empty($targets)) + { + $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, ','); + } + if ($this->parser->getStream()->test(Twig_Token::OPERATOR_TYPE, ')') || + $this->parser->getStream()->test(Twig_Token::VAR_END_TYPE) || + $this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE)) + { + break; + } + $targets[] = $this->parseExpression(); + if (!$this->parser->getStream()->test(Twig_Token::OPERATOR_TYPE, ',')) + { + break; + } + $is_multitarget = true; + } + if (!$is_multitarget && count($targets) == 1) + { + return array(false, $targets[0]); + } + + return array(true, $targets); + } } diff --git a/lib/Twig/TokenParser/Set.php b/lib/Twig/TokenParser/Set.php index 8ab170e..6ce2ae9 100644 --- a/lib/Twig/TokenParser/Set.php +++ b/lib/Twig/TokenParser/Set.php @@ -7,7 +7,7 @@ class Twig_TokenParser_Set extends Twig_TokenParser $lineno = $token->getLine(); list($isMultitarget, $names) = $this->parser->getExpressionParser()->parseAssignmentExpression(); $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, 'as'); - list(, $values) = $this->parser->getExpressionParser()->parseAssignmentExpression(); + list(, $values) = $this->parser->getExpressionParser()->parseMultitargetExpression(); $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); diff --git a/test/fixtures/tags/set/expression.test b/test/fixtures/tags/set/expression.test new file mode 100644 index 0000000..462d33c --- /dev/null +++ b/test/fixtures/tags/set/expression.test @@ -0,0 +1,12 @@ +--TEST-- +"set" tag +--TEMPLATE-- +{% set foo, bar as 'foo' ~ 'bar', 'bar' ~ 'foo' %} + +{{ foo }} +{{ bar }} +--DATA-- +return array() +--EXPECT-- +foobar +barfoo diff --git a/test/unit/integrationTest.php b/test/unit/integrationTest.php index e34bf4d..688b6d8 100644 --- a/test/unit/integrationTest.php +++ b/test/unit/integrationTest.php @@ -35,7 +35,7 @@ class Foo } } -$t = new LimeTest(49); +$t = new LimeTest(50); $fixturesDir = realpath(dirname(__FILE__).'/../fixtures/'); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)