[twig]
{% set foo as 'foo' %}
- {% set key, value as call_something() %}
+ {% set foo, bar as 'foo', 'bar' %}
### Extends
from within a template. The tag can be used like follows:
[twig]
- {% set name "value" %}
+ {% set name as "value" %}
{{ name }}
{# should output value #}
+>**NOTE**
+>The `set` tag is part of the Core extension and as such is always available.
+>The built-in version is slightly more powerful and supports multiple
+>assignments by defaults (cf. template designers chapter for more
+>information).
+
First, we need to create a `Twig_TokenParser` class which will be able to
parse this new language construct:
{
$lineno = $token->getLine();
$name = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue();
+ $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, 'as');
$value = $this->parser->getExpressionParser()->parseExpression();
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
returns it.
* `expect()`: Expects a token and returns it (like `test()`) or throw a
- syntax error if not found.
+ syntax error if not found (the second argument is the expected value of the
+ token).
* `look()`: Looks a the next token. This is how you can have a look at the
next token without consume it.
class Twig_Node_Set extends Twig_Node
{
protected $names;
- protected $value;
+ protected $values;
protected $isMultitarget;
- public function __construct($isMultitarget, $names, Twig_Node_Expression $value, $lineno, $tag = null)
+ public function __construct($isMultitarget, $names, $values, $lineno, $tag = null)
{
parent::__construct($lineno, $tag);
$this->isMultitarget = $isMultitarget;
$this->names = $names;
- $this->value = $value;
+ $this->values = $values;
}
public function compile($compiler)
;
}
- $compiler
- ->raw(' = ')
- ->subcompile($this->value)
- ->raw(";\n")
- ;
+ $compiler->raw(' = ');
+
+ if ($this->isMultitarget)
+ {
+ $compiler->write('array(');
+ foreach ($this->values as $idx => $value)
+ {
+ if ($idx)
+ {
+ $compiler->raw(', ');
+ }
+
+ $compiler->subcompile($value);
+ }
+ $compiler->raw(')');
+ }
+ else
+ {
+ $compiler->subcompile($this->values);
+ }
+
+ $compiler->raw(";\n");
}
}
$lineno = $token->getLine();
list($isMultitarget, $names) = $this->parser->getExpressionParser()->parseAssignmentExpression();
$this->parser->getStream()->expect(Twig_Token::NAME_TYPE, 'as');
- $value = $this->parser->getExpressionParser()->parseExpression();
+ list(, $values) = $this->parser->getExpressionParser()->parseAssignmentExpression();
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
- return new Twig_Node_Set($isMultitarget, $names, $value, $lineno, $this->getTag());
+ if (count($names) !== count($values))
+ {
+ throw new Twig_SyntaxError("When using set, you must have the same number of variables and assignements.", $lineno);
+ }
+
+ return new Twig_Node_Set($isMultitarget, $names, $values, $lineno, $this->getTag());
}
public function getTag()
--- /dev/null
+--TEST--
+"set" tag
+--TEMPLATE--
+{% set foo as 'foo' %}
+
+{{ foo }}
+
+{% set foo, bar as 'foo', 'bar' %}
+
+{{ foo }}{{ bar }}
+--DATA--
+return array()
+--EXPECT--
+foo
+
+
+foobar
}
}
-$t = new LimeTest(48);
+$t = new LimeTest(49);
$fixturesDir = realpath(dirname(__FILE__).'/../fixtures/');
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)