4 * Smarty Internal Plugin Resource PHP
5 * Implements the file system as resource for PHP templates
8 * @subpackage TemplateResources
12 class Smarty_Internal_Resource_PHP extends Smarty_Resource_Uncompiled
15 * container for short_open_tag directive's value before executing PHP templates
19 protected $short_open_tag;
22 * Create a new PHP Resource
25 public function __construct()
27 $this->short_open_tag = ini_get('short_open_tag');
31 * populate Source Object with meta data from Resource
33 * @param Smarty_Template_Source $source source object
34 * @param Smarty_Internal_Template $_template template object
38 public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null)
40 $source->filepath = $this->buildFilepath($source, $_template);
42 if ($source->filepath !== false) {
43 if (is_object($source->smarty->security_policy)) {
44 $source->smarty->security_policy->isTrustedResourceDir($source->filepath);
47 $source->uid = sha1($source->filepath);
48 if ($source->smarty->compile_check) {
49 $source->timestamp = @filemtime($source->filepath);
50 $source->exists = !!$source->timestamp;
56 * populate Source Object with timestamp and exists from Resource
58 * @param Smarty_Template_Source $source source object
62 public function populateTimestamp(Smarty_Template_Source $source)
64 $source->timestamp = @filemtime($source->filepath);
65 $source->exists = !!$source->timestamp;
69 * Load template's source from file into current template object
71 * @param Smarty_Template_Source $source source object
73 * @return string template source
74 * @throws SmartyException if source cannot be loaded
76 public function getContent(Smarty_Template_Source $source)
78 if ($source->timestamp) {
81 throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
85 * Render and output the template (without using the compiler)
87 * @param Smarty_Template_Source $source source object
88 * @param Smarty_Internal_Template $_template template object
91 * @throws SmartyException if template cannot be loaded or allow_php_templates is disabled
93 public function renderUncompiled(Smarty_Template_Source $source, Smarty_Internal_Template $_template)
95 if (!$source->smarty->allow_php_templates) {
96 throw new SmartyException("PHP templates are disabled");
98 if (!$source->exists) {
99 if ($_template->parent instanceof Smarty_Internal_Template) {
100 $parent_resource = " in '{$_template->parent->template_resource}'";
102 $parent_resource = '';
104 throw new SmartyException("Unable to load template {$source->type} '{$source->name}'{$parent_resource}");
108 extract($_template->getTemplateVars());
110 // include PHP template with short open tags enabled
111 ini_set('short_open_tag', '1');
112 /** @var Smarty_Internal_Template $_smarty_template
113 * used in included file
115 $_smarty_template = $_template;
116 include($source->filepath);
117 ini_set('short_open_tag', $this->short_open_tag);