From 2674ea27552c0c52418d98efe1df60bca0ece0cc Mon Sep 17 00:00:00 2001 From: fabien Date: Sun, 18 Oct 2009 22:50:16 +0000 Subject: [PATCH] added set tag git-svn-id: http://svn.twig-project.org/trunk@73 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- lib/Twig/Extension/Core.php | 1 + lib/Twig/Node/Set.php | 42 +++++++++++++++++++++++++++++++++++------- lib/Twig/TokenParser/Set.php | 5 +++-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 0be3e20..eab044e 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -39,6 +39,7 @@ class Twig_Extension_Core extends Twig_Extension new Twig_TokenParser_Filter(), new Twig_TokenParser_Macro(), new Twig_TokenParser_Import(), + new Twig_TokenParser_Set(), ); } diff --git a/lib/Twig/Node/Set.php b/lib/Twig/Node/Set.php index 7816242..b91e476 100644 --- a/lib/Twig/Node/Set.php +++ b/lib/Twig/Node/Set.php @@ -2,24 +2,52 @@ class Twig_Node_Set extends Twig_Node { - protected $name; + protected $names; protected $value; + protected $isMultitarget; - public function __construct($name, Twig_Node_Expression $value, $lineno) + public function __construct($isMultitarget, $names, Twig_Node_Expression $value, $lineno) { parent::__construct($lineno); - $this->name = $name; + $this->isMultitarget = $isMultitarget; + $this->names = $names; $this->value = $value; } public function compile($compiler) { + $compiler->addDebugInfo($this); + + if ($this->isMultitarget) + { + $compiler->write('list('); + foreach ($this->names as $idx => $node) + { + if ($idx) + { + $compiler->raw(', '); + } + + $compiler + ->raw('$context[') + ->string($node->getName()) + ->raw(']') + ; + } + $compiler->raw(')'); + } + else + { + $compiler + ->write('$context[') + ->string($this->names->getName()) + ->raw(']') + ; + } + $compiler - ->addDebugInfo($this) - ->write('$context[') - ->string($this->name) - ->write('] = ') + ->raw(' = ') ->subcompile($this->value) ->raw(";\n") ; diff --git a/lib/Twig/TokenParser/Set.php b/lib/Twig/TokenParser/Set.php index 01f011f..e31d1e6 100644 --- a/lib/Twig/TokenParser/Set.php +++ b/lib/Twig/TokenParser/Set.php @@ -5,12 +5,13 @@ class Twig_TokenParser_Set extends Twig_TokenParser public function parse(Twig_Token $token) { $lineno = $token->getLine(); - $name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(); + list($isMultitarget, $names) = $this->parser->getExpressionParser()->parseAssignmentExpression(); + $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, 'as'); $value = $this->parser->getExpressionParser()->parseExpression(); $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - return new Twig_Node_Set($name, $value, $lineno, $this->getTag()); + return new Twig_Node_Set($isMultitarget, $names, $value, $lineno, $this->getTag()); } public function getTag() -- 1.7.2.5