From 971058fc6185cad1c24dc4ff2959c5634fd17175 Mon Sep 17 00:00:00 2001 From: uwetews Date: Mon, 2 May 2016 00:58:17 +0200 Subject: [PATCH] - enhancement {block} tag names can now be variable https://github.com/smarty-php/smarty/issues/221 --- INHERITANCE_RELEASE_NOTES.txt | 7 ++++++- change_log.txt | 3 +++ libs/Smarty.class.php | 2 +- libs/sysplugins/smarty_internal_block.php | 3 ++- libs/sysplugins/smarty_internal_compile_block.php | 15 +++++++-------- 5 files changed, 19 insertions(+), 11 deletions(-) diff --git a/INHERITANCE_RELEASE_NOTES.txt b/INHERITANCE_RELEASE_NOTES.txt index 03044dea2..0d772385c 100644 --- a/INHERITANCE_RELEASE_NOTES.txt +++ b/INHERITANCE_RELEASE_NOTES.txt @@ -5,7 +5,7 @@ This does resolve all known existing restrictions (see below). The $smarty::$inheritance_merge_compiled_includes property has been removed. Any access to it is ignored. -This does enable some new features: +New features: Any code outside root {block} tags in child templates is now executed but any output will be ignored. @@ -31,6 +31,11 @@ Any code outside root {block} tags in child templates is now executed but any ou {/if} {/block} +{block} tags can have variable names. + + {block $foo} + .... + {/block} Starting with 3.1.28 you can mix inheritance by extends resuorce with the {extends} tag. A template called by extends resoure can extend a subtemple or chain buy the {extends} tag. diff --git a/change_log.txt b/change_log.txt index eaefb5ffa..e0c6cc47f 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,4 +1,7 @@  ===== 3.1.30-dev ===== (xx.xx.xx) + 02.05.2026 + - enhancement {block} tag names can now be variable https://github.com/smarty-php/smarty/issues/221 + 01.05.2016 - bugfix same relative filepath at {include} called from template in different folders could display wrong sub-template diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index f734e3c64..1241b337b 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase /** * smarty version */ - const SMARTY_VERSION = '3.1.30-dev/64'; + const SMARTY_VERSION = '3.1.30-dev/65'; /** * define variable scopes diff --git a/libs/sysplugins/smarty_internal_block.php b/libs/sysplugins/smarty_internal_block.php index 9a67757b9..20ca3d026 100644 --- a/libs/sysplugins/smarty_internal_block.php +++ b/libs/sysplugins/smarty_internal_block.php @@ -80,8 +80,9 @@ class Smarty_Internal_Block * @param \Smarty_Internal_Template $tpl * @param int|null $tplIndex index of outer level {block} if nested */ - public function __construct(Smarty_Internal_Template $tpl, $tplIndex = null) + public function __construct(Smarty_Internal_Template $tpl, $name, $tplIndex = null) { + $this->name = $name; $inheritance = &$tpl->ext->_inheritance; $this->tplIndex = $tplIndex ? $tplIndex : $inheritance->tplIndex; if (isset($inheritance->childRoot[ $this->name ])) { diff --git a/libs/sysplugins/smarty_internal_compile_block.php b/libs/sysplugins/smarty_internal_compile_block.php index 0d3c59ba5..c84f7d878 100644 --- a/libs/sysplugins/smarty_internal_compile_block.php +++ b/libs/sysplugins/smarty_internal_compile_block.php @@ -86,7 +86,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ $_attr = $this->getAttributes($compiler, $args); $compiler->_cache[ 'blockNesting' ] ++; $compiler->_cache[ 'blockName' ][ $compiler->_cache[ 'blockNesting' ] ] = $_attr[ 'name' ]; - $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ][ 'name' ] = "{$_attr['name']}"; + $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ] = array(); $this->openTag($compiler, 'block', array($_attr, $compiler->nocache, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code, $compiler->template->caching)); @@ -167,7 +167,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ // init block parameter $_block = $compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]; unset($compiler->_cache[ 'blockParams' ][ $compiler->_cache[ 'blockNesting' ] ]); - $_name = trim($_attr[ 'name' ], "'\""); + $_name = $_attr[ 'name' ]; $_assign = isset($_attr[ 'assign' ]) ? $_attr[ 'assign' ] : null; unset($_attr[ 'assign' ], $_attr[ 'name' ]); foreach ($_attr as $name => $stat) { @@ -175,8 +175,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ $_block[ $name ] = 'true'; } } - $_className = 'Block_' . preg_replace('#[^\w\|]+#S', '_', $_name) . '_' . - preg_replace('![^\w]+!', '_', uniqid(rand(), true)); + $_className = 'Block_' . preg_replace('![^\w]+!', '_', uniqid(rand(), true)); // get compiled block code $_functionCode = $compiler->parser->current_buffer; // setup buffer for template function code @@ -190,7 +189,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ } $output = " $value) { @@ -215,7 +214,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ } $output .= "}\n"; $output .= "}\n"; - $output .= "/* {/block '{$_name}'} */\n\n"; + $output .= "/* {/block {$_name}} */\n\n"; $output .= "?>\n"; $compiler->parser->current_buffer->append_subtree($compiler->parser, new Smarty_Internal_ParseTree_Tag($compiler->parser, @@ -239,9 +238,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $ $compiler->parser->current_buffer = $_buffer; $output = "_cache[ 'blockNesting' ] == 1) { - $output .= "new {$_className}(\$_smarty_tpl);\n"; + $output .= "new {$_className}(\$_smarty_tpl, $_name);\n"; } else { - $output .= "new {$_className}(\$_smarty_tpl, \$this->tplIndex);\n"; + $output .= "new {$_className}(\$_smarty_tpl, $_name, \$this->tplIndex);\n"; } $output .= "?>\n"; $compiler->_cache[ 'blockNesting' ] --;