From d991f96423d68fc8aa5807510aff6732d11e32eb Mon Sep 17 00:00:00 2001 From: "Uwe.Tews" Date: Tue, 21 Apr 2009 22:09:17 +0000 Subject: [PATCH] - unified template stream syntax with standard Smarty resource syntax $smarty->display('mystream:mytemplate') --- change_log.txt | 3 + libs/sysplugins/internal.template.php | 206 ++++++++++++++------------ 2 files changed, 111 insertions(+), 98 deletions(-) diff --git a/change_log.txt b/change_log.txt index 4f0397f2..2a2dc2de 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,6 @@ +04/22/2009 +- unified template stream syntax with standard Smarty resource syntax $smarty->display('mystream:mytemplate') + 04/21/2009 - change of new style syntax for forach. Now: {foreach $array as $var} like in PHP diff --git a/libs/sysplugins/internal.template.php b/libs/sysplugins/internal.template.php index b07887ac..2671f8b8 100644 --- a/libs/sysplugins/internal.template.php +++ b/libs/sysplugins/internal.template.php @@ -473,7 +473,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { // no resource given, use default $this->resource_type = $this->smarty->default_resource_type; $this->resource_name = $template_resource; - } elseif (strpos($template_resource, '://') != strpos($template_resource, ':')) { + } else { // get type and name from path list($this->resource_type, $this->resource_name) = explode(':', $template_resource, 2); if (strlen($this->resource_type) == 1) { @@ -483,117 +483,127 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { } else { $this->resource_type = strtolower($this->resource_type); } - } else { - // stream resource - $this->resource_type = 'stream'; - $this->resource_name = $template_resource; } // 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', 'extend', 'stream'))) { - // internal, get from sysplugins dir - $_resource_class = "Smarty_Internal_Resource_{$this->resource_type}"; + // try sysplugins dir first + $_resource_class = "Smarty_Internal_Resource_{$this->resource_type}"; + if ($this->smarty->loadPlugin($_resource_class)) { + $this->resource_objects[$this->resource_type] = new $_resource_class; } else { - // custom, get from plugins dir + // try plugins dir $_resource_class = "Smarty_Resource_{$this->resource_type}"; + if ($this->smarty->loadPlugin($_resource_class)) { + $this->resource_objects[$this->resource_type] = new $_resource_class; + } else { + // try streams + $_known_stream = stream_get_wrappers(); + if (in_array($this->resource_type, $_known_stream)) { + // is known stream + if (!isset($this->resource_objects['stream'])) { + $this->smarty->loadPlugin('Smarty_Internal_Resource_Stream'); + $this->resource_objects['stream'] = new Smarty_Internal_Resource_Stream; + } + $this->resource_type = 'stream'; + $this->resource_name = str_replace(':', '://', $template_resource); + } else { + throw new Exception('Unkown resource type \'' . $this->resource_type . '\''); + } + } } - // load resource plugin, instantiate - $this->smarty->loadPlugin($_resource_class); - $this->resource_objects[$this->resource_type] = new $_resource_class; } // cache template object under a unique ID // do not cache string resources - if (!in_array($this->resource_type, array('string', 'stream'))) { - Smarty::$template_objects[$this->buildTemplateId ($this->template_resource, $this->cache_id, $this->compile_id)] = $this; - } - return true; + if ($this->resource_type != 'string') { + Smarty::$template_objects[$this->buildTemplateId ($this->template_resource, $this->cache_id, $this->compile_id)] = $this; + } + return true; + } + + /** + * get system filepath to template + */ + 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; } - /** - * get system filepath to template - */ - 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 . $file; - if (file_exists($_filepath)) - return $_filepath; - } - if (file_exists($file)) return $file; - // no tpl file found - if (!empty($this->smarty->default_template_handler_func)) { - if (!is_callable($this->smarty->default_template_handler_func)) { - throw new Exception("Default template handler not callable"); - } else { - $_return = call_user_func_array($this->smarty->default_template_handler_func, - array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, &$this)); - if ($_return == true) { - return $_filepath; - } - } - } - throw new Exception("Unable to load template \"{$file}\""); - return false; - } - - /** - * Update Smarty variables in parent variable object - */ - public function updateParentVariables ($scope = SMARTY_LOCAL_SCOPE) - { - foreach ($this->tpl_vars as $_key => $_value) { - // copy global vars back to parent - if (isset($this->parent) && ($scope == SMARTY_PARENT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_PARENT_SCOPE)) { - if (isset($this->parent->tpl_vars[$_key])) { - // variable is already defined in parent, copy value - $this->parent->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; - } else { - // create variable in parent - $this->parent->tpl_vars[$_key] = clone $_value; - $this->smarty->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE; - } - } - if ($scope == SMARTY_ROOT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_ROOT_SCOPE) { - $_ptr = $this; - // find root - while ($_ptr->parent != null) { - $_ptr = $_ptr->parent; - } - if (isset($_ptr->tpl_vars[$_key])) { - // variable is already defined in root, copy value - $_ptr->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; - } else { - // create variable in root - $_ptr->tpl_vars[$_key] = clone $_value; - $_ptr->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE; - } - } - if ($scope == SMARTY_GLOBAL_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_GLOBAL_SCOPE) { - if (isset($this->smarty->global_tpl_vars[$_key])) { - // variable is already defined in root, copy value - $this->smarty->global_tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; - } else { - // create variable in root - $this->smarty->global_tpl_vars[$_key] = clone $_value; - } - $this->smarty->global_tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE; - } + $_filepath = $_template_dir . $file; + if (file_exists($_filepath)) + return $_filepath; + } + if (file_exists($file)) return $file; + // no tpl file found + if (!empty($this->smarty->default_template_handler_func)) { + if (!is_callable($this->smarty->default_template_handler_func)) { + throw new Exception("Default template handler not callable"); + } else { + $_return = call_user_func_array($this->smarty->default_template_handler_func, + array($this->resource_type, $this->resource_name, &$this->template_source, &$this->template_timestamp, &$this)); + if ($_return == true) { + return $_filepath; } } } + throw new Exception("Unable to load template \"{$file}\""); + return false; + } - /** - * wrapper for template class - */ - class Smarty_Template extends Smarty_Internal_Template { + /** + * Update Smarty variables in parent variable object + */ + public function updateParentVariables ($scope = SMARTY_LOCAL_SCOPE) + { + foreach ($this->tpl_vars as $_key => $_value) { + // copy global vars back to parent + if (isset($this->parent) && ($scope == SMARTY_PARENT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_PARENT_SCOPE)) { + if (isset($this->parent->tpl_vars[$_key])) { + // variable is already defined in parent, copy value + $this->parent->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; + } else { + // create variable in parent + $this->parent->tpl_vars[$_key] = clone $_value; + $this->smarty->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE; + } + } + if ($scope == SMARTY_ROOT_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_ROOT_SCOPE) { + $_ptr = $this; + // find root + while ($_ptr->parent != null) { + $_ptr = $_ptr->parent; + } + if (isset($_ptr->tpl_vars[$_key])) { + // variable is already defined in root, copy value + $_ptr->tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; + } else { + // create variable in root + $_ptr->tpl_vars[$_key] = clone $_value; + $_ptr->tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE; + } + } + if ($scope == SMARTY_GLOBAL_SCOPE || $this->tpl_vars[$_key]->scope == SMARTY_GLOBAL_SCOPE) { + if (isset($this->smarty->global_tpl_vars[$_key])) { + // variable is already defined in root, copy value + $this->smarty->global_tpl_vars[$_key]->value = $this->tpl_vars[$_key]->value; + } else { + // create variable in root + $this->smarty->global_tpl_vars[$_key] = clone $_value; + } + $this->smarty->global_tpl_vars[$_key]->scope = SMARTY_LOCAL_SCOPE; + } } + } +} - ?> +/** +* wrapper for template class +*/ +class Smarty_Template extends Smarty_Internal_Template { +} + +?>