- enhancement {block} tag names can now be variable https://github.com/smarty-php/smarty/issues/221

This commit is contained in:
uwetews
2016-05-02 00:58:17 +02:00
parent f1b3662b28
commit 971058fc61
5 changed files with 19 additions and 11 deletions

View File

@@ -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.

View File

@@ -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

View File

@@ -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

View File

@@ -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 ])) {

View File

@@ -86,7 +86,7 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_Compile_Shared_Inher
$_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 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
// 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 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
$_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 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
}
$output = "<?php\n";
$output .= "/* {block '{$_name}'} {$sourceInfo} */\n";
$output .= "/* {block {$_name}} {$sourceInfo} */\n";
$output .= "class {$_className} extends Smarty_Internal_Block\n";
$output .= "{\n";
foreach ($_block as $property => $value) {
@@ -215,7 +214,7 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
}
$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 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
$compiler->parser->current_buffer = $_buffer;
$output = "<?php \n";
if ($compiler->_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' ] --;