*/
class Twig_Node_For extends Twig_Node
{
- public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $withLoop = false, $lineno, $tag = null)
+ public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $withLoop = false, Twig_Node_Expression $joinedBy = null, $lineno, $tag = null)
{
- parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => $withLoop), $lineno, $tag);
+ parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else, 'joined_with' => $joinedBy), array('with_loop' => $withLoop), $lineno, $tag);
}
/**
;
}
+ if (null !== $this->getNode('joined_with')) {
+ $compiler->write("\$context['_first_iteration'] = true;\n");
+ }
+
$compiler
->write("foreach (\$context['_seq'] as ")
->subcompile($this->getNode('key_target'))
$compiler->write("\$context['_iterated'] = true;\n");
}
+ if (null !== $this->getNode('joined_with')) {
+ $compiler
+ ->write("if (\$context['_first_iteration']) {\n")
+ ->indent()
+ ->write("\$context['_first_iteration'] = false;\n")
+ ->outdent()
+ ->write("} else {\n")
+ ->indent()
+ ->write("echo ")
+ ->subcompile($this->getNode('joined_with'))
+ ->raw(";\n")
+ ->outdent()
+ ->write("}\n");
+ }
+
$compiler->subcompile($this->getNode('body'));
if ($this->getAttribute('with_loop')) {
$withLoop = false;
}
+ $joinedBy = null;
+ if ($this->parser->getStream()->test('joined')) {
+ $this->parser->getStream()->next();
+ $this->parser->getStream()->expect('by');
+ $joinedBy = $this->parser->getExpressionParser()->parseExpression();
+ }
+
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideForFork'));
if ($this->parser->getStream()->next()->getValue() == 'else') {
$valueTarget = $targets->getNode(0);
}
- return new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, $lineno, $this->getTag());
+ return new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, $joinedBy, $lineno, $this->getTag());
}
public function decideForFork($token)
$body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
$else = null;
$withLoop = false;
- $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
+ $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, null, 0);
$this->assertEquals($keyTarget, $node->getNode('key_target'));
$this->assertEquals($valueTarget, $node->getNode('value_target'));
$this->assertEquals($withLoop, $node->getAttribute('with_loop'));
$else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
- $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
+ $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, null, 0);
$this->assertEquals($else, $node->getNode('else'));
}
$body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
$else = null;
$withLoop = false;
- $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
+ $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, null, 0);
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
$body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
$else = null;
$withLoop = true;
- $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
+ $joinedBy = new Twig_Node_Expression_Constant(', ', 0);
+ $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, $joinedBy, 0);
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
\$context['loop']['length'] = \$length;
\$context['loop']['last'] = 1 === \$length;
}
+\$context['_first_iteration'] = true;
foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
+ if (\$context['_first_iteration']) {
+ \$context['_first_iteration'] = false;
+ } else {
+ echo ", ";
+ }
echo (isset(\$context['foo']) ? \$context['foo'] : null);
++\$context['loop']['index0'];
++\$context['loop']['index'];
$body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
$else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0);
$withLoop = true;
- $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0);
+ $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, null, 0);
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;