|
eZ Publish
[trunk]
|
00001 <?php 00002 /** 00003 * File containing the eZTemplateWhileFunction class. 00004 * 00005 * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. 00006 * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 00007 * @version //autogentag// 00008 * @package lib 00009 */ 00010 00011 /*! 00012 \class eZTemplateWhileFunction eztemplatewhilefunction.php 00013 \ingroup eZTemplateFunctions 00014 \brief WHILE loop 00015 00016 Syntax: 00017 \code 00018 {while <condition> [sequence <array> as $seqVar] } 00019 [{delimiter}...{/delimiter}] 00020 [{break}] 00021 [{continue}] 00022 [{skip}] 00023 {/while} 00024 \endcode 00025 00026 Example: 00027 \code 00028 {while ne( $var, false() ) } 00029 I like big trucks 00030 {/while} 00031 \endcode 00032 */ 00033 00034 class eZTemplateWhileFunction 00035 { 00036 const FUNCTION_NAME = 'while'; 00037 00038 /*! 00039 * Returns an array of the function names, required for eZTemplate::registerFunctions. 00040 */ 00041 function functionList() 00042 { 00043 $functionList = array( eZTemplateWhileFunction::FUNCTION_NAME ); 00044 return $functionList; 00045 } 00046 00047 /*! 00048 * Returns the attribute list. 00049 * key: parameter name 00050 * value: can have children 00051 */ 00052 function attributeList() 00053 { 00054 return array( 'delimiter' => true, 00055 'break' => false, 00056 'continue' => false, 00057 'skip' => false ); 00058 } 00059 00060 00061 /*! 00062 * Returns the array with hits for the template compiler. 00063 */ 00064 function functionTemplateHints() 00065 { 00066 return array( eZTemplateWhileFunction::FUNCTION_NAME => array( 'parameters' => true, 00067 'static' => false, 00068 'transform-parameters' => true, 00069 'tree-transformation' => true ) ); 00070 } 00071 00072 /*! 00073 * Compiles the function and its children into PHP code. 00074 */ 00075 function templateNodeTransformation( $functionName, &$node, 00076 $tpl, &$parameters, $privateData ) 00077 { 00078 // {while <condition> [sequence <sequence_array> as $<sequence_var>]} 00079 00080 $tpl->WhileCounter++; 00081 $newNodes = array(); 00082 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node ); 00083 $uniqid = md5( $nodePlacement[2] ) . "_" . $tpl->WhileCounter; 00084 00085 $loop = new eZTemplateCompiledLoop( eZTemplateWhileFunction::FUNCTION_NAME, 00086 $newNodes, $parameters, $nodePlacement, $uniqid, 00087 $node, $tpl, $privateData ); 00088 00089 00090 $loop->initVars(); 00091 00092 // loop header 00093 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "while ( 1 ) // while\n{" ); 00094 $newNodes[] = eZTemplateNodeTool::createSpacingIncreaseNode(); 00095 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameters['condition'], $nodePlacement, array( 'treat-value-as-non-object' => true ), 00096 "while_cond" ); 00097 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "if ( ! \$while_cond ) break;\n" ); 00098 00099 $loop->processBody(); 00100 00101 // loop footer 00102 $newNodes[] = eZTemplateNodeTool::createSpacingDecreaseNode(); 00103 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "} // end of while\n" ); 00104 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( 'while_cond' ); 00105 $loop->cleanup(); 00106 00107 return $newNodes; 00108 } 00109 00110 /*! 00111 * Actually executes the function and its children (in processed mode). 00112 */ 00113 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace ) 00114 { 00115 if ( count( $functionParameters ) == 0 ) 00116 { 00117 eZDebug::writeError( "Not enough arguments passed to 'while' function." ); 00118 return; 00119 } 00120 00121 $loop = new eZTemplateLoop( eZTemplateWhileFunction::FUNCTION_NAME, 00122 $functionParameters, $functionChildren, $functionPlacement, 00123 $tpl, $textElements, $rootNamespace, $currentNamespace ); 00124 00125 if ( !$loop->initialized() ) 00126 return; 00127 00128 while ( $tpl->elementValue( $functionParameters['condition'], $rootNamespace, $currentNamespace, $functionPlacement ) ) 00129 { 00130 $loop->setSequenceVar(); // set sequence variable (if specified) 00131 $loop->processDelimiter(); 00132 $loop->resetIteration(); 00133 00134 if ( $loop->processChildren() ) 00135 break; 00136 00137 $loop->incrementSequence(); 00138 } 00139 00140 $loop->cleanup(); 00141 } 00142 00143 /*! 00144 * Returns true, telling the template parser that the function can have children. 00145 */ 00146 function hasChildren() 00147 { 00148 return true; 00149 } 00150 } 00151 00152 ?>