- bugfix on nested {block} tags

- changed Smarty special variable $smarty.parent to $smarty.block.parent
This commit is contained in:
Uwe.Tews
2010-02-12 15:56:17 +00:00
parent 99ab6e96a5
commit 78c2e92f19
4 changed files with 40 additions and 27 deletions

4
README
View File

@@ -460,7 +460,7 @@ Child title
grandchild.tpl:
{extends file='child.tpl'}
{block name='title'}Home - {$smarty.parent}{/block}
{block name='title'}Home - {$smarty.block.parent}{/block}
{block name='page-title'}My home{/block}
{block name='content'}
{foreach $images as $img}
@@ -468,7 +468,7 @@ grandchild.tpl:
{/foreach}
{/block}
We redefined all the blocks here, however in the title block we used {$smarty.parent},
We redefined all the blocks here, however in the title block we used {$smarty.block.parent},
which tells Smarty to insert the default content from the parent template in its place.
The content block was overriden to display the image files, and page-title has also be
overriden to display a completely different title.

View File

@@ -1,4 +1,6 @@
12/02/2010
- bugfix on nested {block} tags
- changed Smarty special variable $smarty.parent to $smarty.block.parent
- added support of nested {bock} tags
10/02/2010

View File

@@ -36,23 +36,29 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
$_template = new $compiler->smarty->template_class($include_file, $this->smarty, $compiler->template);
// save file dependency
$compiler->template->properties['file_dependency'][sha1($_template->getTemplateFilepath())] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
$_old_source = $compiler->template->template_source;
if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_old_source, $s, PREG_OFFSET_CAPTURE) !=
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_old_source, $c, PREG_OFFSET_CAPTURE)) {
$_content = $compiler->template->template_source;
if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $s) !=
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $c)) {
$this->compiler->trigger_template_error('unmatched {block} {/block} pairs');
}
preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_old_source, $_result, PREG_OFFSET_CAPTURE);
preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
$_result_count = count($_result[0]);
$_i = 0;
while ($_i < $_result_count) {
$_ii = 1;
while (!strpos($_result[0][$_i + $_ii][0], '/')) {
$_ii++;
$_start = 0;
while ($_start < $_result_count) {
$_end = 0;
$_level = 1;
while ($_level != 0) {
$_end++;
if (!strpos($_result[0][$_start + $_end][0], '/')) {
$_level++;
} else {
$_level--;
}
}
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
substr($_old_source, $_result[0][$_i][1] + strlen($_result[0][$_i][0]), $_result[0][$_i-1 + 2 * $_ii][1] - $_result[0][$_i][1] - + strlen($_result[0][$_i][0])));
$this->saveBlockData($_block_content, $_result[0][$_i][0], $compiler->template);
$_i = $_i + 2 * $_ii;
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
$this->saveBlockData($_block_content, $_result[0][$_start][0], $compiler->template);
$_start = $_start + $_end + 1;
}
$compiler->template->template_source = $_template->getTemplateSource();
$compiler->template->template_filepath = $_template->getTemplateFilepath();

View File

@@ -98,22 +98,28 @@ class Smarty_Internal_Resource_Extends {
$_template->template_filepath = $_filepath;
$_content = file_get_contents($_filepath);
if ($_filepath != $_files[count($_files)-1]) {
if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $_open, PREG_OFFSET_CAPTURE) !=
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $_close, PREG_OFFSET_CAPTURE)) {
if (preg_match_all("!({$this->_ldl}block(.+?){$this->_rdl})!", $_content, $_open) !=
preg_match_all("!({$this->_ldl}/block(.*?){$this->_rdl})!", $_content, $_close)) {
$this->smarty->trigger_error("unmatched {block} {/block} pairs in file '$_filepath'");
}
preg_match_all("!{$this->_ldl}block(.+?){$this->_rdl}|{$this->_ldl}/block.*{$this->_rdl}!", $_content, $_result, PREG_OFFSET_CAPTURE);
$_result_count = count($_result[0]);
$_i = 0;
while ($_i < $_result_count) {
$_ii = 1;
while (!strpos($_result[0][$_i + $_ii][0], '/')) {
$_ii++;
$_start = 0;
while ($_start < $_result_count) {
$_end = 0;
$_level = 1;
while ($_level != 0) {
$_end++;
if (!strpos($_result[0][$_start + $_end][0], '/')) {
$_level++;
} else {
$_level--;
}
}
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
substr($_content, $_result[0][$_i][1]+strlen($_result[0][$_i][0]), $_result[0][$_i-1 + 2 * $_ii][1] - $_result[0][$_i][1] - +strlen($_result[0][$_i][0])));
$this->saveBlockData($_block_content, $_result[0][$_i][0], $_filepath);
$_i = $_i + 2 * $_ii;
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
substr($_content, $_result[0][$_start][1] + strlen($_result[0][$_start][0]), $_result[0][$_start + $_end][1] - $_result[0][$_start][1] - + strlen($_result[0][$_start][0])));
$this->saveBlockData($_block_content, $_result[0][$_start][0], $_filepath);
$_start = $_start + $_end + 1;
}
} else {
$_template->template_source = $_content;
@@ -189,5 +195,4 @@ class Smarty_Internal_Resource_Extends {
return $_compile_dir . $_filepath . '.' . $_template->resource_type . '.' . basename($_files[count($_files)-1]) . $_cache . '.php';
}
}
?>