mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 02:44:27 +02:00
04.08.2015
- improvement move template function source parameter into extension
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
===== 3.1.30-dev ===== (xx.xx.xx)
|
||||
04.08.2015
|
||||
- improvement move template function source parameter into extension
|
||||
|
||||
26.07.2016
|
||||
- optimization unneeded loading of compiled resource
|
||||
|
||||
|
@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
/**
|
||||
* smarty version
|
||||
*/
|
||||
const SMARTY_VERSION = '3.1.30-dev/(88)';
|
||||
const SMARTY_VERSION = '3.1.30-dev/89';
|
||||
|
||||
/**
|
||||
* define variable scopes
|
||||
|
@@ -53,7 +53,6 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
|
||||
*/
|
||||
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
||||
{
|
||||
$compiler->loopNesting ++;
|
||||
// check and get attributes
|
||||
$_attr = $this->getAttributes($compiler, $args);
|
||||
|
||||
@@ -62,8 +61,7 @@ class Smarty_Internal_Compile_Function extends Smarty_Internal_CompileBase
|
||||
}
|
||||
unset($_attr[ 'nocache' ]);
|
||||
$_name = trim($_attr[ 'name' ], "'\"");
|
||||
$compiler->parent_compiler->tpl_function[ $_name ] =
|
||||
$compiler->parent_compiler->template->tpl_function[ $_name ] = array();
|
||||
$compiler->parent_compiler->tpl_function[ $_name ] = array();
|
||||
$save = array($_attr, $compiler->parser->current_buffer, $compiler->template->compiled->has_nocache_code,
|
||||
$compiler->template->caching);
|
||||
$this->openTag($compiler, 'function', $save);
|
||||
@@ -101,20 +99,13 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
*/
|
||||
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
||||
{
|
||||
$compiler->loopNesting --;
|
||||
$this->compiler = $compiler;
|
||||
$saved_data = $this->closeTag($compiler, array('function'));
|
||||
$_attr = $saved_data[ 0 ];
|
||||
$_name = trim($_attr[ 'name' ], "'\"");
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'called_functions' ] =
|
||||
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'called_functions' ] =
|
||||
$compiler->called_functions;
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'compiled_filepath' ] =
|
||||
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'compiled_filepath' ] =
|
||||
$compiler->parent_compiler->template->compiled->filepath;
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] =
|
||||
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
|
||||
$compiler->called_functions = array();
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'uid' ] = $compiler->template->source->uid;
|
||||
$_parameter = $_attr;
|
||||
unset($_parameter[ 'name' ]);
|
||||
// default parameter
|
||||
@@ -139,8 +130,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
$_funcName = "smarty_template_function_{$_name}_{$compiler->template->compiled->nocache_hash}";
|
||||
$_funcNameCaching = $_funcName . '_nocache';
|
||||
if ($compiler->template->compiled->has_nocache_code) {
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] =
|
||||
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name_caching' ] = $_funcNameCaching;
|
||||
$output = "<?php\n";
|
||||
$output .= "/* {$_funcNameCaching} */\n";
|
||||
$output .= "if (!function_exists('{$_funcNameCaching}')) {\n";
|
||||
@@ -151,14 +141,14 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
$output .= "foreach (\$params as \$key => \$value) {\n\$_smarty_tpl->tpl_vars[\$key] = new Smarty_Variable(\$value, \$_smarty_tpl->isRenderingCache);\n}";
|
||||
$output .= "\$params = var_export(\$params, true);\n";
|
||||
$output .= "echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
|
||||
$output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
|
||||
$output .= "\\\$_smarty_tpl->ext->_tplFunction->saveTemplateVariables(\\\$_smarty_tpl, '{$_name}');\nforeach (\$params as \\\$key => \\\$value) {\n\\\$_smarty_tpl->tpl_vars[\\\$key] = new Smarty_Variable(\\\$value, \\\$_smarty_tpl->isRenderingCache);\n}\n?>";
|
||||
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\n\";?>";
|
||||
$compiler->parser->current_buffer->append_subtree($compiler->parser,
|
||||
new Smarty_Internal_ParseTree_Tag($compiler->parser,
|
||||
$output));
|
||||
$compiler->parser->current_buffer->append_subtree($compiler->parser, $_functionCode);
|
||||
$output = "<?php echo \"/*%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/<?php ";
|
||||
$output .= "\\\$_smarty_tpl->smarty->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
|
||||
$output .= "\\\$_smarty_tpl->ext->_tplFunction->restoreTemplateVariables(\\\$_smarty_tpl, '{$_name}');?>\n";
|
||||
$output .= "/*/%%SmartyNocache:{$compiler->template->compiled->nocache_hash}%%*/\";\n?>";
|
||||
$output .= "<?php echo str_replace('{$compiler->template->compiled->nocache_hash}', \$_smarty_tpl->compiled->nocache_hash, ob_get_clean());\n";
|
||||
$output .= "}\n}\n";
|
||||
@@ -172,8 +162,7 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
array($this, 'removeNocache'),
|
||||
$_functionCode->to_smarty_php($compiler->parser)));
|
||||
}
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] =
|
||||
$compiler->parent_compiler->template->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
|
||||
$compiler->parent_compiler->tpl_function[ $_name ][ 'call_name' ] = $_funcName;
|
||||
$output = "<?php\n";
|
||||
$output .= "/* {$_funcName} */\n";
|
||||
$output .= "if (!function_exists('{$_funcName}')) {\n";
|
||||
@@ -210,9 +199,11 @@ class Smarty_Internal_Compile_Functionclose extends Smarty_Internal_CompileBase
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove nocache code
|
||||
*
|
||||
* @param $match
|
||||
*
|
||||
* @return mixed
|
||||
* @return string
|
||||
*/
|
||||
function removeNocache($match)
|
||||
{
|
||||
|
@@ -10,6 +10,13 @@
|
||||
**/
|
||||
class Smarty_Internal_Runtime_TplFunction
|
||||
{
|
||||
/**
|
||||
* Array of source information for known template functions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $tplFunctions = array();
|
||||
|
||||
/**
|
||||
* Call template function
|
||||
*
|
||||
@@ -22,14 +29,14 @@ class Smarty_Internal_Runtime_TplFunction
|
||||
*/
|
||||
public function callTemplateFunction(Smarty_Internal_Template $tpl, $name, $params, $nocache)
|
||||
{
|
||||
if (isset($tpl->tpl_function[ $name ])) {
|
||||
if (isset($this->tplFunctions[ $name ])) {
|
||||
if (!$tpl->caching || ($tpl->caching && $nocache)) {
|
||||
$function = $tpl->tpl_function[ $name ][ 'call_name' ];
|
||||
$function = $this->tplFunctions[ $name ][ 'call_name' ];
|
||||
} else {
|
||||
if (isset($tpl->tpl_function[ $name ][ 'call_name_caching' ])) {
|
||||
$function = $tpl->tpl_function[ $name ][ 'call_name_caching' ];
|
||||
if (isset($this->tplFunctions[ $name ][ 'call_name_caching' ])) {
|
||||
$function = $this->tplFunctions[ $name ][ 'call_name_caching' ];
|
||||
} else {
|
||||
$function = $tpl->tpl_function[ $name ][ 'call_name' ];
|
||||
$function = $this->tplFunctions[ $name ][ 'call_name' ];
|
||||
}
|
||||
}
|
||||
if (function_exists($function)) {
|
||||
@@ -49,6 +56,39 @@ class Smarty_Internal_Runtime_TplFunction
|
||||
throw new SmartyException("Unable to find template function '{$name}'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Register template functions defined by template
|
||||
*
|
||||
* @param \Smarty_Internal_Template $tpl
|
||||
* @param array $tplFunctions source information array of template functions defined in template
|
||||
*/
|
||||
public function registerTplFunctions(Smarty_Internal_Template $tpl, $tplFunctions)
|
||||
{
|
||||
$this->tplFunctions = array_merge($this->tplFunctions, $tplFunctions);
|
||||
$ptr = $tpl;
|
||||
// make sure that the template functions are known in parent templates
|
||||
while (isset($ptr->parent) && $ptr->parent->_objType === 2 && !isset($ptr->ext->_tplFunction)) {
|
||||
$ptr->ext->_tplFunction = $this;
|
||||
$ptr = $ptr->parent;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return source parameter array for single or all template functions
|
||||
*
|
||||
* @param null|string $name template function name
|
||||
*
|
||||
* @return array|bool|mixed
|
||||
*/
|
||||
public function getTplFunction($name = null)
|
||||
{
|
||||
if (isset($name)) {
|
||||
return $this->tplFunctions[ $name ] ? $this->tplFunctions[ $name ] : false;
|
||||
} else {
|
||||
return $this->tplFunctions;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Add template function to cache file for nocache calls
|
||||
@@ -61,7 +101,7 @@ class Smarty_Internal_Runtime_TplFunction
|
||||
*/
|
||||
public function addTplFuncToCache(Smarty_Internal_Template $tpl, $_name, $_function)
|
||||
{
|
||||
$funcParam = $tpl->tpl_function[ $_name ];
|
||||
$funcParam = $this->tplFunctions[ $_name ];
|
||||
if (is_file($funcParam[ 'compiled_filepath' ])) {
|
||||
// read compiled file
|
||||
$code = file_get_contents($funcParam[ 'compiled_filepath' ]);
|
||||
|
@@ -74,13 +74,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
|
||||
*/
|
||||
public $templateId = null;
|
||||
|
||||
/**
|
||||
* Known template functions
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $tpl_function = array();
|
||||
|
||||
/**
|
||||
* Scope in which variables shall be assigned
|
||||
*
|
||||
@@ -220,15 +213,10 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
|
||||
}
|
||||
}
|
||||
if ($parentIsTpl) {
|
||||
if (!empty($this->tpl_function)) {
|
||||
$this->parent->tpl_function = array_merge($this->parent->tpl_function, $this->tpl_function);
|
||||
}
|
||||
if (isset($this->compiled)) {
|
||||
foreach ($this->compiled->required_plugins as $code => $tmp1) {
|
||||
foreach ($tmp1 as $name => $tmp) {
|
||||
foreach ($tmp as $type => $data) {
|
||||
$this->parent->compiled->required_plugins[ $code ][ $name ][ $type ] = $data;
|
||||
}
|
||||
foreach ($this->compiled->required_plugins as $code => $tmp1) {
|
||||
foreach ($tmp1 as $name => $tmp) {
|
||||
foreach ($tmp as $type => $data) {
|
||||
$this->parent->compiled->required_plugins[ $code ][ $name ][ $type ] = $data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -483,9 +471,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
|
||||
$resource->has_nocache_code = $properties[ 'has_nocache_code' ];
|
||||
// $tpl->compiled->nocache_hash = $properties['nocache_hash'];
|
||||
$resource->file_dependency = $properties[ 'file_dependency' ];
|
||||
if (isset($properties[ 'tpl_function' ])) {
|
||||
$tpl->tpl_function = array_merge($tpl->tpl_function, $properties[ 'tpl_function' ]);
|
||||
}
|
||||
}
|
||||
return $is_valid && !function_exists($properties[ 'unifunc' ]);
|
||||
}
|
||||
@@ -571,7 +556,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
|
||||
*/
|
||||
public function _cleanUp()
|
||||
{
|
||||
$this->tpl_function = array();
|
||||
$this->startRenderCallbacks = array();
|
||||
$this->endRenderCallbacks = array();
|
||||
$this->inheritance = null;
|
||||
|
@@ -526,7 +526,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
||||
}
|
||||
// compile the smarty tag (required compile classes to compile the tag are auto loaded)
|
||||
if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
|
||||
if (isset($this->parent_compiler->template->tpl_function[ $tag ])) {
|
||||
if (isset($this->parent_compiler->tpl_function[ $tag ])) {
|
||||
// template defined by {template} tag
|
||||
$args[ '_attr' ][ 'name' ] = "'" . $tag . "'";
|
||||
$_output = $this->callTagCompiler('call', $args, $parameter);
|
||||
|
@@ -167,9 +167,6 @@ class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
|
||||
if ($_template->caching && $this->has_nocache_code) {
|
||||
$_template->cached->hashes[ $this->nocache_hash ] = true;
|
||||
}
|
||||
if (isset($_template->parent) && $_template->parent->_objType == 2 && !empty($_template->tpl_function)) {
|
||||
$_template->parent->tpl_function = array_merge($_template->parent->tpl_function, $_template->tpl_function);
|
||||
}
|
||||
if ($_template->smarty->debugging) {
|
||||
$_template->smarty->_debug->end_render($_template);
|
||||
}
|
||||
|
Reference in New Issue
Block a user