ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Twig_Node_For Class Reference

Represents a for node. More...

+ Inheritance diagram for Twig_Node_For:
+ Collaboration diagram for Twig_Node_For:

Public Member Functions

 __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)
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
- Public Member Functions inherited from Twig_Node
 __construct (array $nodes=array(), array $attributes=array(), $lineno=0, $tag=null)
 Constructor. More...
 
 __toString ()
 
 toXml ($asDom=false)
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
 getTemplateLine ()
 
 getLine ()
 
 getNodeTag ()
 
 hasAttribute ($name)
 
 getAttribute ($name)
 
 setAttribute ($name, $value)
 
 removeAttribute ($name)
 
 hasNode ($name)
 
 getNode ($name)
 
 setNode ($name, $node=null)
 
 removeNode ($name)
 
 count ()
 
 getIterator ()
 
 setTemplateName ($name)
 
 getTemplateName ()
 
 setFilename ($name)
 
 getFilename ()
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
 getLine ()
 
 getNodeTag ()
 

Protected Attributes

 $loop
 
- Protected Attributes inherited from Twig_Node
 $nodes
 
 $attributes
 
 $lineno
 
 $tag
 

Detailed Description

Represents a for node.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 18 of file For.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Node_For::__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 
)

Definition at line 22 of file For.php.

23 {
24 $body = new Twig_Node(array($body, $this->loop = new Twig_Node_ForLoop($lineno, $tag)));
25
26 if (null !== $ifexpr) {
27 $body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $tag);
28 }
29
30 $nodes = array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body);
31 if (null !== $else) {
32 $nodes['else'] = $else;
33 }
34
35 parent::__construct($nodes, array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag);
36 }
Internal node used by the for node.
Definition: ForLoop.php:18
Represents an if node.
Definition: If.php:19
Represents a node in the AST.
Definition: Node.php:19
$lineno
Definition: Node.php:22
$nodes
Definition: Node.php:20

References Twig_Node\$lineno, Twig_Node\$nodes, and Twig_Node\$tag.

Member Function Documentation

◆ compile()

Twig_Node_For::compile ( Twig_Compiler  $compiler)

Compiles the node to PHP.

Reimplemented from Twig_Node.

Definition at line 38 of file For.php.

39 {
40 $compiler
41 ->addDebugInfo($this)
42 ->write("\$context['_parent'] = \$context;\n")
43 ->write("\$context['_seq'] = twig_ensure_traversable(")
44 ->subcompile($this->getNode('seq'))
45 ->raw(");\n")
46 ;
47
48 if ($this->hasNode('else')) {
49 $compiler->write("\$context['_iterated'] = false;\n");
50 }
51
52 if ($this->getAttribute('with_loop')) {
53 $compiler
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")
59 ->write(");\n")
60 ;
61
62 if (!$this->getAttribute('ifexpr')) {
63 $compiler
64 ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
65 ->indent()
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")
71 ->outdent()
72 ->write("}\n")
73 ;
74 }
75 }
76
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'));
80
81 $compiler
82 ->write("foreach (\$context['_seq'] as ")
83 ->subcompile($this->getNode('key_target'))
84 ->raw(' => ')
85 ->subcompile($this->getNode('value_target'))
86 ->raw(") {\n")
87 ->indent()
88 ->subcompile($this->getNode('body'))
89 ->outdent()
90 ->write("}\n")
91 ;
92
93 if ($this->hasNode('else')) {
94 $compiler
95 ->write("if (!\$context['_iterated']) {\n")
96 ->indent()
97 ->subcompile($this->getNode('else'))
98 ->outdent()
99 ->write("}\n")
100 ;
101 }
102
103 $compiler->write("\$_parent = \$context['_parent'];\n");
104
105 // remove some "private" loop variables (needed for nested loops)
106 $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
107
108 // keep the values set in the inner context for variables defined in the outer context
109 $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
110 }
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
Definition: Compiler.php:212
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124
hasNode($name)
Definition: Node.php:178
getAttribute($name)
Definition: Node.php:152
getNode($name)
Definition: Node.php:186
$context
Definition: webdav.php:25

References $context, Twig_Compiler\addDebugInfo(), Twig_Node\getAttribute(), Twig_Node\getNode(), Twig_Node\hasNode(), and Twig_Compiler\write().

+ Here is the call graph for this function:

Field Documentation

◆ $loop

Twig_Node_For::$loop
protected

Definition at line 20 of file For.php.


The documentation for this class was generated from the following file: