Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztemplatedeffunction.php
Go to the documentation of this file.
1 <?php
37 class eZTemplateDefFunction
38 {
39 const DEF_FUNCTION_NAME = 'def';
40 const UNDEF_FUNCTION_NAME = 'undef';
41
45 function functionList()
46 {
47 $functionList = array( eZTemplateDefFunction::DEF_FUNCTION_NAME, eZTemplateDefFunction::UNDEF_FUNCTION_NAME );
48 return $functionList;
49 }
50
56 function attributeList()
57 {
58 return array();
59 }
60
61
65 function functionTemplateHints()
66 {
67 return array( eZTemplateDefFunction::DEF_FUNCTION_NAME => array( 'parameters' => true,
68 'static' => false,
69 'transform-parameters' => true,
70 'tree-transformation' => true ),
71 eZTemplateDefFunction::UNDEF_FUNCTION_NAME => array( 'parameters' => true,
72 'static' => false,
73 'transform-parameters' => true,
74 'tree-transformation' => true ) );
75 }
76
80 function templateNodeTransformation( $functionName, &$node,
81 $tpl, &$parameters, $privateData )
82 {
83 $undef = ( $functionName == 'undef' );
84 $newNodes = array();
85
86 if ( !$parameters )
87 {
88 if ( !$undef )
89 // prevent execution of the function in processed mode
90 return array( eZTemplateNodeTool::createCodePieceNode( "// an error occured in $functionName" ) );
91
92 // {undef} called w/o arguments => destroy all local variables
93 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// undef all" );
94 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "\$tpl->unsetLocalVariables();" );
95 return $newNodes;
96 }
97
98 $nodePlacement = eZTemplateNodeTool::extractFunctionNodePlacement( $node );
99 foreach ( array_keys( $parameters ) as $parameterName )
100 {
101 $parameterData = $parameters[$parameterName];
102 if ( $undef )
103 {
104 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// undef \$$parameterName" );
105 // generates "$tpl->unsetLocalVariable();"
106 $newNodes[] = eZTemplateNodeTool::createVariableUnsetNode( array( $namespaceValue = false,
107 $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL,
108 $parameterName ),
109 array( 'remember_set' => false, 'local-variable' => true ) );
110 }
111 else
112 {
113 $newNodes[] = eZTemplateNodeTool::createCodePieceNode( "// def \$$parameterName" );
114 // generates "$tpl->setLocalVariable();"
115 $newNodes[] = eZTemplateNodeTool::createVariableNode( false, $parameterData, $nodePlacement, array( 'local-variable' => true ),
116 array( $namespaceValue = false, $scope = eZTemplate::NAMESPACE_SCOPE_LOCAL, $parameterName ),
117 $onlyExisting = false, $overwrite = true, false, $rememberSet = false );
118 }
119 }
120
121 return $newNodes;
122 }
123
127 function process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
128 {
129 $undef = ( $functionName == eZTemplateDefFunction::UNDEF_FUNCTION_NAME ) ? true : false;
130
131 if ( $undef && !count( $functionParameters ) ) // if {undef} called w/o arguments
132 {
133 // destroy all variables defined in the current template using {def}
134 $tpl->unsetLocalVariables();
135 }
136
137 foreach ( array_keys( $functionParameters ) as $key )
138 {
139 $varName = $key;
140 $param = $functionParameters[$varName];
141 $varValue = $tpl->elementValue( $param, $rootNamespace, $currentNamespace, $functionPlacement );
142
143
144 if ( $undef ) // {undef}
145 {
146 if ( !$tpl->hasLocalVariable( $varName, $rootNamespace ) )
147 $tpl->warning( eZTemplateDefFunction::UNDEF_FUNCTION_NAME, "Variable '$varName' is not defined with {def}.", $functionPlacement );
148 else
149 $tpl->unsetLocalVariable( $varName, $rootNamespace );
150
151 }
152 else // {def}
153 {
154 if ( $tpl->hasVariable( $varName, $rootNamespace ) ) // if the variable already exists
155 {
156 // we don't create new variable but just assign value to the existing one.
157 $tpl->warning( eZTemplateDefFunction::DEF_FUNCTION_NAME, "Variable '$varName' is already defined.", $functionPlacement );
158 $tpl->setVariable( $varName, $varValue, $rootNamespace );
159 }
160 else
161 // create a new local variable and assign a value to it.
162 $tpl->setLocalVariable( $varName, $varValue, $rootNamespace );
163
164 }
165 }
166 }
167
171 function hasChildren()
172 {
173 return false;
174 }
175 }
176
177 ?>
eZTemplateDefFunction
Allows to define/undefine template variables in any place.
Definition: eztemplatedeffunction.php:38
eZTemplateDefFunction\attributeList
attributeList()
Definition: eztemplatedeffunction.php:56
eZTemplateDefFunction\functionTemplateHints
functionTemplateHints()
Definition: eztemplatedeffunction.php:65
eZTemplateDefFunction\hasChildren
hasChildren()
Definition: eztemplatedeffunction.php:171
eZTemplateDefFunction\UNDEF_FUNCTION_NAME
const UNDEF_FUNCTION_NAME
Definition: eztemplatedeffunction.php:40
eZTemplateDefFunction\DEF_FUNCTION_NAME
const DEF_FUNCTION_NAME
Definition: eztemplatedeffunction.php:39
eZTemplateDefFunction\process
process( $tpl, &$textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace)
Definition: eztemplatedeffunction.php:127
eZTemplateDefFunction\functionList
functionList()
Definition: eztemplatedeffunction.php:45
eZTemplateDefFunction\templateNodeTransformation
templateNodeTransformation( $functionName, &$node, $tpl, &$parameters, $privateData)
Definition: eztemplatedeffunction.php:80
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\createVariableUnsetNode
static createVariableUnsetNode( $variableName, $parameters=array())
Definition: eztemplatenodetool.php:462
eZTemplateNodeTool\createCodePieceNode
static createCodePieceNode( $codePiece, $parameters=array())
Definition: eztemplatenodetool.php:457
eZTemplateNodeTool\extractFunctionNodePlacement
static extractFunctionNodePlacement(&$node)
Definition: eztemplatenodetool.php:660
eZTemplate\NAMESPACE_SCOPE_LOCAL
const NAMESPACE_SCOPE_LOCAL
Definition: eztemplate.php:285
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.