mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
made the set tag more powerful
git-svn-id: http://svn.twig-project.org/trunk@103 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
@@ -507,7 +507,7 @@ the `set` tag and can have multiple targets:
|
||||
[twig]
|
||||
{% set foo as 'foo' %}
|
||||
|
||||
{% set key, value as call_something() %}
|
||||
{% set foo, bar as 'foo', 'bar' %}
|
||||
|
||||
### Extends
|
||||
|
||||
|
||||
@@ -152,12 +152,18 @@ Let's create a simple `set` tag that allows the definition of simple variables
|
||||
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:
|
||||
|
||||
@@ -189,6 +195,7 @@ Now, let's see the actual code of the token parser class:
|
||||
{
|
||||
$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);
|
||||
@@ -212,7 +219,8 @@ stream (`$this->parser->getStream()`):
|
||||
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.
|
||||
|
||||
+25
-8
@@ -3,16 +3,16 @@
|
||||
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)
|
||||
@@ -46,10 +46,27 @@ class Twig_Node_Set extends Twig_Node
|
||||
;
|
||||
}
|
||||
|
||||
$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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,11 +7,16 @@ 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');
|
||||
$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()
|
||||
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
--TEST--
|
||||
"set" tag
|
||||
--TEMPLATE--
|
||||
{% set foo as 'foo' %}
|
||||
|
||||
{{ foo }}
|
||||
|
||||
{% set foo, bar as 'foo', 'bar' %}
|
||||
|
||||
{{ foo }}{{ bar }}
|
||||
--DATA--
|
||||
return array()
|
||||
--EXPECT--
|
||||
foo
|
||||
|
||||
|
||||
foobar
|
||||
@@ -35,7 +35,7 @@ class Foo
|
||||
}
|
||||
}
|
||||
|
||||
$t = new LimeTest(48);
|
||||
$t = new LimeTest(49);
|
||||
$fixturesDir = realpath(dirname(__FILE__).'/../fixtures/');
|
||||
|
||||
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($fixturesDir), RecursiveIteratorIterator::LEAVES_ONLY) as $file)
|
||||
|
||||
Reference in New Issue
Block a user