* 0.9.6-DEV
+ * added a long-syntax for the set tag ({% set foo %}...{% endset %})
* unit tests are now powered by PHPUnit
* added support for gettext via the `i18n` extension
* fixed twig_capitalize_string_filter() and fixed twig_length_filter() when used with UTF-8 values
{% set foo, bar as 'foo', 'bar' %}
+The `set` tag can also be used to 'capture' chunks of HTML:
+
+ [twig]
+ {% set foo %}
+ <div id="pagination">
+ ...
+ </div>
+ {% endset %}
+
### Extends
The `extends` tag can be used to extend a template from another one. You can
In the inner loop, the `loop.parent` variable is used to access the outer
context. So, the index of the current `topic` defined in the outer for loop is
accessible via the `loop.parent.loop.index` variable.
+
+Passing a Macro as an Argument
+------------------------------
+
+By default, a macro directly outputs its content to the screen. If you want to
+pass the content of a macro as an argument to a method or to another macro,
+you can use the `set` tag:
+
+ [twig]
+ {% import "form_elements.html" as form %}
+
+ {% set theinput %}
+ {{ form.input('test', 'text', 'Value') }}
+ {% endset %}
+
+ {{ form.row('Label', theinput) }}
<?php
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2010 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Represents a set node.
+ *
+ * @package twig
+ * @author Fabien Potencier <fabien.potencier@symfony-project.com>
+ * @version SVN: $Id$
+ */
class Twig_Node_Set extends Twig_Node implements Twig_NodeListInterface
{
protected $names;
protected $values;
protected $isMultitarget;
+ protected $capture;
- public function __construct($isMultitarget, $names, $values, $lineno, $tag = null)
+ public function __construct($isMultitarget, $capture, $names, $values, $lineno, $tag = null)
{
parent::__construct($lineno, $tag);
$this->isMultitarget = $isMultitarget;
+ $this->capture = $capture;
$this->names = $names;
$this->values = $values;
}
}
else
{
+ if ($this->capture)
+ {
+ $compiler
+ ->write("ob_start();\n")
+ ->subcompile($this->values)
+ ;
+ }
+
$compiler
->write('$context[')
->string($this->names->getName())
->raw(']')
;
- }
- $compiler->raw(' = ');
+ if ($this->capture)
+ {
+ $compiler->raw(" = ob_get_clean()");
+ }
+ }
- if ($this->isMultitarget)
+ if (!$this->capture)
{
- $compiler->write('array(');
- foreach ($this->values as $idx => $value)
+ $compiler->raw(' = ');
+
+ if ($this->isMultitarget)
{
- if ($idx)
+ $compiler->write('array(');
+ foreach ($this->values as $idx => $value)
{
- $compiler->raw(', ');
- }
+ if ($idx)
+ {
+ $compiler->raw(', ');
+ }
- $compiler->subcompile($value);
+ $compiler->subcompile($value);
+ }
+ $compiler->raw(')');
+ }
+ else
+ {
+ $compiler->subcompile($this->values);
}
- $compiler->raw(')');
- }
- else
- {
- $compiler->subcompile($this->values);
}
$compiler->raw(";\n");
public function parse(Twig_Token $token)
{
$lineno = $token->getLine();
+ $stream = $this->parser->getStream();
list($isMultitarget, $names) = $this->parser->getExpressionParser()->parseAssignmentExpression();
- $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, 'as');
- list(, $values) = $this->parser->getExpressionParser()->parseMultitargetExpression();
- $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
+ $capture = false;
+ if ($stream->test(Twig_Token::NAME_TYPE, 'as'))
+ {
+ $stream->expect(Twig_Token::NAME_TYPE, 'as');
+ list(, $values) = $this->parser->getExpressionParser()->parseMultitargetExpression();
+
+ $stream->expect(Twig_Token::BLOCK_END_TYPE);
+ }
+ else
+ {
+ $capture = true;
+
+ if ($isMultitarget)
+ {
+ throw new Twig_SyntaxError("When using set with a block, you cannot have a multi-target.", $lineno);
+ }
+
+ $stream->expect(Twig_Token::BLOCK_END_TYPE);
+
+ $values = $this->parser->subparse(array($this, 'decideBlockEnd'), true);
+ $stream->expect(Twig_Token::BLOCK_END_TYPE);
+ }
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());
+ return new Twig_Node_Set($isMultitarget, $capture, $names, $values, $lineno, $this->getTag());
+ }
+
+ public function decideBlockEnd($token)
+ {
+ return $token->test('endset');
}
public function getTag()