Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplateforfunction.php
Go to the documentation of this file.
1 <?php
38 class eZTemplateForFunction
39 {
40 const FUNCTION_NAME = 'for';
41
45 function functionList()
46 {
47 $functionList = array( eZTemplateForFunction::FUNCTION_NAME );
48 return $functionList;
49 }
50
56 function attributeList()
57 {
58 return array( 'delimiter' => true,
59 'break' => false,
60 'continue' => false,
61 'skip' => false );
62 }
63
64
68 function functionTemplateHints()
69 {
70 return array( eZTemplateForFunction::FUNCTION_NAME => array( 'parameters' => true,
71 'static' => false,
72 'transform-parameters' => true,
73 'tree-transformation' => true ) );
74 }
75
79 function templateNodeTransformation( $functionName, &$node,
80 $tpl, $parameters, $privateData )
81 {
82 // {for <first_val> to <last_val> as $<loop_var> [sequence <sequence_array> as $<sequence_var>]}
83
84 $newNodes = array();
85 $tpl->ForCounter++;
86 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
87 $uniqid = md5( $nodePlacement[2] ) . "_" . $tpl->ForCounter;
88
89 $loop = new eZTemplateCompiledLoop( eZTemplateForFunction::FUNCTION_NAME,
90 $newNodes, $parameters, $nodePlacement, $uniqid,
91 $node, $tpl, $privateData );
92
93 $variableStack = "for_variable_stack_$uniqid";
94 $namesArray = array( "for_firstval_$uniqid", "for_lastval_$uniqid", "for_i_$uniqid" );
95
96 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$for_firstval_$uniqid = 0;" );
97 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$for_lastval_$uniqid = 0;" );
98 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$for_i_$uniqid = 0;" );
99
100 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// for begins" );
101 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( !isset( \$$variableStack ) ) \$$variableStack = [];" );
102 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$" . $variableStack ."[] = @compact( '" . implode( "', '", $namesArray ) . "' );" );
103
104 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['first_val'], $nodePlacement, array( 'treat-value-as-non-object' => true ), "for_firstval_$uniqid" );
105 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['last_val'], $nodePlacement, array( 'treat-value-as-non-object' => true ), "for_lastval_$uniqid" );
106
107 $loop->initVars();
108
109 // loop header
110 $modifyLoopCounterCode = "\$for_firstval_$uniqid < \$for_lastval_$uniqid ? \$for_i_{$uniqid}++ : \$for_i_{$uniqid}--"; // . ";\n";
111 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "for ( \$for_i_$uniqid = \$for_firstval_$uniqid ; ; $modifyLoopCounterCode )\n{" );
112 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode();
113 // Check for index
114 $indexArray = isset( $parameters['loop_var'][0][1] ) ? $parameters['loop_var'][0][1] : array( "", 2, "default_index_$uniqid" );
115 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, "for_i_$uniqid", $nodePlacement,
116 array( 'text-result' => true ), $indexArray, false, true, true );
117
118 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( !( \$for_firstval_$uniqid < \$for_lastval_$uniqid ? " .
119 "\$for_i_$uniqid <= \$for_lastval_$uniqid : " .
120 "\$for_i_$uniqid >= \$for_lastval_$uniqid ) )\n" .
121 " break;\n" );
122
123 $loop->processBody();
124
125 // loop footer
126 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode();
127 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // for" );
128
129 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( count( \$$variableStack ) ) extract( array_pop( \$$variableStack ) );\n" );
130 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "else\n{\n" );
131 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( $indexArray );
132 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "for_firstval_$uniqid" );
133 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "for_lastval_$uniqid" );
134 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( "for_i_$uniqid" );
135 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( $variableStack );
136 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "}\n" );
137 $loop->cleanup();
138 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// for ends\n" );
139
140 return $newNodes;
141 }
142
146 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
147 {
148 /*
149 * Check function parameters
150 */
151
152 $loop = new eZTemplateLoop( eZTemplateForFunction::FUNCTION_NAME,
153 $functionParameters, $functionChildren, $functionPlacement,
154 $tpl, $textElements, $rootNamespace, $currentNamespace );
155
156 if ( !$loop->initialized() )
157 return;
158
159 $loop->parseScalarParamValue( 'first_val', $firstVal, $firstValIsProxy );
160 $loop->parseScalarParamValue( 'last_val', $lastVal, $lastValIsProxy );
161
162 if ( $firstValIsProxy || $lastValIsProxy )
163 {
164 $tpl->error( eZTemplateForFunction::FUNCTION_NAME,
165 "Proxy objects ({section} loop iterators) cannot be used to specify the range \n" .
166 "(this will lead to indefinite loops in compiled mode).\n" .
167 "Please explicitly dereference the proxy object like this: \$current_node.item." );
168 return;
169 }
170
171 $loop->parseParamVarName( 'loop_var' , $loopVarName );
172
173 if ( $firstVal === null || $lastVal === null || !$loopVarName )
174 {
175 $tpl->error( eZTemplateForFunction::FUNCTION_NAME, "Wrong arguments passed." );
176 return;
177 }
178
179 if ( !is_numeric( $firstVal ) || !is_numeric( $lastVal ) )
180 {
181 $tpl->error( eZTemplateForFunction::FUNCTION_NAME, "Both 'from' and 'to' values can only be numeric." );
182 return;
183 }
184
185 $loop->initLoopVariable( $loopVarName );
186
187 /*
188 * Everything is ok, run the 'for' loop itself
189 */
190 for ( $i = $firstVal; $firstVal < $lastVal ? $i <= $lastVal : $i >= $lastVal; )
191 {
192 // set loop variable
193 $tpl->setVariable( $loopVarName, $i, $rootNamespace );
194
195 $loop->setSequenceVar(); // set sequence variable (if specified)
196 $loop->processDelimiter();
197 $loop->resetIteration();
198
199 if ( $loop->processChildren() )
200 break;
201
202 // increment loop variable here for delimiter to be processed correctly
203 $firstVal < $lastVal ? $i++ : $i--;
204
205 $loop->incrementSequence();
206 } // for
207
208 $loop->cleanup();
209 }
210
214 function hasChildren()
215 {
216 return true;
217 }
218 }
219
220 ?>
eZTemplateCompiledLoop
Common code for compiling the loop functions.
Definition: eztemplatecompiledloop.php:17
eZTemplateForFunction
FOR loop.
Definition: eztemplateforfunction.php:39
eZTemplateForFunction\FUNCTION_NAME
const FUNCTION_NAME
Definition: eztemplateforfunction.php:40
eZTemplateForFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplateforfunction.php:68
eZTemplateForFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplateforfunction.php:146
eZTemplateForFunction\attributeList
attributeList()
Definition: eztemplateforfunction.php:56
eZTemplateForFunction\functionList
functionList()
Definition: eztemplateforfunction.php:45
eZTemplateForFunction\hasChildren
hasChildren()
Definition: eztemplateforfunction.php:214
eZTemplateForFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, $parameters, $privateData)
Definition: eztemplateforfunction.php:79
eZTemplateLoop
Code common for the loop functions in processed mode.
Definition: eztemplateloop.php:49
eZTemplateNodeTool\createVariableNode
static createVariableNode( $originalNode=false, $variableData=false, $variablePlacement=false, $parameters=array(), $variableAssignmentName=false, $onlyExisting=false, $overWrite=true, $assignFromVariable=false, $rememberSet=false)
Definition: eztemplatenodetool.php:395
eZTemplateNodeTool\createSpacingDecreaseNode
static createSpacingDecreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:508
eZTemplateNodeTool\createVariableUnsetNode
static createVariableUnsetNode( $variableName, $parameters=array())
Definition: eztemplatenodetool.php:462
eZTemplateNodeTool\createCodePieceNode
static createCodePieceNode( $codePiece, $parameters=array())
Definition: eztemplatenodetool.php:457
eZTemplateNodeTool\createSpacingIncreaseNode
static createSpacingIncreaseNode( $spacing=4, $parameters=array())
Definition: eztemplatenodetool.php:503
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.