- 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 ===== ===== trunk =====
26.07.2013
- bugfix template inheritance nesting problem (forum topic 24387)
15.7.2013 15.7.2013
- update code generated by PSR-2 standards fixer which introduced PHP 5.4 incompatibilities of 14.7.2013 - update code generated by PSR-2 standards fixer which introduced PHP 5.4 incompatibilities of 14.7.2013

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* Smarty Internal Plugin Compile Block * Smarty Internal Plugin Compile Block
* *
* Compiles the {block}{/block} tags * Compiles the {block}{/block} tags
@@ -10,14 +10,14 @@
* @author Uwe Tews * @author Uwe Tews
*/ */
/** /**
* Smarty Internal Plugin Compile Block Class * Smarty Internal Plugin Compile Block Class
* *
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
*/ */
class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
{ {
/** /**
* Attribute definition: Overwrites base class. * Attribute definition: Overwrites base class.
* *
@@ -79,6 +79,9 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
*/ */
static function saveBlockData($block_content, $block_tag, $template, $filepath) static function saveBlockData($block_content, $block_tag, $template, $filepath)
{ {
$nesting = array();
$hide_level = 0;
$child_flag = false;
$_rdl = preg_quote($template->smarty->right_delimiter); $_rdl = preg_quote($template->smarty->right_delimiter);
$_ldl = preg_quote($template->smarty->left_delimiter); $_ldl = preg_quote($template->smarty->left_delimiter);
if (!$template->smarty->auto_literal) { if (!$template->smarty->auto_literal) {
@@ -91,44 +94,63 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
throw new SmartyCompilerException($error_text); throw new SmartyCompilerException($error_text);
} else { } else {
$_name = trim($_match[3], '\'"'); $_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 // 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)) { 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);
foreach ($_match2[3] as $key => $name) { $block_content = '';
// get it's replacement foreach ($_match2[0] as $key => $text) {
$_name2 = trim($name, '\'"'); // {block} tag
if ($_match2[5][$key] != 'hide' || isset($template->block_data[$_name2])) { if (!empty($_match2[3][$key])) {
if (isset($template->block_data[$_name2])) { $name = trim($_match2[3][$key], '\'"');
$replacement = $template->block_data[$_name2]['source']; 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 { } 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 { } else {
// remove hidden blocks $block_content = $content . $block_content;
$block_content = preg_replace("!(({$_ldl}{$al}block)(.*)?{$name}(.*)?({$_rdl}[\s\S]*?{$_ldl}{$al}/block\s*{$_rdl}))!", '', $block_content); $cf = true;
} }
$child_flag = $cf;
if (count($nesting) < $hide_level) {
$hide_level = 0;
} }
continue;
} }
// do we have not nested {$smart.block.child} // hide option active
if (0 != preg_match("!({$_ldl}{$al}\\\$smarty\.block\.child\s*{$_rdl})!", $block_content, $_match2)) { if ($hide_level && $hide_level <= count($nesting)) {
// get child replacement for this block continue;
if (isset($template->block_data[$_name])) {
$replacement = $template->block_data[$_name]['source'];
unset($template->block_data[$_name]);
} else {
$replacement = '';
} }
$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 (isset($template->block_data[$_name])) {
if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) { if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
$template->block_data[$_name]['source'] = $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]['source'] = $block_content;
$template->block_data[$_name]['file'] = $filepath; $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') { if ($_match[6] == 'append') {
$template->block_data[$_name]['mode'] = 'append'; $template->block_data[$_name]['mode'] = 'append';
} elseif ($_match[6] == 'prepend') { } elseif ($_match[6] == 'prepend') {
@@ -151,7 +177,6 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
} }
} }
} }
}
/** /**
* Compile saved child block source * 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 * @param string $_name optional name of child block
* @return string compiled code of schild block * @return string compiled code of schild block
*/ */
public static function compileChildBlock($compiler, $_name = null) static function compileChildBlock($compiler, $_name = null)
{ {
$_output = ''; $_output = '';
// if called by {$smarty.block.child} we must search the name of enclosing {block} // if called by {$smarty.block.child} we must search the name of enclosing {block}
@@ -230,16 +255,16 @@ class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase
return $_output; return $_output;
} }
} }
/** /**
* Smarty Internal Plugin Compile BlockClose Class * Smarty Internal Plugin Compile BlockClose Class
* *
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
*/ */
class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase
{ {
/** /**
* Compiles code for the {/block} tag * Compiles code for the {/block} tag
* *
@@ -285,4 +310,4 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase
return $_output; return $_output;
} }
} }