- fix template scope processing

This commit is contained in:
uwetews
2015-10-24 04:45:09 +02:00
parent b26ba9ce52
commit eec7639c9c
4 changed files with 70 additions and 55 deletions

View File

@@ -248,7 +248,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
if (isset($_assign)) {
$_output .= "ob_start();\n";
}
$_output .= "\$_smarty_tpl->_inline->render(\$_smarty_tpl, {$fullResourceName}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func']}');\n";
$_output .= "\$_smarty_tpl->smarty->ext->_inline->render(\$_smarty_tpl, {$fullResourceName}, {$_cache_id}, {$_compile_id}, {$_caching}, {$_cache_lifetime}, {$_vars}, {$_parent_scope}, {$_cache_tpl}, '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['uid']}', '{$compiler->parent_compiler->mergedSubTemplatesData[$hashResourceName][$t_hash]['func']}');\n";
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(ob_get_clean());\n";
}
@@ -271,7 +271,7 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
if (isset($_assign)) {
$_output .= "ob_start();\n";
}
$_output .= "\$_smarty_tpl->_subtemplate->render(\$_smarty_tpl, {$fullResourceName}, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl});\n";
$_output .= "\$_smarty_tpl->smarty->ext->_subtemplate->render(\$_smarty_tpl, {$fullResourceName}, $_cache_id, $_compile_id, $_caching, $_cache_lifetime, $_vars, $_parent_scope, {$_cache_tpl});\n";
if (isset($_assign)) {
$_output .= "\$_smarty_tpl->tpl_vars[$_assign] = new Smarty_Variable(ob_get_clean());\n";
}

View File

@@ -1,14 +1,14 @@
<?php
/**
* Inline Runtime Methods render
* Inline Runtime Methods render, setSourceByUid, setupSubTemplate
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*
**/
class Smarty_Internal_Runtime_Inline extends Smarty_Internal_Runtime_Subtemplate
class Smarty_Internal_Runtime_Inline extends Smarty_Internal_Runtime_SubTemplate
{
/**
@@ -21,18 +21,18 @@ class Smarty_Internal_Runtime_Inline extends Smarty_Internal_Runtime_Subtemplate
* @param integer $caching cache mode
* @param integer $cache_lifetime life time of cache data
* @param array $data passed parameter template variables
* @param int $parent_scope scope in which {include} should execute
* @param bool $cache_tpl_obj cache template object
* @param int $scope scope in which {include} should execute
* @param bool $forceTplCache cache template object
* @param string $uid file dependency uid
* @param string $content_func function name
*
* @throws \Exception
*/
public function render(\Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching,
$cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $uid, $content_func)
$cache_lifetime, $data, $scope, $forceTplCache, $uid, $content_func)
{
$tpl = $this->setupSubtemplate($parent, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data,
$parent_scope, $cache_tpl_obj, $uid);
$tpl = $this->setupSubTemplate($parent, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data,
$scope, $uid);
if ($parent->smarty->debugging) {
$parent->smarty->_debug->start_template($tpl);
$parent->smarty->_debug->start_render($tpl);
@@ -45,6 +45,7 @@ class Smarty_Internal_Runtime_Inline extends Smarty_Internal_Runtime_Subtemplate
if ($tpl->caching == 9999 && $tpl->compiled->has_nocache_code) {
$parent->cached->hashes[$tpl->compiled->nocache_hash] = true;
}
$this->updateTemplateCache($tpl, $forceTplCache);
}
/**

View File

@@ -1,14 +1,14 @@
<?php
/**
* Subtemplate Runtime Methods render, setupSubtemplate
* Sub Template Runtime Methods render, setupSubTemplate
*
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
*
**/
class Smarty_Internal_Runtime_Subtemplate
class Smarty_Internal_Runtime_SubTemplate
{
/**
@@ -21,16 +21,17 @@ class Smarty_Internal_Runtime_Subtemplate
* @param integer $caching cache mode
* @param integer $cache_lifetime life time of cache data
* @param array $data passed parameter template variables
* @param int $parent_scope scope in which {include} should execute
* @param bool $cache_tpl_obj cache template object
* @param int $scope scope in which {include} should execute
* @param bool $forceTplCache cache template object
*
*/
public function render(Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching,
$cache_lifetime, $data, $parent_scope, $cache_tpl_obj)
$cache_lifetime, $data, $scope, $forceTplCache)
{
$this->setupSubtemplate($parent, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data,
$parent_scope, $cache_tpl_obj)
->render();
$tpl = $this->setupSubTemplate($parent, $template, $cache_id, $compile_id, $caching, $cache_lifetime, $data,
$scope);
$tpl->render();
$this->updateTemplateCache($tpl, $forceTplCache);
}
/**
@@ -43,15 +44,14 @@ class Smarty_Internal_Runtime_Subtemplate
* @param integer $caching cache mode
* @param integer $cache_lifetime life time of cache data
* @param array $data passed parameter template variables
* @param int $parent_scope scope in which {include} should execute
* @param bool $cache_tpl_obj cache template object
* @param int $scope scope in which {include} should execute
* @param string|null $uid source uid
*
* @return \Smarty_Internal_Template template object
* @throws \SmartyException
*/
public function setupSubtemplate(Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching,
$cache_lifetime, $data, $parent_scope, $cache_tpl_obj, $uid = null)
public function setupSubTemplate(Smarty_Internal_Template $parent, $template, $cache_id, $compile_id, $caching,
$cache_lifetime, $data, $scope, $uid = null)
{
// if there are cached template objects calculate $templateID
$_templateId = isset($parent->smarty->_cache['template_objects']) ?
@@ -67,7 +67,7 @@ class Smarty_Internal_Runtime_Subtemplate
unset($tpl->compiled);
}
// get variables from calling scope
if ($parent_scope == Smarty::SCOPE_LOCAL) {
if ($scope == Smarty::SCOPE_LOCAL) {
$tpl->tpl_vars = $parent->tpl_vars;
$tpl->config_vars = $parent->config_vars;
}
@@ -89,9 +89,6 @@ class Smarty_Internal_Runtime_Subtemplate
// $uid is set if template is inline
if (isset($uid)) {
$this->setSourceByUid($tpl, $uid);
// if template is called multiple times set flag to to cache template objects
$cache_tpl_obj = $cache_tpl_obj || (isset($tpl->compiled->includes[$tpl->template_resource]) &&
$tpl->compiled->includes[$tpl->template_resource] > 1);
} else {
$tpl->source = null;
unset($tpl->compiled);
@@ -100,14 +97,6 @@ class Smarty_Internal_Runtime_Subtemplate
$tpl->source = Smarty_Template_Source::load($tpl);
}
unset($tpl->cached);
// check if template object should be cached
if (!$tpl->source->handler->recompiled && (isset($tpl->parent->templateId) &&
isset($tpl->smarty->_cache['template_objects'][$tpl->parent->templateId]) ||
($cache_tpl_obj && $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) ||
$tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
) {
$tpl->smarty->_cache['template_objects'][$tpl->_getTemplateId()] = $tpl;
}
}
}
$tpl->caching = $caching;
@@ -116,14 +105,19 @@ class Smarty_Internal_Runtime_Subtemplate
$tpl->cached = $parent->cached;
}
// get variables from calling scope
if ($parent_scope != Smarty::SCOPE_LOCAL) {
if ($parent_scope == Smarty::SCOPE_PARENT) {
if ($scope != $tpl->scope) {
if ($tpl->scope != Smarty::SCOPE_LOCAL) {
unset($tpl->tpl_vars, $tpl->config_vars);
$tpl->tpl_vars = array();
$tpl->config_vars = array();
}
if ($scope == Smarty::SCOPE_PARENT) {
$tpl->tpl_vars = &$parent->tpl_vars;
$tpl->config_vars = &$parent->config_vars;
} elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
} elseif ($scope == Smarty::SCOPE_GLOBAL) {
$tpl->tpl_vars = &Smarty::$global_tpl_vars;
$tpl->config_vars = $parent->config_vars;
} elseif ($parent_scope == Smarty::SCOPE_ROOT) {
} elseif ($scope == Smarty::SCOPE_ROOT) {
$ptr = $tpl->parent;
while (!empty($ptr->parent)) {
$ptr = $ptr->parent;
@@ -134,6 +128,7 @@ class Smarty_Internal_Runtime_Subtemplate
$tpl->tpl_vars = $parent->tpl_vars;
$tpl->config_vars = $parent->config_vars;
}
$tpl->scope = $scope;
}
if (!empty($data)) {
@@ -144,4 +139,31 @@ class Smarty_Internal_Runtime_Subtemplate
}
return $tpl;
}
public function updateTemplateCache(Smarty_Internal_Template $tpl, $forceTplCache)
{
if (isset($tpl->smarty->_cache['template_objects'][$tpl->_getTemplateId()]) ||
$tpl->source->handler->recompiled
) {
return;
}
// if template is called multiple times set flag to to cache template objects
$forceTplCache = $forceTplCache || (isset($tpl->compiled->includes[$tpl->template_resource]) &&
$tpl->compiled->includes[$tpl->template_resource] > 1);
// check if template object should be cached
if ((isset($tpl->parent->templateId) &&
isset($tpl->smarty->_cache['template_objects'][$tpl->parent->templateId]) ||
($forceTplCache && $tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_AUTOMATIC) ||
$tpl->smarty->resource_cache_mode & Smarty::RESOURCE_CACHE_ON)
) {
$tpl->parent = null;
if ($tpl->scope != Smarty::SCOPE_LOCAL) {
unset($tpl->tpl_vars, $tpl->config_vars);
$tpl->scope = Smarty::SCOPE_LOCAL;
}
$tpl->tpl_vars = array();
$tpl->config_vars = array();
$tpl->smarty->_cache['template_objects'][$tpl->_getTemplateId()] = $tpl;
}
}
}

View File

@@ -17,12 +17,6 @@
* @property Smarty_Template_Source|Smarty_Template_Config $source
* @property Smarty_Template_Compiled $compiled
* @property Smarty_Template_Cached $cached
* @property Smarty_Internal_Runtime_Inheritance $_inheritance
* @property Smarty_Internal_Runtime_Subtemplate $_subtemplate
* @property Smarty_Internal_Runtime_Inline $_inline
* @property Smarty_Internal_Runtime_Tplfunc $_tplfunc
* @property Smarty_Internal_Runtime_Var $_var
* @property Smarty_Internal_Runtime_Foreach $_foreach
* @method bool mustCompile()
*/
class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
@@ -76,6 +70,13 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public $tpl_function = array();
/**
* Scope in which template is rendered
*
* @var int
*/
public $scope = 0;
/**
* Create template data object
* Some of the global Smarty settings copied to template scope
@@ -108,6 +109,7 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
// Template resource
$this->template_resource = $template_resource;
$this->source = Smarty_Template_Source::load($this);
parent::__construct();
}
/**
@@ -427,14 +429,12 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public function __set($property_name, $value)
{
if ($property_name[0] == '_') {
$this->$property_name = $value;
return;
}
switch ($property_name) {
case 'compiled':
case 'cached':
case 'compiler':
case 'tpl_vars':
case 'config_vars':
$this->$property_name = $value;
return;
default:
@@ -457,14 +457,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
*/
public function __get($property_name)
{
// object properties of runtime template extensions will start with '_'
if ($property_name[0] == '_') {
$class = 'Smarty_Internal_Runtime' . $property_name;
$class[24] = chr(ord($class[24]) & 0xDF);
if (class_exists($class)) {
return $this->$property_name = new $class();
}
}
switch ($property_name) {
case 'compiled':
$this->loadCompiled();