From e89c15d65d246e8f0d8e460c8f0e6f50a04faf85 Mon Sep 17 00:00:00 2001 From: "Uwe.Tews" Date: Fri, 22 Jan 2010 16:39:30 +0000 Subject: [PATCH] - new method $smarty->createData([$parent]) for creating a data object (required for bugfixes below) - bugfix config_load() method now works also on a data object - bugfix get_config_vars() method now works also on a data and template objects - bugfix clear_config() method now works also on a data and template objects --- README | 10 +-- change_log.txt | 10 +++ libs/Smarty.class.php | 61 +++++++++++++++- libs/sysplugins/smarty_internal_data.php | 69 ++++++++----------- .../sysplugins/smarty_method_clear_config.php | 30 -------- .../smarty_method_get_config_vars.php | 36 ---------- 6 files changed, 105 insertions(+), 111 deletions(-) delete mode 100644 libs/sysplugins/smarty_method_clear_config.php delete mode 100644 libs/sysplugins/smarty_method_get_config_vars.php diff --git a/README b/README index 84a37ba2..beb9c0a4 100644 --- a/README +++ b/README @@ -260,14 +260,14 @@ Besides the above mentioned objects, there is also a special storage area for global variables. A Smarty data object can be created as follows: -$data = new Smarty_Data; // create root data object +$data = $smarty->createData(); // create root data object $data->assign('foo','bar'); // assign variables as usual -$data->conf_load('my.conf'); // load config file +$data->config_load('my.conf'); // load config file -$data= new Smarty_Data($smarty); // create data object having a parent link to +$data= $smarty->createData($smarty); // create data object having a parent link to the Smarty object -$data2= new Smarty_Data($data); // create data object having a parent link to +$data2= $smarty->createData($data); // create data object having a parent link to the $data data object A template object can be created by using the createTemplate method. It has the @@ -280,7 +280,7 @@ The first parameter can be a template name, a smarty object or a data object. Examples: $tpl = $smarty->createTemplate('mytpl.tpl'); // create template object not linked to any parent $tpl->assign('foo','bar'); // directly assign variables -$tpl->conf_load('my.conf'); // load config file +$tpl->config_load('my.conf'); // load config file $tpl = $smarty->createTemplate('mytpl.tpl',$smarty); // create template having a parent link to the Smarty object $tpl = $smarty->createTemplate('mytpl.tpl',$data); // create template having a parent link to the $data object diff --git a/change_log.txt b/change_log.txt index 83466f5f..2301925a 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,6 +1,16 @@ +01/22/2010 +- new method $smarty->createData([$parent]) for creating a data object (required for bugfixes below) +- bugfix config_load() method now works also on a data object +- bugfix get_config_vars() method now works also on a data and template objects +- bugfix clear_config() method now works also on a data and template objects + 01/19/2010 - bugfix on plugins if same plugin was called from a nocache section first and later from a cached section + +###beta 7### + + 01/17/2010 - bugfix on $smarty.const... in double quoted strings diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 37bcfb83..baabca6d 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -203,7 +203,9 @@ class Smarty extends Smarty_Internal_Data { // autoload filter public $autoload_filters = array(); // status of filter on variable output - public $variable_filter = true; + public $variable_filter = true; + // default modifier + public $default_modifiers = array(); // global internal smarty vars public $_smarty_vars = array(); // start time for execution time calculation @@ -346,6 +348,63 @@ class Smarty extends Smarty_Internal_Data { return $template->isCached(); } + /** + * creates a data object + * + * @param object $parent next higher level of Smarty variables + * @returns object data object + */ + public function createData($parent = null) + { + return new Smarty_Data($parent, $this); + } + + /** + * creates a template object + * + * @param string $template the resource handle of the template file + * @param object $parent next higher level of Smarty variables + * @param mixed $cache_id cache id to be used with this template + * @param mixed $compile_id compile id to be used with this template + * @returns object template object + */ + public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null) + { + if (is_object($cache_id) || is_array($cache_id)) { + $parent = $cache_id; + $cache_id = null; + } + if (is_array($parent)) { + $data = $parent; + $parent = null; + } else { + $data = null; + } + if (!is_object($template)) { + // we got a template resource + // already in template cache? + $_templateId = crc32($template . $cache_id . $compile_id); + if (isset($this->template_objects[$_templateId]) && $this->caching) { + // return cached template object + $tpl = $this->template_objects[$_templateId]; + } else { + // create new template object + $tpl = new $this->template_class($template, $this, $parent, $cache_id, $compile_id); + } + } else { + // just return a copy of template class + $tpl = $template; + } + // fill data if present + if (is_array($data)) { + // set up variable values + foreach ($data as $_key => $_val) { + $tpl->tpl_vars[$_key] = new Smarty_variable($_val); + } + } + return $tpl; + } + /** * Loads security class and enables security */ diff --git a/libs/sysplugins/smarty_internal_data.php b/libs/sysplugins/smarty_internal_data.php index 61b9b52b..3727a22f 100644 --- a/libs/sysplugins/smarty_internal_data.php +++ b/libs/sysplugins/smarty_internal_data.php @@ -158,7 +158,6 @@ class Smarty_Internal_Data { } } - /** * clear the given assigned template variable. * @@ -278,49 +277,38 @@ class Smarty_Internal_Data { } /** - * creates a template object + * Returns a single or all config variables * - * @param string $template the resource handle of the template file - * @param object $parent next higher level of Smarty variables - * @param mixed $cache_id cache id to be used with this template - * @param mixed $compile_id compile id to be used with this template - * @returns object template object + * @param string $varname variable name or null + * @return string variable value or or array of variables */ - public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null) + function get_config_vars($varname = null) { - if (is_object($cache_id) || is_array($cache_id)) { - $parent = $cache_id; - $cache_id = null; - } - if (is_array($parent)) { - $data = $parent; - $parent = null; - } else { - $data = null; - } - if (!is_object($template)) { - // we got a template resource - // already in template cache? - $_templateId = crc32($template . $cache_id . $compile_id); - if (isset($this->smarty->template_objects[$_templateId]) && $this->smarty->caching) { - // return cached template object - $tpl = $this->smarty->template_objects[$_templateId]; + if (isset($varname)) { + if (isset($this->config_vars[$varname])) { + return $this->config_vars[$varname]; } else { - // create new template object - $tpl = new $this->template_class($template, $this->smarty, $parent, $cache_id, $compile_id); + return ''; } } else { - // just return a copy of template class - $tpl = $template; + return $this->config_vars; } - // fill data if present - if (is_array($data)) { - // set up variable values - foreach ($data as $_key => $_val) { - $tpl->tpl_vars[$_key] = new Smarty_variable($_val); - } + } + + /** + * Deassigns a single or all config variables + * + * @param string $varname variable name or null + */ + function clear_config($varname = null) + { + if (isset($varname)) { + unset($this->config_vars[$varname]); + return; + } else { + $this->config_vars = array(); + return; } - return $tpl; } /** @@ -349,12 +337,15 @@ class Smarty_Data extends Smarty_Internal_Data { // back pointer to parent object public $parent = null; // config vars - public $config_vars = array(); + public $config_vars = array(); + // Smarty object + public $smarty = null; /** * create Smarty data object */ - public function __construct ($_parent = null) + public function __construct ($_parent = null, $smarty = null) { + $this->smarty = $smarty; if (is_object($_parent)) { // when object set up back pointer $this->parent = $_parent; @@ -363,7 +354,7 @@ class Smarty_Data extends Smarty_Internal_Data { foreach ($_parent as $_key => $_val) { $this->tpl_vars[$_key] = new Smarty_variable($_val); } - } else { + } elseif ($_parent != null) { throw new Exception("Wrong type for template variables"); } } diff --git a/libs/sysplugins/smarty_method_clear_config.php b/libs/sysplugins/smarty_method_clear_config.php deleted file mode 100644 index 3c752e05..00000000 --- a/libs/sysplugins/smarty_method_clear_config.php +++ /dev/null @@ -1,30 +0,0 @@ -config_vars[$varname]); - return; - } else { - $smarty->config_vars = array(); - return; - } -} - -?> diff --git a/libs/sysplugins/smarty_method_get_config_vars.php b/libs/sysplugins/smarty_method_get_config_vars.php deleted file mode 100644 index 9bbf4b58..00000000 --- a/libs/sysplugins/smarty_method_get_config_vars.php +++ /dev/null @@ -1,36 +0,0 @@ -config_vars[$varname])) { - return $smarty->config_vars[$varname]; - } else { - return ''; - } - } else { - return $smarty->config_vars; - } -} - -?>