From e315782819c73189e37d7e6ecd57b68f33c64173 Mon Sep 17 00:00:00 2001 From: "Uwe.Tews" Date: Fri, 24 Apr 2009 19:59:51 +0000 Subject: [PATCH] - added new {template} tag --- change_log.txt | 3 + libs/sysplugins/internal.compile_block.php | 4 +- .../internal.compile_blockclose.php | 2 +- libs/sysplugins/internal.compile_extend.php | 2 +- libs/sysplugins/internal.compile_include.php | 2 +- libs/sysplugins/internal.compile_template.php | 42 +++++++++++ .../internal.compile_templatecall.php | 72 +++++++++++++++++++ .../internal.compile_templateclose.php | 44 ++++++++++++ libs/sysplugins/internal.template.php | 23 +++--- .../internal.templatecompilerbase.php | 9 ++- 10 files changed, 186 insertions(+), 17 deletions(-) create mode 100644 libs/sysplugins/internal.compile_template.php create mode 100644 libs/sysplugins/internal.compile_templatecall.php create mode 100644 libs/sysplugins/internal.compile_templateclose.php diff --git a/change_log.txt b/change_log.txt index a2ece6e3..3bbf2a2c 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,6 @@ +04/24/2009 +- added new {template} tag + 04/23/2009 - fixed access of special smarty variables from included template diff --git a/libs/sysplugins/internal.compile_block.php b/libs/sysplugins/internal.compile_block.php index 67077ada..7d011dd8 100644 --- a/libs/sysplugins/internal.compile_block.php +++ b/libs/sysplugins/internal.compile_block.php @@ -1,6 +1,6 @@ template); // save file dependency - $compiler->template->file_dependency['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp()); + $compiler->template->properties['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp()); // $_old_source = preg_replace ('/' . $this->smarty->left_delimiter . 'extend\s+(?:file=)?\s*(\S+?|(["\']).+?\2)' . $this->smarty->right_delimiter . '/i', '' , $compiler->template->template_source, 1); $_old_source = $compiler->template->template_source; $_old_source = 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_Compile_Extend', 'saveBlockData'), $_old_source); diff --git a/libs/sysplugins/internal.compile_include.php b/libs/sysplugins/internal.compile_include.php index 090f1940..a4cc31b6 100644 --- a/libs/sysplugins/internal.compile_include.php +++ b/libs/sysplugins/internal.compile_include.php @@ -68,7 +68,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase { // if ($this->compiler->tag_nocache == false) { // save file dependency -// $compiler->template->file_dependency['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp()); +// $compiler->template->properties['file_dependency'][] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp()); // unset ($_template); // } // create template object diff --git a/libs/sysplugins/internal.compile_template.php b/libs/sysplugins/internal.compile_template.php new file mode 100644 index 00000000..1fb02158 --- /dev/null +++ b/libs/sysplugins/internal.compile_template.php @@ -0,0 +1,42 @@ +compiler = $compiler; + $this->required_attributes = array('name'); + $this->optional_attributes = array('_any'); + // check and get attributes + $_attr = $this->_get_attributes($args); + $save = array($_attr, $compiler->template->extracted_compiled_code, $compiler->template->extract_code); + $this->_open_tag('template', $save); + $_name = trim($_attr['name'], "'"); + foreach ($_attr as $_key => $_data) { + $compiler->template->properties['template'][$_name]['parameter'][$_key] = $_data; + } + $compiler->template->extract_code = true; + $compiler->template->extracted_compiled_code = ''; + $compiler->template->has_code = false; + return true; + } +} + +?> diff --git a/libs/sysplugins/internal.compile_templatecall.php b/libs/sysplugins/internal.compile_templatecall.php new file mode 100644 index 00000000..8fabfb77 --- /dev/null +++ b/libs/sysplugins/internal.compile_templatecall.php @@ -0,0 +1,72 @@ +compiler = $compiler; + $this->required_attributes = array('name'); + $this->optional_attributes = array('_any'); + // check and get attributes + $_attr = $this->_get_attributes($args); + // save posible attributes + if (isset($_attr['assign'])) { + // output will be stored in a smarty variable instead of beind displayed + $_assign = $_attr['assign']; + } + $_name = trim( $_attr['name'],"'"); + // create template object + $_output = "template->properties['template'][$_name]['parameter'])) { + foreach ($compiler->template->properties['template'][$_name]['parameter'] as $_key => $_value) { + if (!isset($_attr[$_key])) { + $_output .= "\$_template->assign('$_key',$_value);"; + } + } + } + // delete {include} standard attributes + unset($_attr['name'], $_attr['assign']); + // remaining attributes must be assigned as smarty variable + if (!empty($_attr)) { + // create variables + foreach ($_attr as $_key => $_value) { + $_output .= "\$_template->assign('$_key',$_value);"; + } + } + if (isset($compiler->template->properties['template'][$_name]['compiled'])) { + $_compiled = str_replace(array('_%n',"'"), array('',"\'"), $compiler->template->properties['template'][$_name]['compiled']); + $_output .= "\$_template->compiled_template = '$_compiled'; \$_template->mustCompile = false;"; + } else { +// for recursion + $_output .= "\$_template->compiled_template = \$_smarty_tpl->compiled_template; \$_template->mustCompile = false;"; + } + // was there an assign attribute + if (isset($_assign)) { + $_output .= "\$_smarty_tpl->assign($_assign,\$_smarty_tpl->smarty->fetch(\$_template)); ?>"; + } else { + $_output .= "echo \$_smarty_tpl->smarty->fetch(\$_template); ?>"; + } + return $_output; + } +} + +?> diff --git a/libs/sysplugins/internal.compile_templateclose.php b/libs/sysplugins/internal.compile_templateclose.php new file mode 100644 index 00000000..df922d01 --- /dev/null +++ b/libs/sysplugins/internal.compile_templateclose.php @@ -0,0 +1,44 @@ +compiler = $compiler; + $this->compiler->has_code = false; + // turn off block code extraction + $compiler->template->extract_code = false; + // check and get attributes + $this->optional_attributes = array('name'); + $_attr = $this->_get_attributes($args); + $saved_data = $this->_close_tag(array('template')); + // if name does match to opening tag + if (isset($_attr['name']) && $saved_data[0]['name'] != $_attr['name']) { + $this->compiler->trigger_template_error('mismatching name attributes "' . $saved_data[0]['name'] . '" and "' . $_attr['name'] . '"'); + } + $_name = trim($saved_data[0]['name'], "'"); + $compiler->template->properties['template'][$_name]['compiled'] = str_replace("\n",'_%n',$compiler->template->extracted_compiled_code); + $compiler->template->extracted_compiled_code = $saved_data[1]; + $compiler->template->extract_code = $saved_data[2]; + return true; + } +} + +?> diff --git a/libs/sysplugins/internal.template.php b/libs/sysplugins/internal.template.php index d711702f..40248173 100644 --- a/libs/sysplugins/internal.template.php +++ b/libs/sysplugins/internal.template.php @@ -59,7 +59,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { // storage for plugin public $plugin_data = array(); // files template is depending from - public $file_dependency = array(); + public $properties = array(); // storage for block data public $block_data = array(); @@ -89,7 +89,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { $this->caching_type = $this->smarty->default_caching_type; $this->security = $this->smarty->security; $this->cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($this->caching_type); - $this->parent = $_parent; + $this->parent = $_parent; // Template resource $this->template_resource = $template_resource; // parse resource name @@ -194,15 +194,18 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { public function mustCompile () { if ($this->mustCompile === null) { - $this->mustCompile = ($this->usesCompiler() && ($this->force_compile || $this->isEvaluated() || ($this->smarty->compile_check && $this->getCompiledTimestamp () !== $this->getTemplateTimestamp ()))); + $this->mustCompile = ($this->usesCompiler() && ($this->force_compile || $this->isEvaluated() || ($this->smarty->compile_check && $this->getCompiledTimestamp () !== $this->getTemplateTimestamp ()))); + if ($this->mustCompile) { + return true; + } // read compiled template if ($this->compiled_template !== true && file_exists($this->getCompiledFilepath())) { $this->compiled_template = !$this->isEvaluated() ? file_get_contents($this->getCompiledFilepath()):''; $found = preg_match('~\<\?php /\*(.*)\*/ \?\>~', $this->compiled_template, $matches); if ($found) { - $this->file_dependency = unserialize($matches[1]); - if (!empty($this->file_dependency)) { - foreach ($this->file_dependency['file_dependency'] as $file_to_check) { + $_properties = unserialize($matches[1]); + if (!empty($_properties['file_dependency'])) { + foreach ($_properties['file_dependency'] as $file_to_check) { If (filemtime($file_to_check[0]) != $file_to_check[1]) { $this->mustCompile = true; return $this->mustCompile; @@ -284,9 +287,9 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { // compiling succeded if (!$this->isEvaluated()) { // build file dependency string - $this->file_dependency_string = 'file_dependency) . "*/ ?>\n"; + $this->properties_string = 'properties) . "*/ ?>\n"; // write compiled template - $this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->file_dependency_string . $this->dir_acc_sec_string . $this->getCompiledTemplate()); + $this->smarty->write_file_object->writeFile($this->getCompiledFilepath(), $this->properties_string . $this->dir_acc_sec_string . $this->getCompiledTemplate()); // make template and compiled file timestamp match touch($this->getCompiledFilepath(), $this->getTemplateTimestamp()); } @@ -344,8 +347,8 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { public function writeCachedContent () { // build file dependency string - $this->file_dependency_string = 'file_dependency) . "*/ ?>\n"; - $this->rendered_content = $this->file_dependency_string . $this->dir_acc_sec_string . $this->rendered_content; + $this->properties_string = 'properties) . "*/ ?>\n"; + $this->rendered_content = $this->properties_string . $this->dir_acc_sec_string . $this->rendered_content; return ($this->isEvaluated() || !$this->caching) ? false : $this->cacher_object->writeCachedContent($this); } diff --git a/libs/sysplugins/internal.templatecompilerbase.php b/libs/sysplugins/internal.templatecompilerbase.php index bc289e3a..21265c96 100644 --- a/libs/sysplugins/internal.templatecompilerbase.php +++ b/libs/sysplugins/internal.templatecompilerbase.php @@ -98,8 +98,8 @@ class Smarty_Internal_TemplateCompilerBase extends Smarty_Internal_Base { /** * Compile Tag * - * This is a call back from the lexer/parser - * It executes the required compile plugin for the Smarty tag + * This is a call back from the lexer/parser + * It executes the required compile plugin for the Smarty tag * * @param string $tag tag name * @param array $args array with tag attributes @@ -112,6 +112,11 @@ class Smarty_Internal_TemplateCompilerBase extends Smarty_Internal_Base { $this->has_code = true; $this->has_output = false; // compile the smarty tag (required compile classes to compile the tag are autoloaded) + if (isset($this->template->properties['template'][$tag])) { + // template defined by {template} tag + $args['name'] = $tag; + $tag = 'templatecall'; + } if (!($_output = $this->$tag($args, $this)) === false) { if ($_output !== true) { // did we get compiled code