3 * Smarty Internal Plugin Template
4 * This file contains the Smarty template engine
12 * Main class with template data structures and methods
15 * @subpackage Template
16 * @property Smarty_Template_Source $source
17 * @property Smarty_Template_Compiled $compiled
18 * @property Smarty_Template_Cached $cached
20 class Smarty_Internal_Template
extends Smarty_Internal_TemplateBase
27 public $cache_id = null;
32 public $compile_id = null;
38 public $caching = null;
40 * cache lifetime in seconds
44 public $cache_lifetime = null;
50 public $template_resource = null;
52 * flag if compiled template is invalid and must be (re)compiled
56 public $mustCompile = null;
58 * flag if template does contain nocache code sections
62 public $has_nocache_code = false;
64 * special compiled and cached template properties
68 public $properties = array('file_dependency' => array(),
70 'function' => array());
76 public $required_plugins = array('compiled' => array(), 'nocache' => array());
78 * Global smarty instance
82 public $smarty = null;
84 * blocks for template inheritance
88 public $block_data = array();
94 public $variable_filters = array();
96 * optional log of tag/attributes
100 public $used_tags = array();
102 * internal flag to allow relative path in child template blocks
106 public $allow_relative_path = false;
108 * internal capture runtime stack
112 public $_capture_stack = array(0 => array());
115 * Create template data object
116 * Some of the global Smarty settings copied to template scope
117 * It load the required template resources and cacher plugins
119 * @param string $template_resource template resource string
120 * @param Smarty $smarty Smarty instance
121 * @param Smarty_Internal_Template $_parent back pointer to parent object with variables or null
122 * @param mixed $_cache_id cache id or null
123 * @param mixed $_compile_id compile id or null
124 * @param bool $_caching use caching?
125 * @param int $_cache_lifetime cache life-time in seconds
127 public function __construct($template_resource, $smarty, $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
129 $this->smarty
= & $smarty;
131 $this->cache_id
= $_cache_id === null ?
$this->smarty
->cache_id
: $_cache_id;
132 $this->compile_id
= $_compile_id === null ?
$this->smarty
->compile_id
: $_compile_id;
133 $this->caching
= $_caching === null ?
$this->smarty
->caching
: $_caching;
134 if ($this->caching
=== true) {
135 $this->caching
= Smarty
::CACHING_LIFETIME_CURRENT
;
137 $this->cache_lifetime
= $_cache_lifetime === null ?
$this->smarty
->cache_lifetime
: $_cache_lifetime;
138 $this->parent
= $_parent;
140 $this->template_resource
= $template_resource;
141 // copy block data of template inheritance
142 if ($this->parent
instanceof Smarty_Internal_Template
) {
143 $this->block_data
= $this->parent
->block_data
;
148 * Returns if the current template must be compiled by the Smarty compiler
149 * It does compare the timestamps of template source and the compiled templates and checks the force compile configuration
151 * @throws SmartyException
152 * @return boolean true if the template must be compiled
154 public function mustCompile()
156 if (!$this->source
->exists
) {
157 if ($this->parent
instanceof Smarty_Internal_Template
) {
158 $parent_resource = " in '$this->parent->template_resource}'";
160 $parent_resource = '';
162 throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
164 if ($this->mustCompile
=== null) {
165 $this->mustCompile
= (!$this->source
->uncompiled
&& ($this->smarty
->force_compile ||
$this->source
->recompiled ||
$this->compiled
->timestamp
=== false ||
166 ($this->smarty
->compile_check
&& $this->compiled
->timestamp
< $this->source
->timestamp
)));
169 return $this->mustCompile
;
173 * Compiles the template
174 * If the template is not evaluated the compiled template is saved on disk
176 public function compileTemplateSource()
178 if (!$this->source
->recompiled
) {
179 $this->properties
['file_dependency'] = array();
180 if ($this->source
->components
) {
181 // for the extends resource the compiler will fill it
182 // uses real resource for file dependency
183 // $source = end($this->source->components);
184 // $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type);
186 $this->properties
['file_dependency'][$this->source
->uid
] = array($this->source
->filepath
, $this->source
->timestamp
, $this->source
->type
);
190 if ($this->smarty
->compile_locking
&& !$this->source
->recompiled
) {
191 if ($saved_timestamp = $this->compiled
->timestamp
) {
192 touch($this->compiled
->filepath
);
197 $code = $this->compiler
->compileTemplate($this);
199 catch (Exception
$e) {
200 // restore old timestamp in case of error
201 if ($this->smarty
->compile_locking
&& !$this->source
->recompiled
&& $saved_timestamp) {
202 touch($this->compiled
->filepath
, $saved_timestamp);
206 // compiling succeded
207 if (!$this->source
->recompiled
&& $this->compiler
->write_compiled_code
) {
208 // write compiled template
209 $_filepath = $this->compiled
->filepath
;
210 if ($_filepath === false) {
211 throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
213 Smarty_Internal_Write_File
::writeFile($_filepath, $code, $this->smarty
);
214 $this->compiled
->exists
= true;
215 $this->compiled
->isCompiled
= true;
217 // release compiler object to free memory
218 unset($this->compiler
);
222 * Writes the cached template output
224 * @param string $content
228 public function writeCachedContent($content)
230 if ($this->source
->recompiled ||
!($this->caching
== Smarty
::CACHING_LIFETIME_CURRENT ||
$this->caching
== Smarty
::CACHING_LIFETIME_SAVED
)) {
231 // don't write cache file
234 $this->cached
->timestamp
= time();
235 $this->properties
['cache_lifetime'] = $this->cache_lifetime
;
236 $this->properties
['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
237 $content = $this->createTemplateCodeFrame($content, true);
238 /** @var Smarty_Internal_Template $_smarty_tpl
239 * used in evaluated code
241 $_smarty_tpl = $this;
242 eval("?>" . $content);
243 $this->cached
->valid
= true;
244 $this->cached
->processed
= true;
246 return $this->cached
->write($this, $content);
250 * Template code runtime function to get subtemplate content
252 * @param string $template the resource handle of the template file
253 * @param mixed $cache_id cache id to be used with this template
254 * @param mixed $compile_id compile id to be used with this template
255 * @param integer $caching cache mode
256 * @param integer $cache_lifetime life time of cache data
258 * @param int $parent_scope scope in which {include} should execute
260 * @returns string template content
262 public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
264 // already in template cache?
265 if ($this->smarty
->allow_ambiguous_resources
) {
266 $_templateId = Smarty_Resource
::getUniqueTemplateName($this, $template) . $cache_id . $compile_id;
268 $_templateId = $this->smarty
->joined_template_dir
. '#' . $template . $cache_id . $compile_id;
271 if (isset($_templateId[150])) {
272 $_templateId = sha1($_templateId);
274 if (isset($this->smarty
->template_objects
[$_templateId])) {
275 // clone cached template object because of possible recursive call
276 $tpl = clone $this->smarty
->template_objects
[$_templateId];
277 $tpl->parent
= $this;
278 $tpl->caching
= $caching;
279 $tpl->cache_lifetime
= $cache_lifetime;
281 $tpl = new $this->smarty
->template_class($template, $this->smarty
, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
283 // get variables from calling scope
284 if ($parent_scope == Smarty
::SCOPE_LOCAL
) {
285 $tpl->tpl_vars
= $this->tpl_vars
;
286 $tpl->tpl_vars
['smarty'] = clone $this->tpl_vars
['smarty'];
287 } elseif ($parent_scope == Smarty
::SCOPE_PARENT
) {
288 $tpl->tpl_vars
= & $this->tpl_vars
;
289 } elseif ($parent_scope == Smarty
::SCOPE_GLOBAL
) {
290 $tpl->tpl_vars
= & Smarty
::$global_tpl_vars;
291 } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
292 $tpl->tpl_vars
= & $this->tpl_vars
;
294 $tpl->tpl_vars
= & $scope_ptr->tpl_vars
;
296 $tpl->config_vars
= $this->config_vars
;
298 // set up variable values
299 foreach ($data as $_key => $_val) {
300 $tpl->tpl_vars
[$_key] = new Smarty_variable($_val);
304 return $tpl->fetch(null, null, null, null, false, false, true);
308 * Template code runtime function to set up an inline subtemplate
310 * @param string $template the resource handle of the template file
311 * @param mixed $cache_id cache id to be used with this template
312 * @param mixed $compile_id compile id to be used with this template
313 * @param integer $caching cache mode
314 * @param integer $cache_lifetime life time of cache data
316 * @param int $parent_scope scope in which {include} should execute
317 * @param string $hash nocache hash code
319 * @returns string template content
321 public function setupInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash)
323 $tpl = new $this->smarty
->template_class($template, $this->smarty
, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
324 $tpl->properties
['nocache_hash'] = $hash;
325 // get variables from calling scope
326 if ($parent_scope == Smarty
::SCOPE_LOCAL
) {
327 $tpl->tpl_vars
= $this->tpl_vars
;
328 $tpl->tpl_vars
['smarty'] = clone $this->tpl_vars
['smarty'];
329 } elseif ($parent_scope == Smarty
::SCOPE_PARENT
) {
330 $tpl->tpl_vars
= & $this->tpl_vars
;
331 } elseif ($parent_scope == Smarty
::SCOPE_GLOBAL
) {
332 $tpl->tpl_vars
= & Smarty
::$global_tpl_vars;
333 } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
334 $tpl->tpl_vars
= & $this->tpl_vars
;
336 $tpl->tpl_vars
= & $scope_ptr->tpl_vars
;
338 $tpl->config_vars
= $this->config_vars
;
340 // set up variable values
341 foreach ($data as $_key => $_val) {
342 $tpl->tpl_vars
[$_key] = new Smarty_variable($_val);
350 * Create code frame for compiled and cached templates
352 * @param string $content optional template content
353 * @param bool $cache flag for cache file
357 public function createTemplateCodeFrame($content = '', $cache = false)
359 $plugins_string = '';
360 // include code for plugins
362 if (!empty($this->required_plugins
['compiled'])) {
363 $plugins_string = '<?php ';
364 foreach ($this->required_plugins
['compiled'] as $tmp) {
365 foreach ($tmp as $data) {
366 $file = addslashes($data['file']);
367 if (is_Array($data['function'])) {
368 $plugins_string .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n";
370 $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
374 $plugins_string .= '?>';
376 if (!empty($this->required_plugins
['nocache'])) {
377 $this->has_nocache_code
= true;
378 $plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
379 foreach ($this->required_plugins
['nocache'] as $tmp) {
380 foreach ($tmp as $data) {
381 $file = addslashes($data['file']);
382 if (is_Array($data['function'])) {
383 $plugins_string .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n");
385 $plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
389 $plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
392 // build property code
393 $this->properties
['has_nocache_code'] = $this->has_nocache_code
;
395 if (!$this->source
->recompiled
) {
396 $output = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/";
397 if ($this->smarty
->direct_access_security
) {
398 $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
402 // remove compiled code of{function} definition
403 unset($this->properties
['function']);
404 if (!empty($this->smarty
->template_functions
)) {
405 // copy code of {function} tags called in nocache mode
406 foreach ($this->smarty
->template_functions
as $name => $function_data) {
407 if (isset($function_data['called_nocache'])) {
408 foreach ($function_data['called_functions'] as $func_name) {
409 $this->smarty
->template_functions
[$func_name]['called_nocache'] = true;
413 foreach ($this->smarty
->template_functions
as $name => $function_data) {
414 if (isset($function_data['called_nocache'])) {
415 unset($function_data['called_nocache'], $function_data['called_functions'], $this->smarty
->template_functions
[$name]['called_nocache']);
416 $this->properties
['function'][$name] = $function_data;
421 $this->properties
['version'] = Smarty
::SMARTY_VERSION
;
422 if (!isset($this->properties
['unifunc'])) {
423 $this->properties
['unifunc'] = 'content_' . str_replace(array('.', ','), '_', uniqid('', true));
425 if (!$this->source
->recompiled
) {
426 $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties
, true) . ',' . ($cache ?
'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n";
427 $output .= '<?php if ($_valid && !is_callable(\'' . $this->properties
['unifunc'] . '\')) {function ' . $this->properties
['unifunc'] . '($_smarty_tpl) {?>';
429 $output .= $plugins_string;
431 if (!$this->source
->recompiled
) {
432 $output .= "<?php }} ?>\n";
439 * This function is executed automatically when a compiled or cached template file is included
440 * - Decode saved properties from compiled template and cache files
441 * - Check if compiled or cache file is valid
443 * @param array $properties special template properties
444 * @param bool $cache flag if called from cache file
446 * @return bool flag if compiled or cache file is valid
448 public function decodeProperties($properties, $cache = false)
450 $this->has_nocache_code
= $properties['has_nocache_code'];
451 $this->properties
['nocache_hash'] = $properties['nocache_hash'];
452 if (isset($properties['cache_lifetime'])) {
453 $this->properties
['cache_lifetime'] = $properties['cache_lifetime'];
455 if (isset($properties['file_dependency'])) {
456 $this->properties
['file_dependency'] = array_merge($this->properties
['file_dependency'], $properties['file_dependency']);
458 if (!empty($properties['function'])) {
459 $this->properties
['function'] = array_merge($this->properties
['function'], $properties['function']);
460 $this->smarty
->template_functions
= array_merge($this->smarty
->template_functions
, $properties['function']);
462 $this->properties
['version'] = (isset($properties['version'])) ?
$properties['version'] : '';
463 $this->properties
['unifunc'] = $properties['unifunc'];
464 // check file dependencies at compiled code
466 if ($this->properties
['version'] != Smarty
::SMARTY_VERSION
) {
468 } elseif (((!$cache && $this->smarty
->compile_check
&& empty($this->compiled
->_properties
) && !$this->compiled
->isCompiled
) ||
$cache && ($this->smarty
->compile_check
=== true ||
$this->smarty
->compile_check
=== Smarty
::COMPILECHECK_ON
)) && !empty($this->properties
['file_dependency'])) {
469 foreach ($this->properties
['file_dependency'] as $_file_to_check) {
470 if ($_file_to_check[2] == 'file' ||
$_file_to_check[2] == 'php') {
471 if ($this->source
->filepath
== $_file_to_check[0] && isset($this->source
->timestamp
)) {
472 // do not recheck current template
473 $mtime = $this->source
->timestamp
;
475 // file and php types can be checked without loading the respective resource handlers
476 $mtime = @filemtime
($_file_to_check[0]);
478 } elseif ($_file_to_check[2] == 'string') {
481 $source = Smarty_Resource
::source(null, $this->smarty
, $_file_to_check[0]);
482 $mtime = $source->timestamp
;
484 if (!$mtime ||
$mtime > $_file_to_check[1]) {
491 // CACHING_LIFETIME_SAVED cache expiry has to be validated here since otherwise we'd define the unifunc
492 if ($this->caching
=== Smarty
::CACHING_LIFETIME_SAVED
&&
493 $this->properties
['cache_lifetime'] >= 0 &&
494 (time() > ($this->cached
->timestamp +
$this->properties
['cache_lifetime']))
498 $this->cached
->valid
= $is_valid;
500 $this->mustCompile
= !$is_valid;
502 // store data in reusable Smarty_Template_Compiled
504 $this->compiled
->_properties
= $properties;
511 * Template code runtime function to create a local Smarty variable for array assignments
513 * @param string $tpl_var tempate variable name
514 * @param bool $nocache cache mode of variable
515 * @param int $scope scope of variable
517 public function createLocalArrayVariable($tpl_var, $nocache = false, $scope = Smarty
::SCOPE_LOCAL
)
519 if (!isset($this->tpl_vars
[$tpl_var])) {
520 $this->tpl_vars
[$tpl_var] = new Smarty_variable(array(), $nocache, $scope);
522 $this->tpl_vars
[$tpl_var] = clone $this->tpl_vars
[$tpl_var];
523 if ($scope != Smarty
::SCOPE_LOCAL
) {
524 $this->tpl_vars
[$tpl_var]->scope
= $scope;
526 if (!(is_array($this->tpl_vars
[$tpl_var]->value
) ||
$this->tpl_vars
[$tpl_var]->value
instanceof ArrayAccess
)) {
527 settype($this->tpl_vars
[$tpl_var]->value
, 'array');
533 * Template code runtime function to get pointer to template variable array of requested scope
535 * @param int $scope requested variable scope
537 * @return array array of template variables
539 public function &getScope($scope)
541 if ($scope == Smarty
::SCOPE_PARENT
&& !empty($this->parent
)) {
542 return $this->parent
->tpl_vars
;
543 } elseif ($scope == Smarty
::SCOPE_ROOT
&& !empty($this->parent
)) {
544 $ptr = $this->parent
;
545 while (!empty($ptr->parent
)) {
549 return $ptr->tpl_vars
;
550 } elseif ($scope == Smarty
::SCOPE_GLOBAL
) {
551 return Smarty
::$global_tpl_vars;
559 * Get parent or root of template parent chain
561 * @param int $scope pqrent or root scope
563 * @return mixed object
565 public function getScopePointer($scope)
567 if ($scope == Smarty
::SCOPE_PARENT
&& !empty($this->parent
)) {
568 return $this->parent
;
569 } elseif ($scope == Smarty
::SCOPE_ROOT
&& !empty($this->parent
)) {
570 $ptr = $this->parent
;
571 while (!empty($ptr->parent
)) {
582 * [util function] counts an array, arrayaccess/traversable or PDOStatement object
584 * @param mixed $value
586 * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0 for empty elements
588 public function _count($value)
590 if (is_array($value) === true ||
$value instanceof Countable
) {
591 return count($value);
592 } elseif ($value instanceof IteratorAggregate
) {
593 // Note: getIterator() returns a Traversable, not an Iterator
594 // thus rewind() and valid() methods may not be present
595 return iterator_count($value->getIterator());
596 } elseif ($value instanceof Iterator
) {
597 return iterator_count($value);
598 } elseif ($value instanceof PDOStatement
) {
599 return $value->rowCount();
600 } elseif ($value instanceof Traversable
) {
601 return iterator_count($value);
602 } elseif ($value instanceof ArrayAccess
) {
603 if ($value->offsetExists(0)) {
606 } elseif (is_object($value)) {
607 return count($value);
614 * runtime error not matching capture tags
617 public function capture_error()
619 throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
623 * Empty cache for this template
625 * @param integer $exp_time expiration time
627 * @return integer number of cache files deleted
629 public function clearCache($exp_time = null)
631 Smarty_CacheResource
::invalidLoadedCache($this->smarty
);
633 return $this->cached
->handler
->clear($this->smarty
, $this->template_name
, $this->cache_id
, $this->compile_id
, $exp_time);
637 * set Smarty property in template context
639 * @param string $property_name property name
640 * @param mixed $value value
642 * @throws SmartyException
644 public function __set($property_name, $value)
646 switch ($property_name) {
651 $this->$property_name = $value;
655 // FIXME: routing of template -> smarty attributes
657 if (property_exists($this->smarty
, $property_name)) {
658 $this->smarty
->$property_name = $value;
664 throw new SmartyException("invalid template property '$property_name'.");
668 * get Smarty property in template context
670 * @param string $property_name property name
672 * @throws SmartyException
674 public function __get($property_name)
676 switch ($property_name) {
678 if (strlen($this->template_resource
) == 0) {
679 throw new SmartyException('Missing template name');
681 $this->source
= Smarty_Resource
::source($this);
682 // cache template object under a unique ID
683 // do not cache eval resources
684 if ($this->source
->type
!= 'eval') {
685 if ($this->smarty
->allow_ambiguous_resources
) {
686 $_templateId = $this->source
->unique_resource
. $this->cache_id
. $this->compile_id
;
688 $_templateId = $this->smarty
->joined_template_dir
. '#' . $this->template_resource
. $this->cache_id
. $this->compile_id
;
691 if (isset($_templateId[150])) {
692 $_templateId = sha1($_templateId);
694 $this->smarty
->template_objects
[$_templateId] = $this;
697 return $this->source
;
700 $this->compiled
= $this->source
->getCompiled($this);
702 return $this->compiled
;
705 if (!class_exists('Smarty_Template_Cached')) {
706 include SMARTY_SYSPLUGINS_DIR
. 'smarty_cacheresource.php';
708 $this->cached
= new Smarty_Template_Cached($this);
710 return $this->cached
;
713 $this->smarty
->loadPlugin($this->source
->compiler_class
);
714 $this->compiler
= new $this->source
->compiler_class($this->source
->template_lexer_class
, $this->source
->template_parser_class
, $this->smarty
);
716 return $this->compiler
;
718 // FIXME: routing of template -> smarty attributes
720 if (property_exists($this->smarty
, $property_name)) {
721 return $this->smarty
->$property_name;
725 throw new SmartyException("template property '$property_name' does not exist.");
729 * Template data object destructor
732 public function __destruct()
734 if ($this->smarty
->cache_locking
&& isset($this->cached
) && $this->cached
->is_locked
) {
735 $this->cached
->handler
->releaseLock($this->smarty
, $this->cached
);