- allow multiple template inheritance branches starting in subtemplates

This commit is contained in:
Uwe.Tews
2010-09-08 17:40:00 +00:00
parent a2f50cebd4
commit 550a416209
6 changed files with 46 additions and 48 deletions

View File

@@ -1,3 +1,6 @@
08/09/2010
- allow multiple template inheritance branches starting in subtemplates
07/09/2010 07/09/2010
- bugfix {counter} and {cycle} plugin assigned result to smarty variable not in local(template) scope - bugfix {counter} and {cycle} plugin assigned result to smarty variable not in local(template) scope
- bugfix templates containing just {strip} {/strip} tags did produce an error - bugfix templates containing just {strip} {/strip} tags did produce an error

View File

@@ -223,9 +223,6 @@ class Smarty extends Smarty_Internal_Data {
public $_dir_perms = 0771; public $_dir_perms = 0771;
// smarty object reference // smarty object reference
public $smarty = null; public $smarty = null;
// block data at template inheritance
public $block_data = array();
public $block_data_stack = array();
// block tag hierarchy // block tag hierarchy
public $_tag_stack = array(); public $_tag_stack = array();
// plugins // plugins
@@ -308,8 +305,6 @@ class Smarty extends Smarty_Internal_Data {
// get default Smarty data object // get default Smarty data object
$parent = $this; $parent = $this;
} }
array_push($this->block_data_stack, $this->block_data);
$this->block_data = array();
// create template object if necessary // create template object if necessary
($template instanceof $this->template_class)? $_template = $template : ($template instanceof $this->template_class)? $_template = $template :
$_template = $this->createTemplate ($template, $cache_id, $compile_id, $parent); $_template = $this->createTemplate ($template, $cache_id, $compile_id, $parent);
@@ -352,11 +347,9 @@ class Smarty extends Smarty_Internal_Data {
if ($this->debugging) { if ($this->debugging) {
Smarty_Internal_Debug::display_debug($this); Smarty_Internal_Debug::display_debug($this);
} }
$this->block_data = array_pop($this->block_data_stack);
return; return;
} else { } else {
// return fetched content // return fetched content
$this->block_data = array_pop($this->block_data_stack);
return $_output; return $_output;
} }
} }

View File

@@ -66,10 +66,10 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
$this->compiler->trigger_template_error('mismatching name attributes "' . $saved_data[0]['name'] . '" and "' . $_attr['name'] . '"'); $this->compiler->trigger_template_error('mismatching name attributes "' . $saved_data[0]['name'] . '" and "' . $_attr['name'] . '"');
} }
$_name = trim($saved_data[0]['name'], "\"'"); $_name = trim($saved_data[0]['name'], "\"'");
if (isset($this->smarty->block_data[$_name])) { if (isset($compiler->template->block_data[$_name])) {
$_tpl = $this->smarty->createTemplate('string:' . $this->smarty->block_data[$_name]['source'], null, null, $compiler->template); $_tpl = $this->smarty->createTemplate('string:' . $compiler->template->block_data[$_name]['source'], null, null, $compiler->template);
$_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash']; $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
$_tpl->template_filepath = $this->smarty->block_data[$_name]['file']; $_tpl->template_filepath = $compiler->template->block_data[$_name]['file'];
if ($compiler->nocache) { if ($compiler->nocache) {
$_tpl->forceNocache = 2; $_tpl->forceNocache = 2;
} else { } else {
@@ -77,13 +77,13 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
} }
$_tpl->suppressHeader = true; $_tpl->suppressHeader = true;
$_tpl->suppressFileDependency = true; $_tpl->suppressFileDependency = true;
if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) { if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
$_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->getCompiledTemplate()); $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->getCompiledTemplate());
} elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') { } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
$_output = $_tpl->getCompiledTemplate() . $compiler->parser->current_buffer->to_smarty_php(); $_output = $_tpl->getCompiledTemplate() . $compiler->parser->current_buffer->to_smarty_php();
} elseif ($this->smarty->block_data[$_name]['mode'] == 'append') { } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
$_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->getCompiledTemplate(); $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->getCompiledTemplate();
} elseif (!empty($this->smarty->block_data[$_name])) { } elseif (!empty($compiler->template->block_data[$_name])) {
$_output = $_tpl->getCompiledTemplate(); $_output = $_tpl->getCompiledTemplate();
} }
$compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']); $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);

View File

@@ -79,35 +79,35 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_CompileBase {
$_name = trim($_match[3], '\'"'); $_name = trim($_match[3], '\'"');
// replace {$smarty.block.child} // replace {$smarty.block.child}
if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) { if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) {
if (isset($this->smarty->block_data[$_name])) { if (isset($template->block_data[$_name])) {
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter, $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
$this->smarty->block_data[$_name]['source'], $block_content); $template->block_data[$_name]['source'], $block_content);
unset($this->smarty->block_data[$_name]); unset($template->block_data[$_name]);
} else { } else {
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter, $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
'', $block_content); '', $block_content);
} }
} }
if (isset($this->smarty->block_data[$_name])) { if (isset($template->block_data[$_name])) {
if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) { if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
$this->smarty->block_data[$_name]['source'] = $template->block_data[$_name]['source'] =
str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']); str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
} elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') { } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
$this->smarty->block_data[$_name]['source'] .= $block_content; $template->block_data[$_name]['source'] .= $block_content;
} elseif ($this->smarty->block_data[$_name]['mode'] == 'append') { } elseif ($template->block_data[$_name]['mode'] == 'append') {
$this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source']; $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
} }
} else { } else {
$this->smarty->block_data[$_name]['source'] = $block_content; $template->block_data[$_name]['source'] = $block_content;
} }
if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) { if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
$this->smarty->block_data[$_name]['mode'] = 'append'; $template->block_data[$_name]['mode'] = 'append';
} elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) { } elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
$this->smarty->block_data[$_name]['mode'] = 'prepend'; $template->block_data[$_name]['mode'] = 'prepend';
} else { } else {
$this->smarty->block_data[$_name]['mode'] = 'replace'; $template->block_data[$_name]['mode'] = 'replace';
} }
$this->smarty->block_data[$_name]['file'] = $template->getTemplateFilepath(); $template->block_data[$_name]['file'] = $template->getTemplateFilepath();
} }
} }
} }

View File

@@ -119,7 +119,7 @@ class Smarty_Internal_Resource_Extends {
} }
$_block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.parent' . $this->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%', $_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]))); 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); $this->saveBlockData($_block_content, $_result[0][$_start][0], $_filepath, $_template);
$_start = $_start + $_end + 1; $_start = $_start + $_end + 1;
} }
} else { } else {
@@ -133,7 +133,7 @@ class Smarty_Internal_Resource_Extends {
/** /**
* saveBlockData * saveBlockData
*/ */
protected function saveBlockData($block_content, $block_tag, $_filepath) protected function saveBlockData($block_content, $block_tag, $_filepath, $_template)
{ {
if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) { if (0 == preg_match("!(.?)(name=)(.*?)(?=(\s|{$this->_rdl}))!", $block_tag, $_match)) {
$this->smarty->trigger_error("'{$block_tag}' missing name attribute in file '$_filepath'"); $this->smarty->trigger_error("'{$block_tag}' missing name attribute in file '$_filepath'");
@@ -141,35 +141,35 @@ class Smarty_Internal_Resource_Extends {
$_name = trim($_match[3], '\'"'); $_name = trim($_match[3], '\'"');
// replace {$smarty.block.child} // replace {$smarty.block.child}
if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) { if (strpos($block_content, $this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter) !== false) {
if (isset($this->smarty->block_data[$_name])) { if (isset($_template->block_data[$_name])) {
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter, $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
$this->smarty->block_data[$_name]['source'], $block_content); $_template->block_data[$_name]['source'], $block_content);
unset($this->smarty->block_data[$_name]); unset($_template->block_data[$_name]);
} else { } else {
$block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter, $block_content = str_replace($this->smarty->left_delimiter . '$smarty.block.child' . $this->smarty->right_delimiter,
'', $block_content); '', $block_content);
} }
} }
if (isset($this->smarty->block_data[$_name])) { if (isset($_template->block_data[$_name])) {
if (strpos($this->smarty->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) { if (strpos($_template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
$this->smarty->block_data[$_name]['source'] = $_template->block_data[$_name]['source'] =
str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $this->smarty->block_data[$_name]['source']); str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $_template->block_data[$_name]['source']);
} elseif ($this->smarty->block_data[$_name]['mode'] == 'prepend') { } elseif ($_template->block_data[$_name]['mode'] == 'prepend') {
$this->smarty->block_data[$_name]['source'] .= $block_content; $_template->block_data[$_name]['source'] .= $block_content;
} elseif ($this->smarty->block_data[$_name]['mode'] == 'append') { } elseif ($_template->block_data[$_name]['mode'] == 'append') {
$this->smarty->block_data[$_name]['source'] = $block_content . $this->smarty->block_data[$_name]['source']; $_template->block_data[$_name]['source'] = $block_content . $_template->block_data[$_name]['source'];
} }
} else { } else {
$this->smarty->block_data[$_name]['source'] = $block_content; $_template->block_data[$_name]['source'] = $block_content;
} }
if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) { if (preg_match('/(.?)(append)(.*)/', $block_tag, $_match) != 0) {
$this->smarty->block_data[$_name]['mode'] = 'append'; $_template->block_data[$_name]['mode'] = 'append';
} elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) { } elseif (preg_match('/(.?)(prepend)(.*)/', $block_tag, $_match) != 0) {
$this->smarty->block_data[$_name]['mode'] = 'prepend'; $_template->block_data[$_name]['mode'] = 'prepend';
} else { } else {
$this->smarty->block_data[$_name]['mode'] = 'replace'; $_template->block_data[$_name]['mode'] = 'replace';
} }
$this->smarty->block_data[$_name]['file'] = $_filepath; $_template->block_data[$_name]['file'] = $_filepath;
} }
} }

View File

@@ -68,6 +68,8 @@ class Smarty_Internal_Template extends Smarty_Internal_Data {
public $security = false; public $security = false;
public $saved_modifier = null; public $saved_modifier = null;
public $smarty = null; public $smarty = null;
// blocks for template inheritance
public $block_data = array();
/** /**
* Create template data object * Create template data object
* *