26 if (null !== $ifexpr) {
30 $nodes = array(
'key_target' => $keyTarget,
'value_target' => $valueTarget,
'seq' => $seq,
'body' => $body);
35 parent::__construct(
$nodes, array(
'with_loop' =>
true,
'ifexpr' => null !== $ifexpr),
$lineno,
$tag);
42 ->write(
"\$context['_parent'] = \$context;\n")
43 ->write(
"\$context['_seq'] = twig_ensure_traversable(")
44 ->subcompile($this->
getNode(
'seq'))
49 $compiler->
write(
"\$context['_iterated'] = false;\n");
54 ->
write(
"\$context['loop'] = array(\n")
55 ->write(
" 'parent' => \$context['_parent'],\n")
56 ->write(
" 'index0' => 0,\n")
57 ->write(
" 'index' => 1,\n")
58 ->write(
" 'first' => true,\n")
64 ->
write(
"if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
66 ->write(
"\$length = count(\$context['_seq']);\n")
67 ->write(
"\$context['loop']['revindex0'] = \$length - 1;\n")
68 ->write(
"\$context['loop']['revindex'] = \$length;\n")
69 ->write(
"\$context['loop']['length'] = \$length;\n")
70 ->write(
"\$context['loop']['last'] = 1 === \$length;\n")
77 $this->loop->setAttribute(
'else', $this->
hasNode(
'else'));
78 $this->loop->setAttribute(
'with_loop', $this->
getAttribute(
'with_loop'));
79 $this->loop->setAttribute(
'ifexpr', $this->
getAttribute(
'ifexpr'));
82 ->
write(
"foreach (\$context['_seq'] as ")
83 ->subcompile($this->
getNode(
'key_target'))
85 ->subcompile($this->
getNode(
'value_target'))
88 ->subcompile($this->
getNode(
'body'))
95 ->
write(
"if (!\$context['_iterated']) {\n")
97 ->subcompile($this->
getNode(
'else'))
103 $compiler->
write(
"\$_parent = \$context['_parent'];\n");
109 $compiler->
write(
"\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
113 class_alias(
'Twig_Node_For',
'Twig\Node\ForNode',
false);
Represents a node in the AST.
Internal node used by the for node.
Represents a node in the AST.
__construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_Node_Expression $ifexpr=null, Twig_NodeInterface $body, Twig_NodeInterface $else=null, $lineno, $tag=null)
Abstract class for all nodes that represents an expression.
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
write()
Writes a string to the compiled code by adding indentation.