3 * Smarty Internal Plugin Data
4 * This file contains the basic classes and methods for template and variable creation
12 * Base class with template and variable methods
15 * @subpackage Template
17 class Smarty_Internal_Data
20 * name of class used for templates
24 public $template_class = 'Smarty_Internal_Template';
30 public $tpl_vars = array();
32 * parent template (if any)
34 * @var Smarty_Internal_Template
36 public $parent = null;
38 * configuration settings
42 public $config_vars = array();
45 * assigns a Smarty variable
47 * @param array|string $tpl_var the template variable name(s)
48 * @param mixed $value the value to assign
49 * @param boolean $nocache if true any output of this variable will be not cached
51 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
53 public function assign($tpl_var, $value = null, $nocache = false)
55 if (is_array($tpl_var)) {
56 foreach ($tpl_var as $_key => $_val) {
58 $this->tpl_vars[$_key] = new Smarty_variable($_val, $nocache);
63 $this->tpl_vars[$tpl_var] = new Smarty_variable($value, $nocache);
71 * assigns a global Smarty variable
73 * @param string $varname the global variable name
74 * @param mixed $value the value to assign
75 * @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);
84 while ($ptr instanceof Smarty_Internal_Template) {
85 $ptr->tpl_vars[$varname] = clone Smarty::$global_tpl_vars[$varname];
94 * assigns values to template variables by reference
96 * @param string $tpl_var the template variable name
98 * @param boolean $nocache if true any output of this variable will be not cached
100 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
102 public function assignByRef($tpl_var, &$value, $nocache = false)
104 if ($tpl_var != '') {
105 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
106 $this->tpl_vars[$tpl_var]->value = & $value;
113 * appends values to template variables
115 * @param array|string $tpl_var the template variable name(s)
116 * @param mixed $value the value to append
117 * @param boolean $merge flag if array elements shall be merged
118 * @param boolean $nocache if true any output of this variable will be not cached
120 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
122 public function append($tpl_var, $value = null, $merge = false, $nocache = false)
124 if (is_array($tpl_var)) {
125 // $tpl_var is an array, ignore $value
126 foreach ($tpl_var as $_key => $_val) {
128 if (!isset($this->tpl_vars[$_key])) {
129 $tpl_var_inst = $this->getVariable($_key, null, true, false);
130 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
131 $this->tpl_vars[$_key] = new Smarty_variable(null, $nocache);
133 $this->tpl_vars[$_key] = clone $tpl_var_inst;
136 if (!(is_array($this->tpl_vars[$_key]->value) || $this->tpl_vars[$_key]->value instanceof ArrayAccess)) {
137 settype($this->tpl_vars[$_key]->value, 'array');
139 if ($merge && is_array($_val)) {
140 foreach ($_val as $_mkey => $_mval) {
141 $this->tpl_vars[$_key]->value[$_mkey] = $_mval;
144 $this->tpl_vars[$_key]->value[] = $_val;
149 if ($tpl_var != '' && isset($value)) {
150 if (!isset($this->tpl_vars[$tpl_var])) {
151 $tpl_var_inst = $this->getVariable($tpl_var, null, true, false);
152 if ($tpl_var_inst instanceof Undefined_Smarty_Variable) {
153 $this->tpl_vars[$tpl_var] = new Smarty_variable(null, $nocache);
155 $this->tpl_vars[$tpl_var] = clone $tpl_var_inst;
158 if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
159 settype($this->tpl_vars[$tpl_var]->value, 'array');
161 if ($merge && is_array($value)) {
162 foreach ($value as $_mkey => $_mval) {
163 $this->tpl_vars[$tpl_var]->value[$_mkey] = $_mval;
166 $this->tpl_vars[$tpl_var]->value[] = $value;
175 * appends values to template variables by reference
177 * @param string $tpl_var the template variable name
178 * @param mixed &$value the referenced value to append
179 * @param boolean $merge flag if array elements shall be merged
181 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
183 public function appendByRef($tpl_var, &$value, $merge = false)
185 if ($tpl_var != '' && isset($value)) {
186 if (!isset($this->tpl_vars[$tpl_var])) {
187 $this->tpl_vars[$tpl_var] = new Smarty_variable();
189 if (!is_array($this->tpl_vars[$tpl_var]->value)) {
190 settype($this->tpl_vars[$tpl_var]->value, 'array');
192 if ($merge && is_array($value)) {
193 foreach ($value as $_key => $_val) {
194 $this->tpl_vars[$tpl_var]->value[$_key] = & $value[$_key];
197 $this->tpl_vars[$tpl_var]->value[] = & $value;
205 * Returns a single or all template variables
207 * @param string $varname variable name or null
208 * @param object $_ptr optional pointer to data object
209 * @param boolean $search_parents include parent templates?
211 * @return string variable value or or array of variables
213 public function getTemplateVars($varname = null, $_ptr = null, $search_parents = true)
215 if (isset($varname)) {
216 $_var = $this->getVariable($varname, $_ptr, $search_parents, false);
217 if (is_object($_var)) {
224 if ($_ptr === null) {
227 while ($_ptr !== null) {
228 foreach ($_ptr->tpl_vars AS $key => $var) {
229 if (!array_key_exists($key, $_result)) {
230 $_result[$key] = $var->value;
233 // not found, try at parent
234 if ($search_parents) {
235 $_ptr = $_ptr->parent;
240 if ($search_parents && isset(Smarty::$global_tpl_vars)) {
241 foreach (Smarty::$global_tpl_vars AS $key => $var) {
242 if (!array_key_exists($key, $_result)) {
243 $_result[$key] = $var->value;
253 * clear the given assigned template variable.
255 * @param string|array $tpl_var the template variable(s) to clear
257 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
259 public function clearAssign($tpl_var)
261 if (is_array($tpl_var)) {
262 foreach ($tpl_var as $curr_var) {
263 unset($this->tpl_vars[$curr_var]);
266 unset($this->tpl_vars[$tpl_var]);
273 * clear all the assigned template variables.
275 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
277 public function clearAllAssign()
279 $this->tpl_vars = array();
285 * load a config file, optionally load just selected sections
287 * @param string $config_file filename
288 * @param mixed $sections array of section names, single section or null
290 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
292 public function configLoad($config_file, $sections = null)
295 $config = new Smarty_Internal_Config($config_file, $this->smarty, $this);
296 $config->loadConfigVars($sections);
302 * gets the object of a Smarty variable
304 * @param string $variable the name of the Smarty variable
305 * @param object $_ptr optional pointer to data object
306 * @param boolean $search_parents search also in parent data
307 * @param bool $error_enable
309 * @return object the object of the variable
311 public function getVariable($variable, $_ptr = null, $search_parents = true, $error_enable = true)
313 if ($_ptr === null) {
316 while ($_ptr !== null) {
317 if (isset($_ptr->tpl_vars[$variable])) {
318 // found it, return it
319 return $_ptr->tpl_vars[$variable];
321 // not found, try at parent
322 if ($search_parents) {
323 $_ptr = $_ptr->parent;
328 if (isset(Smarty::$global_tpl_vars[$variable])) {
329 // found it, return it
330 return Smarty::$global_tpl_vars[$variable];
332 if ($this->smarty->error_unassigned && $error_enable) {
337 return new Undefined_Smarty_Variable;
341 * gets a config variable
343 * @param string $variable the name of the config variable
344 * @param bool $error_enable
346 * @return mixed the value of the config variable
348 public function getConfigVariable($variable, $error_enable = true)
351 while ($_ptr !== null) {
352 if (isset($_ptr->config_vars[$variable])) {
353 // found it, return it
354 return $_ptr->config_vars[$variable];
356 // not found, try at parent
357 $_ptr = $_ptr->parent;
359 if ($this->smarty->error_unassigned && $error_enable) {
368 * gets a stream variable
370 * @param string $variable the stream of the variable
372 * @throws SmartyException
373 * @return mixed the value of the stream variable
375 public function getStreamVariable($variable)
378 $fp = fopen($variable, 'r+');
380 while (!feof($fp) && ($current_line = fgets($fp)) !== false) {
381 $_result .= $current_line;
388 if ($this->smarty->error_unassigned) {
389 throw new SmartyException('Undefined stream variable "' . $variable . '"');
396 * Returns a single or all config variables
398 * @param string $varname variable name or null
399 * @param bool $search_parents
401 * @return string variable value or or array of variables
403 public function getConfigVars($varname = null, $search_parents = true)
406 $var_array = array();
407 while ($_ptr !== null) {
408 if (isset($varname)) {
409 if (isset($_ptr->config_vars[$varname])) {
410 return $_ptr->config_vars[$varname];
413 $var_array = array_merge($_ptr->config_vars, $var_array);
415 // not found, try at parent
416 if ($search_parents) {
417 $_ptr = $_ptr->parent;
422 if (isset($varname)) {
430 * Deassigns a single or all config variables
432 * @param string $varname variable name or null
434 * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
436 public function clearConfig($varname = null)
438 if (isset($varname)) {
439 unset($this->config_vars[$varname]);
441 $this->config_vars = array();
449 * class for the Smarty data object
450 * The Smarty data object will hold Smarty variables in the current scope
453 * @subpackage Template
455 class Smarty_Data extends Smarty_Internal_Data
462 public $smarty = null;
465 * create Smarty data object
467 * @param Smarty|array $_parent parent template
468 * @param Smarty|Smarty_Internal_Template $smarty global smarty instance
470 * @throws SmartyException
472 public function __construct($_parent = null, $smarty = null)
474 $this->smarty = $smarty;
475 if (is_object($_parent)) {
476 // when object set up back pointer
477 $this->parent = $_parent;
478 } elseif (is_array($_parent)) {
479 // set up variable values
480 foreach ($_parent as $_key => $_val) {
481 $this->tpl_vars[$_key] = new Smarty_variable($_val);
483 } elseif ($_parent != null) {
484 throw new SmartyException("Wrong type for template variables");
490 * class for the Smarty variable object
491 * This class defines the Smarty variable object
494 * @subpackage Template
496 class Smarty_Variable
503 public $value = null;
505 * if true any output of this variable will be not cached
509 public $nocache = false;
511 * the scope the variable will have (local,parent or root)
515 public $scope = Smarty::SCOPE_LOCAL;
518 * create Smarty variable object
520 * @param mixed $value the value to assign
521 * @param boolean $nocache if true any output of this variable will be not cached
522 * @param int $scope the scope the variable will have (local,parent or root)
524 public function __construct($value = null, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
526 $this->value = $value;
527 $this->nocache = $nocache;
528 $this->scope = $scope;
532 * <<magic>> String conversion
536 public function __toString()
538 return (string) $this->value;
543 * class for undefined variable object
544 * This class defines an object for undefined variable handling
547 * @subpackage Template
549 class Undefined_Smarty_Variable
552 * Returns FALSE for 'nocache' and NULL otherwise.
554 * @param string $name
558 public function __get($name)
560 if ($name == 'nocache') {
568 * Always returns an empty string.
572 public function __toString()