Skip to content

Commit

Permalink
- enhancement {block} tag names can now be variable #221
Browse files Browse the repository at this point in the history
  • Loading branch information
uwetews committed May 1, 2016
1 parent f1b3662 commit 971058f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
7 changes: 6 additions & 1 deletion INHERITANCE_RELEASE_NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion libs/sysplugins/smarty_internal_block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ])) {
Expand Down
15 changes: 7 additions & 8 deletions libs/sysplugins/smarty_internal_compile_block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -167,16 +167,15 @@ 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) {
if ((is_bool($stat) && $stat !== false) || (!is_bool($stat) && $stat != 'false')) {
$_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
Expand All @@ -190,7 +189,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
}

$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) {
Expand All @@ -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,
Expand All @@ -239,9 +238,9 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $
$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' ] --;
Expand Down

0 comments on commit 971058f

Please sign in to comment.