3 * Smarty Internal Plugin Data
5 * This file contains the basic classes and methodes for template and variable creation
13 * Base class with template and variable methodes
16 * @subpackage Template
18 class Smarty_Internal_Data {
21 * name of class used for templates
25 public $template_class = 'Smarty_Internal_Template';
31 public $tpl_vars = array();
33 * parent template (if any)
35 * @var Smarty_Internal_Template
37 public $parent = null;
39 * configuration settings
43 public $config_vars = array();
46 * assigns a Smarty variable
48 * @param array|string $tpl_var the template variable name(s)
49 * @param mixed $value the value to assign
50 * @param boolean $nocache if true any output of this variable will be not cached
51 * @param boolean $scope the scope the variable will have (local,parent or root)
52 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
54 public function assign($tpl_var, $value = null, $nocache = false)
56 if (is_array($tpl_var)) {
57 foreach ($tpl_var as $_key => $_val) {
59 $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache);
64 $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache);
72 * assigns a global Smarty variable
74 * @param string $varname the global variable name
75 * @param mixed $value the value to assign
76 * @param boolean $nocache if true any output of this variable will be not cached
77 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
79 public function assignGlobal($varname, $value = null, $nocache = false)
82 Smarty::$global_tpl_vars[$varname] = new Smarty_variable($value, $nocache);
88 * assigns values to template variables by reference
90 * @param string $tpl_var the template variable name
91 * @param mixed $ &$value the referenced value to assign
92 * @param boolean $nocache if true any output of this variable will be not cached
93 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
95 public function assignByRef($tpl_var, &$value, $nocache = false)
98 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
99 $this->tpl_vars[$tpl_var]->value = &$value;
106 * appends values to template variables
108 * @param array|string $tpl_var the template variable name(s)
109 * @param mixed $value the value to append
110 * @param boolean $merge flag if array elements shall be merged
111 * @param boolean $nocache if true any output of this variable will be not cached
112 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
114 public function append($tpl_var, $value = null, $merge = false, $nocache = false)
116 if (is_array($tpl_var)) {
117 // $tpl_var is an array, ignore $value
118 foreach ($tpl_var as $_key => $_val) {
120 if (!isset($this->tpl_vars[$_key])) {
121 $tpl_var_inst = $this->getVariable($_key, null, true, false);
122 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
123 $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache);
125 $this->tpl_vars[$_key] = clone $tpl_var_inst;
128 if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
129 settype($this->tpl_vars[$_key]->value, 'array');
131 if ($merge && is_array($_val)) {
132 foreach($_val as $_mkey => $_mval) {
133 $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
136 $this->tpl_vars[$_key]->value[] = $_val;
141 if ($tpl_var != '' && isset($value)) {
142 if (!isset($this->tpl_vars[$tpl_var])) {
143 $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
144 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
145 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
147 $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
150 if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
151 settype($this->tpl_vars[$tpl_var]->value, 'array');
153 if ($merge && is_array($value)) {
154 foreach($value as $_mkey => $_mval) {
155 $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
158 $this->tpl_vars[$tpl_var]->value[] = $value;
167 * appends values to template variables by reference
169 * @param string $tpl_var the template variable name
170 * @param mixed &$value the referenced value to append
171 * @param boolean $merge flag if array elements shall be merged
172 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
174 public function appendByRef($tpl_var, &$value, $merge = false)
176 if ($tpl_var != '' && isset($value)) {
177 if (!isset($this->tpl_vars[$tpl_var])) {
178 $this->tpl_vars[$tpl_var] = new Smarty_variable();
180 if (!is_array($this->tpl_vars[$tpl_var]->value)) {
181 settype($this->tpl_vars[$tpl_var]->value, 'array');
183 if ($merge && is_array($value)) {
184 foreach($value as $_key => $_val) {
185 $this->tpl_vars[$tpl_var]->value[$_key] = &$value[$_key];
188 $this->tpl_vars[$tpl_var]->value[] = &$value;
196 * Returns a single or all template variables
198 * @param string $varname variable name or null
199 * @param string $_ptr optional pointer to data object
200 * @param boolean $search_parents include parent templates?
201 * @return string variable value or or array of variables
203 public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
205 if (isset($varname)) {
206 $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
207 if (is_object($_var)) {
214 if ($_ptr === null) {
216 } while ($_ptr !== null) {
217 foreach ($_ptr->tpl_vars AS $key => $var) {
218 if (!array_key_exists($key, $_result)) {
219 $_result[$key] = $var->value;
222 // not found, try at parent
223 if ($search_parents) {
224 $_ptr = $_ptr->parent;
229 if ($search_parents && isset(Smarty::$global_tpl_vars)) {
230 foreach (Smarty::$global_tpl_vars AS $key => $var) {
231 if (!array_key_exists($key, $_result)) {
232 $_result[$key] = $var->value;
241 * clear the given assigned template variable.
243 * @param string|array $tpl_var the template variable(s) to clear
244 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
246 public function clearAssign($tpl_var)
248 if (is_array($tpl_var)) {
249 foreach ($tpl_var as $curr_var) {
250 unset($this->tpl_vars[$curr_var]);
253 unset($this->tpl_vars[$tpl_var]);
260 * clear all the assigned template variables.
261 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
263 public function clearAllAssign()
265 $this->tpl_vars = array();
270 * load a config file, optionally load just selected sections
272 * @param string $config_file filename
273 * @param mixed $sections array of section names, single section or null
274 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
276 public function configLoad($config_file, $sections = null)
279 $config = new Smarty_Internal_Config($config_file, $this->smarty, $this);
280 $config->loadConfigVars($sections);
285 * gets the object of a Smarty variable
287 * @param string $variable the name of the Smarty variable
288 * @param object $_ptr optional pointer to data object
289 * @param boolean $search_parents search also in parent data
290 * @return object the object of the variable
292 public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
294 if ($_ptr === null) {
296 } while ($_ptr !== null) {
297 if (isset($_ptr->tpl_vars[$variable])) {
298 // found it, return it
299 return $_ptr->tpl_vars[$variable];
301 // not found, try at parent
302 if ($search_parents) {
303 $_ptr = $_ptr->parent;
308 if (isset(Smarty::$global_tpl_vars[$variable])) {
309 // found it, return it
310 return Smarty::$global_tpl_vars[$variable];
312 if ($this->smarty->error_unassigned && $error_enable) {
316 return new Undefined_Smarty_Variable;
320 * gets a config variable
322 * @param string $variable the name of the config variable
323 * @return mixed the value of the config variable
325 public function getConfigVariable($variable, $error_enable = true)
328 while ($_ptr !== null) {
329 if (isset($_ptr->config_vars[$variable])) {
330 // found it, return it
331 return $_ptr->config_vars[$variable];
333 // not found, try at parent
334 $_ptr = $_ptr->parent;
336 if ($this->smarty->error_unassigned && $error_enable) {
344 * gets a stream variable
346 * @param string $variable the stream of the variable
347 * @return mixed the value of the stream variable
349 public function getStreamVariable($variable)
352 $fp = fopen($variable, 'r+');
354 while (!feof($fp) && ($current_line = fgets($fp)) !== false ) {
355 $_result .= $current_line;
361 if ($this->smarty->error_unassigned) {
362 throw new SmartyException('Undefined stream variable "' . $variable . '"');
369 * Returns a single or all config variables
371 * @param string $varname variable name or null
372 * @return string variable value or or array of variables
374 public function getConfigVars($varname = null, $search_parents = true)
377 $var_array = array();
378 while ($_ptr !== null) {
379 if (isset($varname)) {
380 if (isset($_ptr->config_vars[$varname])) {
381 return $_ptr->config_vars[$varname];
384 $var_array = array_merge($_ptr->config_vars, $var_array);
386 // not found, try at parent
387 if ($search_parents) {
388 $_ptr = $_ptr->parent;
393 if (isset($varname)) {
401 * Deassigns a single or all config variables
403 * @param string $varname variable name or null
404 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
406 public function clearConfig($varname = null)
408 if (isset($varname)) {
409 unset($this->config_vars[$varname]);
411 $this->config_vars = array();
419 * class for the Smarty data object
421 * The Smarty data object will hold Smarty variables in the current scope
424 * @subpackage Template
426 class Smarty_Data extends Smarty_Internal_Data {
433 public $smarty = null;
436 * create Smarty data object
438 * @param Smarty|array $_parent parent template
439 * @param Smarty $smarty global smarty instance
441 public function __construct ($_parent = null, $smarty = null)
443 $this->smarty = $smarty;
444 if (is_object($_parent)) {
445 // when object set up back pointer
446 $this->parent = $_parent;
447 } elseif (is_array($_parent)) {
448 // set up variable values
449 foreach ($_parent as $_key => $_val) {
450 $this->tpl_vars[$_key] = new Smarty_variable($_val);
452 } elseif ($_parent != null) {
453 throw new SmartyException("Wrong type for template variables");
460 * class for the Smarty variable object
462 * This class defines the Smarty variable object
465 * @subpackage Template
467 class Smarty_Variable {
474 public $value = null;
476 * if true any output of this variable will be not cached
480 public $nocache = false;
482 * the scope the variable will have (local,parent or root)
486 public $scope = Smarty::SCOPE_LOCAL;
489 * create Smarty variable object
491 * @param mixed $value the value to assign
492 * @param boolean $nocache if true any output of this variable will be not cached
493 * @param int $scope the scope the variable will have (local,parent or root)
495 public function __construct($value = null, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
497 $this->value = $value;
498 $this->nocache = $nocache;
499 $this->scope = $scope;
503 * <<magic>> String conversion
507 public function __toString()
509 return (string) $this->value;
515 * class for undefined variable object
517 * This class defines an object for undefined variable handling
520 * @subpackage Template
522 class Undefined_Smarty_Variable {
525 * Returns FALSE for 'nocache' and NULL otherwise.
527 * @param string $name
530 public function __get($name)
532 if ($name == 'nocache') {
540 * Always returns an empty string.
544 public function __toString()