4 * Smarty Internal Plugin Data
6 * This file contains the basic classes and methodes for template and variable creation
9 * @subpackage Templates
14 * Base class with template and variable methodes
16 class Smarty_Internal_Data {
17 // class used for templates
18 public $template_class = 'Smarty_Internal_Template';
21 * assigns a Smarty variable
23 * @param array $ |string $tpl_var the template variable name(s)
24 * @param mixed $value the value to assign
25 * @param boolean $nocache if true any output of this variable will be not cached
26 * @param boolean $scope the scope the variable will have (local,parent or root)
28 public function assign($tpl_var, $value = null, $nocache = false)
30 if (is_array($tpl_var)) {
31 foreach ($tpl_var as $_key => $_val) {
33 $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache);
38 $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache);
43 * assigns a global Smarty variable
45 * @param string $varname the global variable name
46 * @param mixed $value the value to assign
47 * @param boolean $nocache if true any output of this variable will be not cached
49 public function assignGlobal($varname, $value = null, $nocache = false)
52 Smarty::$global_tpl_vars[$varname] = new Smarty_variable($value, $nocache);
56 * assigns values to template variables by reference
58 * @param string $tpl_var the template variable name
59 * @param mixed $ &$value the referenced value to assign
60 * @param boolean $nocache if true any output of this variable will be not cached
62 public function assignByRef($tpl_var, &$value, $nocache = false)
65 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
66 $this->tpl_vars[$tpl_var]->value = &$value;
71 * wrapper function for Smarty 2 BC
73 * @param string $tpl_var the template variable name
74 * @param mixed $ &$value the referenced value to assign
76 public function assign_by_ref($tpl_var, &$value)
78 if($this->smarty->deprecation_notices)
79 trigger_error("function call 'assign_by_ref' is unknown or deprecated, use 'assignByRef'", E_USER_NOTICE);
80 $this->assignByRef($tpl_var, $value);
83 * appends values to template variables
85 * @param array $ |string $tpl_var the template variable name(s)
86 * @param mixed $value the value to append
87 * @param boolean $merge flag if array elements shall be merged
88 * @param boolean $nocache if true any output of this variable will be not cached
90 public function append($tpl_var, $value = null, $merge = false, $nocache = false)
92 if (is_array($tpl_var)) {
93 // $tpl_var is an array, ignore $value
94 foreach ($tpl_var as $_key => $_val) {
96 if (!isset($this->tpl_vars[$_key])) {
97 $tpl_var_inst = $this->getVariable($_key, null, true, false);
98 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
99 $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache);
101 $this->tpl_vars[$_key] = clone $tpl_var_inst;
104 if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
105 settype($this->tpl_vars[$_key]->value, 'array');
107 if ($merge && is_array($_val)) {
108 foreach($_val as $_mkey => $_mval) {
109 $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
112 $this->tpl_vars[$_key]->value[] = $_val;
117 if ($tpl_var != '' && isset($value)) {
118 if (!isset($this->tpl_vars[$tpl_var])) {
119 $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
120 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
121 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
123 $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
126 if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
127 settype($this->tpl_vars[$tpl_var]->value, 'array');
129 if ($merge && is_array($value)) {
130 foreach($value as $_mkey => $_mval) {
131 $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
134 $this->tpl_vars[$tpl_var]->value[] = $value;
141 * appends values to template variables by reference
143 * @param string $tpl_var the template variable name
144 * @param mixed $ &$value the referenced value to append
145 * @param boolean $merge flag if array elements shall be merged
147 public function appendByRef($tpl_var, &$value, $merge = false)
149 if ($tpl_var != '' && isset($value)) {
150 if (!isset($this->tpl_vars[$tpl_var])) {
151 $this->tpl_vars[$tpl_var] = new Smarty_variable();
153 if (!@is_array($this->tpl_vars[$tpl_var]->value)) {
154 settype($this->tpl_vars[$tpl_var]->value, 'array');
156 if ($merge && is_array($value)) {
157 foreach($value as $_key => $_val) {
158 $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
161 $this->tpl_vars[$tpl_var]->value[] = &$value;
168 * @param string $tpl_var the template variable name
169 * @param mixed $ &$value the referenced value to append
170 * @param boolean $merge flag if array elements shall be merged
172 public function append_by_ref($tpl_var, &$value, $merge = false)
174 if($this->smarty->deprecation_notices)
175 trigger_error("function call 'append_by_ref' is unknown or deprecated, use 'appendByRef'", E_USER_NOTICE);
176 $this->appendByRef($tpl_var, $value, $merge);
179 * Returns a single or all template variables
181 * @param string $varname variable name or null
182 * @return string variable value or or array of variables
184 function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
186 if (isset($varname)) {
187 $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
188 if (is_object($_var)) {
195 if ($_ptr === null) {
197 } while ($_ptr !== null) {
198 foreach ($_ptr->tpl_vars AS $key => $var) {
199 if (!array_key_exists($key, $_result)) {
200 $_result[$key] = $var->value;
203 // not found, try at parent
204 if ($search_parents) {
205 $_ptr = $_ptr->parent;
210 if ($search_parents && isset(Smarty::$global_tpl_vars)) {
211 foreach (Smarty::$global_tpl_vars AS $key => $var) {
212 if (!array_key_exists($key, $_result)) {
213 $_result[$key] = $var->value;
222 * clear the given assigned template variable.
224 * @param string $ |array $tpl_var the template variable(s) to clear
226 public function clearAssign($tpl_var)
228 if (is_array($tpl_var)) {
229 foreach ($tpl_var as $curr_var) {
230 unset($this->tpl_vars[$curr_var]);
233 unset($this->tpl_vars[$tpl_var]);
238 * clear all the assigned template variables.
240 public function clearAllAssign()
242 $this->tpl_vars = array();
246 * load a config file, optionally load just selected sections
248 * @param string $config_file filename
249 * @param mixed $sections array of section names, single section or null
251 public function configLoad($config_file, $sections = null)
254 $config = new Smarty_Internal_Config($config_file, $this->smarty, $this);
255 $config->loadConfigVars($sections);
259 * gets the object of a Smarty variable
261 * @param string $variable the name of the Smarty variable
262 * @param object $_ptr optional pointer to data object
263 * @param boolean $search_parents search also in parent data
264 * @return object the object of the variable
266 public function getVariable($_variable, $_ptr = null, $search_parents = true, $error_enable = true)
268 if ($_ptr === null) {
270 } while ($_ptr !== null) {
271 if (isset($_ptr->tpl_vars[$_variable])) {
272 // found it, return it
273 return $_ptr->tpl_vars[$_variable];
275 // not found, try at parent
276 if ($search_parents) {
277 $_ptr = $_ptr->parent;
282 if (isset(Smarty::$global_tpl_vars[$_variable])) {
283 // found it, return it
284 return Smarty::$global_tpl_vars[$_variable];
286 if ($this->smarty->error_unassigned && $error_enable) {
287 throw new SmartyException('Undefined Smarty variable "' . $_variable . '"');
293 return new Undefined_Smarty_Variable;
297 * gets a config variable
299 * @param string $variable the name of the config variable
300 * @return mixed the value of the config variable
302 public function getConfigVariable($_variable)
305 while ($_ptr !== null) {
306 if (isset($_ptr->config_vars[$_variable])) {
307 // found it, return it
308 return $_ptr->config_vars[$_variable];
310 // not found, try at parent
311 $_ptr = $_ptr->parent;
313 if ($this->smarty->error_unassigned) {
314 throw new SmartyException('Undefined config variable "' . $_variable . '"');
323 * gets a stream variable
325 * @param string $variable the stream of the variable
326 * @return mixed the value of the stream variable
328 public function getStreamVariable($variable)
331 if ($fp = fopen($variable, 'r+')) {
333 $_result .= fgets($fp);
339 if ($this->smarty->error_unassigned) {
340 throw new SmartyException('Undefined stream variable "' . $variable . '"');
347 * Returns a single or all config variables
349 * @param string $varname variable name or null
350 * @return string variable value or or array of variables
352 function getConfigVars($varname = null, $search_parents = true)
356 $var_array = array();
357 while ($_ptr !== null) {
358 if (isset($varname)) {
359 if (isset($_ptr->config_vars[$varname])) {
360 return $_ptr->config_vars[$varname];
363 $var_array = array_merge($_ptr->config_vars, $var_array);
365 // not found, try at parent
366 if ($search_parents) {
367 $_ptr = $_ptr->parent;
372 if (isset($varname)) {
380 * Deassigns a single or all config variables
382 * @param string $varname variable name or null
384 function clearConfig($varname = null)
386 if (isset($varname)) {
387 unset($this->config_vars[$varname]);
390 $this->config_vars = array();
398 * class for the Smarty data object
400 * The Smarty data object will hold Smarty variables in the current scope
402 * @param object $parent tpl_vars next higher level of Smarty variables
404 class Smarty_Data extends Smarty_Internal_Data {
405 // array of variable objects
406 public $tpl_vars = array();
407 // back pointer to parent object
408 public $parent = null;
410 public $config_vars = array();
412 public $smarty = null;
414 * create Smarty data object
416 public function __construct ($_parent = null, $smarty = null)
418 $this->smarty = $smarty;
419 if (is_object($_parent)) {
420 // when object set up back pointer
421 $this->parent = $_parent;
422 } elseif (is_array($_parent)) {
423 // set up variable values
424 foreach ($_parent as $_key => $_val) {
425 $this->tpl_vars[$_key] = new Smarty_variable($_val);
427 } elseif ($_parent != null) {
428 throw new SmartyException("Wrong type for template variables");
433 * class for the Smarty variable object
435 * This class defines the Smarty variable object
437 class Smarty_Variable {
443 * create Smarty variable object
445 * @param mixed $value the value to assign
446 * @param boolean $nocache if true any output of this variable will be not cached
447 * @param boolean $scope the scope the variable will have (local,parent or root)
449 public function __construct ($value = null, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
451 $this->value = $value;
452 $this->nocache = $nocache;
453 $this->scope = $scope;
456 public function __toString ()
463 * class for undefined variable object
465 * This class defines an object for undefined variable handling
467 class Undefined_Smarty_Variable {
468 // return always false
469 public function __get ($name)
471 if ($name == 'nocache') {