- improvement new Smarty::$extends_recursion property to disable execution of {extends} in templates called by extends resource

https://github.com/smarty-php/smarty/issues/296
This commit is contained in:
uwetews
2016-09-29 04:22:20 +02:00
parent 5b508b7bf3
commit 4de72f1acd
4 changed files with 49 additions and 11 deletions

View File

@@ -432,6 +432,14 @@ class Smarty extends Smarty_Internal_TemplateBase
*/ */
public $merge_compiled_includes = false; public $merge_compiled_includes = false;
/*
* flag for behaviour when extends: resource and {extends} tag are used simultaneous
* if false disable execution of {extends} in templates called by extends resource.
* (behaviour as versions < 3.1.28)
*
* @var boolean
*/
public $extends_recursion = true;
/** /**
* force cache file creation * force cache file creation
* *

View File

@@ -84,8 +84,7 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_Compile_Shared_Inh
$this->compileEndChild($compiler); $this->compileEndChild($compiler);
} }
} else { } else {
$this->compileEndChild($compiler); $this->compileEndChild($compiler, $_attr[ 'file' ]);
$this->compileInclude($compiler, $_attr[ 'file' ]);
} }
$compiler->has_code = false; $compiler->has_code = false;
return ''; return '';
@@ -95,24 +94,35 @@ class Smarty_Internal_Compile_Extends extends Smarty_Internal_Compile_Shared_Inh
* Add code for inheritance endChild() method to end of template * Add code for inheritance endChild() method to end of template
* *
* @param \Smarty_Internal_TemplateCompilerBase $compiler * @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param null|string $template optional inheritance parent template
*/ */
private function compileEndChild(Smarty_Internal_TemplateCompilerBase $compiler) private function compileEndChild(Smarty_Internal_TemplateCompilerBase $compiler, $template = null)
{ {
$inlineUids = '';
if (isset($template) && $compiler->smarty->merge_compiled_includes) {
$code = $compiler->compileTag('include', array($template, array('scope' => 'parent')));
if (preg_match("/([,][\s]*['][a-z0-9]+['][,][\s]*[']content.*['])[)]/", $code, $match)) {
$inlineUids = $match[ 1 ];
}
}
$compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser, $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
"<?php \$_smarty_tpl->inheritance->endChild();\n?>\n"); "<?php \$_smarty_tpl->inheritance->endChild(\$_smarty_tpl" .
(isset($template) ?
', ' . $template . $inlineUids :
'') . ");\n?>\n");
} }
/** /**
* Add code for including subtemplate to end of template * Add code for including subtemplate to end of template
* *
* @param \Smarty_Internal_TemplateCompilerBase $compiler * @param \Smarty_Internal_TemplateCompilerBase $compiler
* @param string $file subtemplate name * @param string $template subtemplate name
*/ */
private function compileInclude(Smarty_Internal_TemplateCompilerBase $compiler, $file) private function compileInclude(Smarty_Internal_TemplateCompilerBase $compiler, $template)
{ {
$compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser, $compiler->parser->template_postfix[] = new Smarty_Internal_ParseTree_Tag($compiler->parser,
$compiler->compileTag('include', $compiler->compileTag('include',
array($file, array($template,
array('scope' => 'parent')))); array('scope' => 'parent'))));
} }

View File

@@ -99,14 +99,22 @@ class Smarty_Internal_Runtime_Inheritance
* End of child template(s) * End of child template(s)
* - if outer level is reached flush output buffer and switch to wait for parent template state * - if outer level is reached flush output buffer and switch to wait for parent template state
* *
* @param \Smarty_Internal_Template $tpl
* @param null|string $template optinal name of inheritance parent template
* @param null|string $uid uid of inline template
* @param null|string $func function call name of inline template
*/ */
public function endChild() public function endChild(Smarty_Internal_Template $tpl, $template = null, $uid = null, $func = null)
{ {
$this->inheritanceLevel --; $this->inheritanceLevel --;
if (!$this->inheritanceLevel) { if (!$this->inheritanceLevel) {
ob_end_clean(); ob_end_clean();
$this->state = 2; $this->state = 2;
} }
if (isset($template) && ($tpl->parent->source->type !== 'extends' || $tpl->smarty->extends_recursion)) {
$tpl->_subTemplateRender($template, $tpl->cache_id, $tpl->compile_id, $tpl->caching ? 9999 : 0,
$tpl->cache_lifetime, array(), 2, false, $uid, $func);
}
} }
/** /**
@@ -197,11 +205,22 @@ class Smarty_Internal_Runtime_Inheritance
* @param \Smarty_Internal_Template $tpl * @param \Smarty_Internal_Template $tpl
* @param \Smarty_Internal_Block $block * @param \Smarty_Internal_Block $block
* *
* @param null $name
*
* @throws \SmartyException * @throws \SmartyException
*/ */
public function callParent(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block) public function callParent(Smarty_Internal_Template $tpl, Smarty_Internal_Block $block, $name = null)
{ {
if (isset($block->parent)) { if (isset($name)) {
$block = $block->parent;
while (isset($block)) {
if (isset($block->subBlocks[ $name ])) {
} else {
$block = $block->parent;
}
}
return;
} else if (isset($block->parent)) {
$this->callBlock($block->parent, $tpl); $this->callBlock($block->parent, $tpl);
} else { } else {
throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'"); throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$tpl->inheritance->sources[$block->tplIndex]->filepath}' block '{$block->name}'");

View File

@@ -61,7 +61,8 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
$this->filepath .= (int) $smarty->config_read_hidden + (int) $smarty->config_booleanize * 2 + $this->filepath .= (int) $smarty->config_read_hidden + (int) $smarty->config_booleanize * 2 +
(int) $smarty->config_overwrite * 4; (int) $smarty->config_overwrite * 4;
} else { } else {
$this->filepath .= (int) $smarty->merge_compiled_includes + (int) $smarty->escape_html * 2; $this->filepath .= (int) $smarty->merge_compiled_includes + (int) $smarty->escape_html * 2 +
(($smarty->merge_compiled_includes && $source->type === 'extends') ? (int) $smarty->extends_recursion * 4 : 0);
} }
$this->filepath .= '.' . $source->type; $this->filepath .= '.' . $source->type;
$basename = $source->handler->getBasename($source); $basename = $source->handler->getBasename($source);