diff --git a/change_log.txt b/change_log.txt index 1a26703d..8125178c 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,6 @@ +03/27/2009 +- added extend resource + 03/26/2009 - fixed parser not to create error on `word` in double quoted strings - allow PHP array(...) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 54b9ef38..447aa118 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -384,12 +384,10 @@ class Smarty extends Smarty_Internal_TemplateBase { $plugin_filename = strtolower('method.' . $name . $this->php_ext); if (!file_exists($this->sysplugins_dir . $plugin_filename)) { throw new Exception("Sysplugin file " . $plugin_filename . " does not exist"); - die(); } require_once($this->sysplugins_dir . $plugin_filename); if (!class_exists($class_name, false)) { throw new Exception ("Sysplugin file " . $plugin_filename . " does not define class " . $class_name); - die(); } } $method = new $class_name; diff --git a/libs/sysplugins/internal.resource_extend.php b/libs/sysplugins/internal.resource_extend.php new file mode 100644 index 00000000..ddf53f93 --- /dev/null +++ b/libs/sysplugins/internal.resource_extend.php @@ -0,0 +1,154 @@ +resource_name); + $_filepath = $_template->buildTemplateFilepath ($_files[0]); + if ($_template->security) { + $_template->smarty->security_handler->isTrustedResourceDir($_filepath); + } + + return $_filepath; + } + + /** + * Get timestamp to template source + * + * @param object $_template template object + * @return integer timestamp of template source file + */ + public function getTemplateTimestamp($_template) + { + return filemtime($_template->getTemplateFilepath()); + } + + /** + * Read template source from file + * + * @param object $_template template object + * @return string content of template source file + */ + public function getTemplateSource($_template) + { + $this->template = $_template; + $_files = explode('|', $_template->resource_name); + foreach ($_files as $_file) { + $_filepath = $_template->buildTemplateFilepath ($_file); + if ($_file != $_files[0]) { + $_template->file_dependency['file_dependency'][] = array($_filepath, filemtime($_filepath)); + } + // read template file + $_content = file_get_contents($_filepath); + if ($_file != $_files[count($_files)-1]) { + $_content = preg_replace ('/' . $this->smarty->left_delimiter . 'extend\s+(?:file=)?\s*(\S+?|(["\']).+?\2)' . $this->smarty->right_delimiter . '/i', '' , $_content, 1); + $_content = preg_replace_callback('/(' . $this->smarty->left_delimiter . 'block(.+?)' . $this->smarty->right_delimiter . ')((?:\r?\n?)(.*?)(?:\r?\n?))(' . $this->smarty->left_delimiter . '\/block(.*?)' . $this->smarty->right_delimiter . ')/is', array('Smarty_Internal_Resource_Extend', 'saveBlockData'), $_content); + } else { + $_template->template_source = $_content; + } + } + } + protected function saveBlockData(array $matches) + { + if (0 == preg_match('/(.?)(name=)(.*)/', $matches[2], $_match)) { + $this->compiler->trigger_template_error("\"" . $matches[0] . "\" missing name attribute"); + } else { + $_name = trim($_match[3], "\"'"); + if (!isset($this->template->block_data[$_name])) { + // check for smarty possible tags + if (strpos($matches[3], $this->smarty->left_delimiter) === false) { + // output as is + $_output = $matches[3]; + } else { + // tags in $_content will be precompiled and compiled code is returnd + $tpl = $this->smarty->createTemplate('string:' . $matches[3]); + $tpl->suppressHeader = true; + $_output = $tpl->getCompiledTemplate(); + $tpl->suppressHeader = false; + // $_output = 'smarty->fetch(\'string:' . addcslashes($_content,"'") . '\', $_smarty_tpl); ? >'; + } + $this->template->block_data[$_name]['source'] = $matches[3]; + $this->template->block_data[$_name]['compiled'] = $_output; + } + } + } + + /** + * Return flag that this resource uses the compiler + * + * @return boolean true + */ + public function usesCompiler() + { + // template has tags, uses compiler + return true; + } + + /** + * Return flag that this is not evaluated + * + * @return boolean false + */ + public function isEvaluated() + { + // save the compiled file to disk, do not evaluate + return false; + } + /** + * Get filepath to compiled template + * + * @param object $_template template object + * @return string return path to compiled template + */ + public function getCompiledFilepath($_template) + { + $_files = explode('|', $_template->resource_name); + $_filepath = md5($_files[0]); + // if use_sub_dirs, break file into directories + if ($_template->smarty->use_sub_dirs) { + $_filepath = substr($_filepath, 0, 3) . DIRECTORY_SEPARATOR + . substr($_filepath, 0, 2) . DIRECTORY_SEPARATOR + . substr($_filepath, 0, 1) . DIRECTORY_SEPARATOR + . $_filepath; + } + $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DIRECTORY_SEPARATOR : '^'; + if (isset($_template->compile_id)) { + $_filepath = $_template->compile_id . $_compile_dir_sep . $_filepath; + } + if ($_template->caching) { + $_cache = '.cache'; + } else { + $_cache = ''; + } + $_compile_dir = $_template->smarty->compile_dir; + if (substr($_compile_dir, -1) != DIRECTORY_SEPARATOR) { + $_compile_dir .= DIRECTORY_SEPARATOR; + } + return $_compile_dir . $_filepath . '.' . basename($_files[0]) . $_cache . $_template->smarty->php_ext; + } +} + +?> diff --git a/libs/sysplugins/internal.template.php b/libs/sysplugins/internal.template.php index e5667751..a2488f58 100644 --- a/libs/sysplugins/internal.template.php +++ b/libs/sysplugins/internal.template.php @@ -487,7 +487,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { // load resource handler if required if (!isset($this->resource_objects[$this->resource_type])) { // is this an internal or custom resource? - if (in_array($this->resource_type, array('file', 'php', 'string'))) { + if (in_array($this->resource_type, array('file', 'php', 'string','extend'))) { // internal, get from sysplugins dir $_resource_class = "Smarty_Internal_Resource_{$this->resource_type}"; } else { @@ -509,20 +509,23 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { /** * get system filepath to template */ - public function buildTemplateFilepath () + public function buildTemplateFilepath ($file = null) { + if ($file == null) { + $file = $this->resource_name; + } foreach((array)$this->smarty->template_dir as $_template_dir) { if (substr($_template_dir, -1) != DIRECTORY_SEPARATOR) { $_template_dir .= DIRECTORY_SEPARATOR; } - $_filepath = $_template_dir . $this->resource_name; + $_filepath = $_template_dir . $file; if (file_exists($_filepath)) return $_filepath; } - if (file_exists($this->resource_name)) return $this->resource_name; + if (file_exists($file)) return $file; // no tpl file found - throw new Exception("Unable to load template \"{$this->resource_name}\""); + throw new Exception("Unable to load template \"{$file}\""); return false; }