added set tag
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Sun, 18 Oct 2009 22:50:16 +0000 (22:50 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Sun, 18 Oct 2009 22:50:16 +0000 (22:50 +0000)
git-svn-id: http://svn.twig-project.org/trunk@73 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/Extension/Core.php
lib/Twig/Node/Set.php
lib/Twig/TokenParser/Set.php

index 0be3e20..eab044e 100644 (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(),
     );
   }
 
index 7816242..b91e476 100644 (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")
     ;
index 01f011f..e31d1e6 100644 (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()