protected $seq;
protected $body;
protected $else;
+ protected $withLoop;
- public function __construct($isMultitarget, $item, $seq, Twig_NodeList $body, Twig_Node $else = null, $lineno, $tag = null)
+ public function __construct($isMultitarget, $item, $seq, Twig_NodeList $body, Twig_Node $else = null, $withLoop = false, $lineno, $tag = null)
{
parent::__construct($lineno, $tag);
$this->isMultitarget = $isMultitarget;
$this->seq = $seq;
$this->body = $body;
$this->else = $else;
+ $this->withLoop = $withLoop;
$this->lineno = $lineno;
}
->write("\$seq$var = twig_iterator_to_array(")
->subcompile($this->seq)
->raw(");\n")
- ->write("\$length = count(\$seq$var);\n")
-
- ->write("\$context['loop'] = array(\n")
- ->write(" 'parent' => \$context['_parent'],\n")
- ->write(" 'length' => \$length,\n")
- ->write(" 'index0' => 0,\n")
- ->write(" 'index' => 1,\n")
- ->write(" 'revindex0' => \$length - 1,\n")
- ->write(" 'revindex' => \$length,\n")
- ->write(" 'first' => true,\n")
- ->write(" 'last' => 1 === \$length,\n")
- ->write(");\n")
+ ;
+
+ if ($this->withLoop)
+ {
+ $compiler
+ ->write("\$length = count(\$seq$var);\n")
+
+ ->write("\$context['loop'] = array(\n")
+ ->write(" 'parent' => \$context['_parent'],\n")
+ ->write(" 'length' => \$length,\n")
+ ->write(" 'index0' => 0,\n")
+ ->write(" 'index' => 1,\n")
+ ->write(" 'revindex0' => \$length - 1,\n")
+ ->write(" 'revindex' => \$length,\n")
+ ->write(" 'first' => true,\n")
+ ->write(" 'last' => 1 === \$length,\n")
+ ->write(");\n")
+ ;
+ }
+ $compiler
->write("foreach (\$seq$var as \$context[")
->repr($loopVars[0])
->raw("] => \$context[")
$compiler->write("\$context['_iterated'] = true;\n");
}
- $compiler
- ->subcompile($this->body)
+ $compiler->subcompile($this->body);
- ->write("++\$context['loop']['index0'];\n")
- ->write("++\$context['loop']['index'];\n")
- ->write("--\$context['loop']['revindex0'];\n")
- ->write("--\$context['loop']['revindex'];\n")
- ->write("\$context['loop']['first'] = false;\n")
- ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
+ if ($this->withLoop)
+ {
+ $compiler
+ ->write("++\$context['loop']['index0'];\n")
+ ->write("++\$context['loop']['index'];\n")
+ ->write("--\$context['loop']['revindex0'];\n")
+ ->write("--\$context['loop']['revindex'];\n")
+ ->write("\$context['loop']['first'] = false;\n")
+ ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
+ ;
+ }
+ $compiler
->outdent()
->write("}\n")
;
}
$compiler->popContext();
}
+
+ public function setWithLoop($boolean)
+ {
+ $this->withLoop = (Boolean) $boolean;
+ }
}
list($isMultitarget, $item) = $this->parser->getExpressionParser()->parseAssignmentExpression();
$this->parser->getStream()->expect('in');
$seq = $this->parser->getExpressionParser()->parseExpression();
+
+ $withLoop = true;
+ if ($this->parser->getStream()->test('without'))
+ {
+ $this->parser->getStream()->next();
+ $this->parser->getStream()->expect('loop');
+ $withLoop = false;
+ }
+
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideForFork'));
if ($this->parser->getStream()->next()->getValue() == 'else')
}
$this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
- return new Twig_Node_For($isMultitarget, $item, $seq, $body, $else, $lineno, $this->getTag());
+ return new Twig_Node_For($isMultitarget, $item, $seq, $body, $else, $withLoop, $lineno, $this->getTag());
}
public function decideForFork($token)