added set tag

git-svn-id: http://svn.twig-project.org/trunk@73 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
fabien
2009-10-18 22:50:16 +00:00
parent f07bd4f150
commit 2674ea2755
3 changed files with 39 additions and 9 deletions
+1
View File
@@ -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(),
);
}
+35 -7
View File
@@ -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")
;
+3 -2
View File
@@ -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()