From 7042db4041b4a8af3d27bc5be90c69565f8050ce Mon Sep 17 00:00:00 2001 From: patdenice Date: Fri, 9 Oct 2009 01:00:33 +0000 Subject: merge r3927, r3928, r3951, r3953 from trunk to branch 2.0 r3927: Add Smarty's prefilter capability. see topic:16219 (FR). r3928: Improve template prefilter functions r3951: Allow to add prefilters, postfilters and output filters to templates. r3953: Simplification of commit 3951. git-svn-id: http://piwigo.org/svn/branches/2.0@3998 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/template.class.php | 77 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/include/template.class.php b/include/template.class.php index be9aeda97..d71b23263 100644 --- a/include/template.class.php +++ b/include/template.class.php @@ -46,6 +46,9 @@ class Template { // Template extents filenames for each template handle. var $extents = array(); + // Templates prefilter from external sources (plugins) + var $external_filters = array(); + // used by html_head smarty block to add content before var $html_head_elements = array(); @@ -290,19 +293,19 @@ class Template { $this->smarty->assign( 'TAG_INPUT_ENABLED', ((is_adviser()) ? 'disabled="disabled" onclick="return false;"' : '')); + $save_compile_id = $this->smarty->compile_id; + $this->load_external_filters($handle); + global $conf, $lang_info; if ( $conf['compiled_template_cache_language'] and isset($lang_info['code']) ) { - $save_compile_id = $this->smarty->compile_id; $this->smarty->compile_id .= '.'.$lang_info['code']; } $v = $this->smarty->fetch($this->files[$handle], null, null, false); - if (isset ($save_compile_id) ) - { - $this->smarty->compile_id = $save_compile_id; - } + $this->smarty->compile_id = $save_compile_id; + $this->unload_external_filters($handle); if ($return) { @@ -333,6 +336,7 @@ class Template { } //else maybe error or warning ? $this->html_head_elements = array(); } + echo $this->output; $this->output=''; } @@ -419,6 +423,68 @@ class Template { } } + /** + * This function allows to declare a Smarty prefilter from a plugin, thus allowing + * it to modify template source before compilation and without changing core files + * They will be processed by weight ascending. + * http://www.smarty.net/manual/en/advanced.features.prefilters.php + */ + function set_prefilter($handle, $callback, $weight=50) + { + $this->external_filters[$handle][$weight][] = array('prefilter', $callback); + ksort($this->external_filters[$handle]); + } + + function set_postfilter($handle, $callback, $weight=50) + { + $this->external_filters[$handle][$weight][] = array('postfilter', $callback); + ksort($this->external_filters[$handle]); + } + + function set_outputfilter($handle, $callback, $weight=50) + { + $this->external_filters[$handle][$weight][] = array('outputfilter', $callback); + ksort($this->external_filters[$handle]); + } + + /** + * This function actually triggers the filters on the tpl files. + * Called in the parse method. + * http://www.smarty.net/manual/en/advanced.features.prefilters.php + */ + function load_external_filters($handle) + { + if (isset($this->external_filters[$handle])) + { + $compile_id = ''; + foreach ($this->external_filters[$handle] as $filters) + { + foreach ($filters as $filter) + { + list($type, $callback) = $filter; + $compile_id .= $type.( is_array($callback) ? implode('', $callback) : $callback ); + call_user_func(array($this->smarty, 'register_'.$type), $callback); + } + } + $this->smarty->compile_id .= '.'.base_convert(crc32($compile_id), 10, 36); + } + } + + function unload_external_filters($handle) + { + if (isset($this->external_filters[$handle])) + { + foreach ($this->external_filters[$handle] as $filters) + { + foreach ($filters as $filter) + { + list($type, $callback) = $filter; + call_user_func(array($this->smarty, 'unregister_'.$type), $callback); + } + } + } + } + static function prefilter_white_space($source, &$smarty) { $ld = $smarty->left_delimiter; @@ -465,6 +531,7 @@ class Template { } } + /** * This class contains basic functions that can be called directly from the * templates in the form $pwg->l10n('edit') -- cgit v1.2.3