Skip to content

Commit

Permalink
- improvement new Smarty::$extends_recursion property to disable exec…
Browse files Browse the repository at this point in the history
…ution of {extends} in templates called by extends resource

     #296
  • Loading branch information
uwetews committed Sep 29, 2016
1 parent 5b508b7 commit 4de72f1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
8 changes: 8 additions & 0 deletions libs/Smarty.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,14 @@ class Smarty extends Smarty_Internal_TemplateBase
*/
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
*
Expand Down
24 changes: 17 additions & 7 deletions libs/sysplugins/smarty_internal_compile_extends.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
$this->compileEndChild($compiler);
}
} else {
$this->compileEndChild($compiler);
$this->compileInclude($compiler, $_attr[ 'file' ]);
$this->compileEndChild($compiler, $_attr[ 'file' ]);
}
$compiler->has_code = false;
return '';
Expand All @@ -95,24 +94,35 @@ public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler)
* Add code for inheritance endChild() method to end of template
*
* @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,
"<?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
*
* @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->compileTag('include',
array($file,
array($template,
array('scope' => 'parent'))));
}

Expand Down
25 changes: 22 additions & 3 deletions libs/sysplugins/smarty_internal_runtime_inheritance.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,22 @@ public function init(Smarty_Internal_Template $tpl, $initChild, $blockNames = ar
* End of child template(s)
* - 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 --;
if (!$this->inheritanceLevel) {
ob_end_clean();
$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);
}
}

/**
Expand Down Expand Up @@ -197,11 +205,22 @@ public function callChild(Smarty_Internal_Template $tpl, Smarty_Internal_Block $
* @param \Smarty_Internal_Template $tpl
* @param \Smarty_Internal_Block $block
*
* @param null $name
*
* @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);
} 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}'");
Expand Down
3 changes: 2 additions & 1 deletion libs/sysplugins/smarty_template_compiled.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public function populateCompiledFilepath(Smarty_Internal_Template $_template)
$this->filepath .= (int) $smarty->config_read_hidden + (int) $smarty->config_booleanize * 2 +
(int) $smarty->config_overwrite * 4;
} 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;
$basename = $source->handler->getBasename($source);
Expand Down

0 comments on commit 4de72f1

Please sign in to comment.