- bugfix template inheritance nesting problem (forum topic 24387)

This commit is contained in:
Uwe.Tews@googlemail.com
2013-07-26 13:50:44 +00:00
parent 584886cb1c
commit f83eba920d
2 changed files with 272 additions and 244 deletions

View File

@@ -1,4 +1,7 @@
===== trunk =====
26.07.2013
- bugfix template inheritance nesting problem (forum topic 24387)
15.7.2013
- update code generated by PSR-2 standards fixer which introduced PHP 5.4 incompatibilities of 14.7.2013

View File

@@ -79,6 +79,9 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
*/
static function saveBlockData($block_content, $block_tag, $template, $filepath)
{
$nesting = array();
$hide_level = 0;
$child_flag = false;
$_rdl = preg_quote($template->smarty->right_delimiter);
$_ldl = preg_quote($template->smarty->left_delimiter);
if (!$template->smarty->auto_literal) {
@@ -91,44 +94,63 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
throw new SmartyCompilerException($error_text);
} else {
$_name = trim($_match[3], '\'"');
if ($_match[8] != 'hide' || isset($template->block_data[$_name])) { // replace {$smarty.block.child}
// replace {$smarty.block.child}
// get nested block tags
if (0 != preg_match_all("!({$_ldl}{$al}block\s+)(name=)?(\w+|'.*'|\".*\")([\s\S]*?)(hide)?(\s*{$_rdl})([\s\S]*?)(.*)?({$_ldl}{$al}/block\s*{$_rdl})!", $block_content, $_match2)) {
foreach ($_match2[3] as $key => $name) {
// get it's replacement
$_name2 = trim($name, '\'"');
if ($_match2[5][$key] != 'hide' || isset($template->block_data[$_name2])) {
if (isset($template->block_data[$_name2])) {
$replacement = $template->block_data[$_name2]['source'];
preg_match_all("%({$_ldl}{$al}block\s+)(name=)?(\w+|'.*?'|\".*?\")([\s\S]*?)(hide)?(\s*{$_rdl})|({$_ldl}{$al}/block\s*{$_rdl})|({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})|({$_ldl}|{$_rdl})|([\s\S]*?(?=({$_ldl}|{$_rdl})))%", $block_tag.$block_content."{$template->smarty->left_delimiter}/block{$template->smarty->right_delimiter}", $_match2);
$block_content = '';
foreach ($_match2[0] as $key => $text) {
// {block} tag
if (!empty($_match2[3][$key])) {
$name = trim($_match2[3][$key], '\'"');
array_push($nesting, array($name, $block_content, $child_flag));
$block_content = '';
// hide option
if (!empty($_match2[5][$key]) && $hide_level == 0 && !isset($template->block_data[$name])) {
$hide_level = count($nesting);
}
continue;
}
// {/block} tag
if (!empty($_match2[7][$key])) {
list($name, $content, $cf) = array_pop($nesting);
if (isset($template->block_data[$name]) && !$child_flag) {
if ($template->block_data[$name]['mode'] == 'prepend') {
$block_content = $content . $template->block_data[$name]['source'] . $block_content;
} elseif ($template->block_data[$name]['mode'] == 'append') {
$block_content = $content . $block_content . $template->block_data[$name]['source'];
} else {
$replacement = '';
// check if {$smarty.block.parent} will be later replaced
if (strpos($template->block_data[$name]['source'], '%%%%SMARTY_PARENT%%%%') === false) {
$block_content = $content . $template->block_data[$name]['source'];
}
// replace {$smarty.block.child} tag
if (preg_match("!{$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl}!",$_match2[7][$key])) {
$replacement = preg_replace("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $replacement, $_match2[7][$key]);
$block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl}[\s\S]*?{$_ldl}{$al}/block\s*{$_rdl}))!", $replacement, $block_content);
}
if (preg_match("!{$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl}!",$_match2[8][$key])) {
$replacement = preg_replace("!{$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl}!", $replacement, $_match2[8][$key]);
$block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl})(.*)?({$_ldl}{$al}/block\s*{$_rdl}))!", $replacement, $block_content);
}
} else {
// remove hidden blocks
$block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl}[\s\S]*?{$_ldl}{$al}/block\s*{$_rdl}))!", '', $block_content);
$block_content = $content . $block_content;
$cf = true;
}
$child_flag = $cf;
if (count($nesting) < $hide_level) {
$hide_level = 0;
}
continue;
}
// do we have not nested {$smart.block.child}
if (0 != preg_match("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $block_content, $_match2)) {
// get child replacement for this block
if (isset($template->block_data[$_name])) {
$replacement = $template->block_data[$_name]['source'];
unset($template->block_data[$_name]);
} else {
$replacement = '';
// hide option active
if ($hide_level && $hide_level <= count($nesting)) {
continue;
}
$block_content = preg_replace("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $replacement, $block_content);
// {$smarty.block.child} tag
if (!empty($_match2[8][$key])) {
list($name, $content, $cf) = end($nesting);
if (isset($template->block_data[$name])) {
$child_flag = true;
$block_content .= $template->block_data[$name]['source'];
}
continue;
}
$block_content .= $_match2[0][$key];
}
// $block_content = preg_replace_callback("%({$_ldl}{$al}block\s+)(name=)?(\w+|'.*?'|\".*?\")([\s\S]*?)(hide)?(\s*{$_rdl})|({$_ldl}{$al}/block\s*{$_rdl})|({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})|({$_ldl}|{$_rdl})|([\s\S]*?(?=({$_ldl}|{$_rdl})))%", array('Smarty_Internal_Compile_Block', 'replaceBlockChild'), $block_tag.$block_content."{$template->smarty->left_delimiter}/block{$template->smarty->right_delimiter}");
if (isset($template->block_data[$_name])) {
if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
$template->block_data[$_name]['source'] =
@@ -142,6 +164,10 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
$template->block_data[$_name]['source'] = $block_content;
$template->block_data[$_name]['file'] = $filepath;
}
if ($child_flag) {
$template->block_data[$_name]['source'] = $block_content;
$template->block_data[$_name]['file'] = $filepath;
}
if ($_match[6] == 'append') {
$template->block_data[$_name]['mode'] = 'append';
} elseif ($_match[6] == 'prepend') {
@@ -151,7 +177,6 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
}
}
}
}
/**
* Compile saved child block source
@@ -160,7 +185,7 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
* @param string $_name optional name of child block
* @return string compiled code of schild block
*/
public static function compileChildBlock($compiler, $_name = null)
static function compileChildBlock($compiler, $_name = null)
{
$_output = '';
// if called by {$smarty.block.child} we must search the name of enclosing {block}