mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 10:24:26 +02:00
- optimization of tag attribute compiling
This commit is contained in:
@@ -3,7 +3,8 @@
|
|||||||
- new tag {make_nocache} read NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/110
|
- new tag {make_nocache} read NEW_FEATURES.txt https://github.com/smarty-php/smarty/issues/110
|
||||||
- optimization of sub-template processing
|
- optimization of sub-template processing
|
||||||
- bugfix using extendsall as default resource and {include} inside {block} tags could produce unexpected results https://github.com/smarty-php/smarty/issues/183
|
- bugfix using extendsall as default resource and {include} inside {block} tags could produce unexpected results https://github.com/smarty-php/smarty/issues/183
|
||||||
|
- optimization of tag attribute compiling
|
||||||
|
|
||||||
11.02.2016
|
11.02.2016
|
||||||
- improvement added KnockoutJS comments to trimwhitespace outputfilter https://github.com/smarty-php/smarty/issues/82
|
- improvement added KnockoutJS comments to trimwhitespace outputfilter https://github.com/smarty-php/smarty/issues/82
|
||||||
https://github.com/smarty-php/smarty/pull/181
|
https://github.com/smarty-php/smarty/pull/181
|
||||||
|
@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
|||||||
/**
|
/**
|
||||||
* smarty version
|
* smarty version
|
||||||
*/
|
*/
|
||||||
const SMARTY_VERSION = '3.1.30-dev/41';
|
const SMARTY_VERSION = '3.1.30-dev/42';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* define variable scopes
|
* define variable scopes
|
||||||
|
@@ -31,6 +31,7 @@ class Smarty_Internal_Compile_Append extends Smarty_Internal_Compile_Assign
|
|||||||
$this->required_attributes = array('var', 'value');
|
$this->required_attributes = array('var', 'value');
|
||||||
$this->shorttag_order = array('var', 'value');
|
$this->shorttag_order = array('var', 'value');
|
||||||
$this->optional_attributes = array('scope', 'index');
|
$this->optional_attributes = array('scope', 'index');
|
||||||
|
$this->mapCache = array();
|
||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
// map to compile assign attributes
|
// map to compile assign attributes
|
||||||
|
@@ -49,6 +49,7 @@ class Smarty_Internal_Compile_Assign extends Smarty_Internal_CompileBase
|
|||||||
$this->required_attributes = array('var', 'value');
|
$this->required_attributes = array('var', 'value');
|
||||||
$this->shorttag_order = array('var', 'value');
|
$this->shorttag_order = array('var', 'value');
|
||||||
$this->optional_attributes = array('scope');
|
$this->optional_attributes = array('scope');
|
||||||
|
$this->mapCache = array();
|
||||||
$_nocache = false;
|
$_nocache = false;
|
||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
|
@@ -50,8 +50,6 @@ class Smarty_Internal_Compile_Eval extends Smarty_Internal_CompileBase
|
|||||||
*/
|
*/
|
||||||
public function compile($args, $compiler)
|
public function compile($args, $compiler)
|
||||||
{
|
{
|
||||||
$this->required_attributes = array('var');
|
|
||||||
$this->optional_attributes = array('assign');
|
|
||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
if (isset($_attr[ 'assign' ])) {
|
if (isset($_attr[ 'assign' ])) {
|
||||||
|
@@ -42,6 +42,7 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
|
|||||||
$this->required_attributes = array('start', 'ifexp', 'var', 'step');
|
$this->required_attributes = array('start', 'ifexp', 'var', 'step');
|
||||||
$this->optional_attributes = array();
|
$this->optional_attributes = array();
|
||||||
}
|
}
|
||||||
|
$this->mapCache = array();
|
||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
|
|
||||||
|
@@ -128,10 +128,6 @@ class Smarty_Internal_Compile_Include extends Smarty_Internal_CompileBase
|
|||||||
// assume caching is off
|
// assume caching is off
|
||||||
$_caching = Smarty::CACHING_OFF;
|
$_caching = Smarty::CACHING_OFF;
|
||||||
|
|
||||||
if ($_attr[ 'nocache' ] === true) {
|
|
||||||
$compiler->tag_nocache = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$call_nocache = $compiler->tag_nocache || $compiler->nocache;
|
$call_nocache = $compiler->tag_nocache || $compiler->nocache;
|
||||||
|
|
||||||
// caching was on and {include} is not in nocache mode
|
// caching was on and {include} is not in nocache mode
|
||||||
|
@@ -49,9 +49,6 @@ class Smarty_Internal_Compile_Private_Block_Plugin extends Smarty_Internal_Compi
|
|||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
$this->nesting ++;
|
$this->nesting ++;
|
||||||
if ($_attr[ 'nocache' ] === true) {
|
|
||||||
$compiler->tag_nocache = true;
|
|
||||||
}
|
|
||||||
unset($_attr[ 'nocache' ]);
|
unset($_attr[ 'nocache' ]);
|
||||||
list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
|
list($callback, $_paramsArray, $callable) = $this->setup($compiler, $_attr, $tag, $function);
|
||||||
$_params = 'array(' . implode(",", $_paramsArray) . ')';
|
$_params = 'array(' . implode(",", $_paramsArray) . ')';
|
||||||
|
@@ -50,9 +50,6 @@ class Smarty_Internal_Compile_Private_Function_Plugin extends Smarty_Internal_Co
|
|||||||
//Does tag create output
|
//Does tag create output
|
||||||
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
|
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
|
||||||
|
|
||||||
if ($_attr[ 'nocache' ] === true) {
|
|
||||||
$compiler->tag_nocache = true;
|
|
||||||
}
|
|
||||||
unset($_attr[ 'nocache' ]);
|
unset($_attr[ 'nocache' ]);
|
||||||
// convert attributes into parameter array string
|
// convert attributes into parameter array string
|
||||||
$_paramsArray = array();
|
$_paramsArray = array();
|
||||||
|
@@ -42,9 +42,6 @@ class Smarty_Internal_Compile_Private_Object_Function extends Smarty_Internal_Co
|
|||||||
//Does tag create output
|
//Does tag create output
|
||||||
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
|
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
|
||||||
|
|
||||||
if ($_attr[ 'nocache' ] === true) {
|
|
||||||
$compiler->tag_nocache = true;
|
|
||||||
}
|
|
||||||
unset($_attr[ 'nocache' ]);
|
unset($_attr[ 'nocache' ]);
|
||||||
$_assign = null;
|
$_assign = null;
|
||||||
if (isset($_attr[ 'assign' ])) {
|
if (isset($_attr[ 'assign' ])) {
|
||||||
|
@@ -46,10 +46,6 @@ class Smarty_Internal_Compile_Private_Print_Expression extends Smarty_Internal_C
|
|||||||
{
|
{
|
||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
// nocache option
|
|
||||||
if ($_attr[ 'nocache' ] === true) {
|
|
||||||
$compiler->tag_nocache = true;
|
|
||||||
}
|
|
||||||
if (isset($_attr[ 'assign' ])) {
|
if (isset($_attr[ 'assign' ])) {
|
||||||
// assign output to variable
|
// assign output to variable
|
||||||
$output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
|
$output = "<?php \$_smarty_tpl->assign({$_attr['assign']},{$parameter['value']});?>";
|
||||||
|
@@ -41,9 +41,6 @@ class Smarty_Internal_Compile_Private_Registered_Function extends Smarty_Interna
|
|||||||
//Does tag create output
|
//Does tag create output
|
||||||
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
|
$compiler->has_output = isset($_attr[ 'assign' ]) ? false : true;
|
||||||
|
|
||||||
if ($_attr[ 'nocache' ]) {
|
|
||||||
$compiler->tag_nocache = true;
|
|
||||||
}
|
|
||||||
unset($_attr[ 'nocache' ]);
|
unset($_attr[ 'nocache' ]);
|
||||||
if (isset($compiler->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ])) {
|
if (isset($compiler->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ])) {
|
||||||
$tag_info = $compiler->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ];
|
$tag_info = $compiler->smarty->registered_plugins[ Smarty::PLUGIN_FUNCTION ][ $tag ];
|
||||||
|
@@ -44,6 +44,10 @@ abstract class Smarty_Internal_CompileBase
|
|||||||
*/
|
*/
|
||||||
public $option_flags = array('nocache');
|
public $option_flags = array('nocache');
|
||||||
|
|
||||||
|
public $optionMap = array(1 => true, 0 => false, 'true' => true, 'false' => false);
|
||||||
|
|
||||||
|
public $mapCache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function checks if the attributes passed are valid
|
* This function checks if the attributes passed are valid
|
||||||
* The attributes passed for the tag to compile are checked against the list of required and
|
* The attributes passed for the tag to compile are checked against the list of required and
|
||||||
@@ -59,12 +63,14 @@ abstract class Smarty_Internal_CompileBase
|
|||||||
public function getAttributes($compiler, $attributes)
|
public function getAttributes($compiler, $attributes)
|
||||||
{
|
{
|
||||||
$_indexed_attr = array();
|
$_indexed_attr = array();
|
||||||
// loop over attributes
|
if (!isset($this->mapCache[ 'option' ])) {
|
||||||
|
$this->mapCache[ 'option' ] = array_fill_keys($this->option_flags, true);
|
||||||
|
}
|
||||||
foreach ($attributes as $key => $mixed) {
|
foreach ($attributes as $key => $mixed) {
|
||||||
// shorthand ?
|
// shorthand ?
|
||||||
if (!is_array($mixed)) {
|
if (!is_array($mixed)) {
|
||||||
// option flag ?
|
// option flag ?
|
||||||
if (in_array(trim($mixed, '\'"'), $this->option_flags)) {
|
if (isset($this->mapCache[ 'option' ][ trim($mixed, '\'"') ])) {
|
||||||
$_indexed_attr[ trim($mixed, '\'"') ] = true;
|
$_indexed_attr[ trim($mixed, '\'"') ] = true;
|
||||||
// shorthand attribute ?
|
// shorthand attribute ?
|
||||||
} elseif (isset($this->shorttag_order[ $key ])) {
|
} elseif (isset($this->shorttag_order[ $key ])) {
|
||||||
@@ -75,46 +81,44 @@ abstract class Smarty_Internal_CompileBase
|
|||||||
}
|
}
|
||||||
// named attribute
|
// named attribute
|
||||||
} else {
|
} else {
|
||||||
$kv = each($mixed);
|
foreach ($mixed as $k => $v) {
|
||||||
// option flag?
|
// option flag?
|
||||||
if (in_array($kv[ 'key' ], $this->option_flags)) {
|
if (isset($this->mapCache[ 'option' ][ $k ])) {
|
||||||
if (is_bool($kv[ 'value' ])) {
|
if (is_bool($v)) {
|
||||||
$_indexed_attr[ $kv[ 'key' ] ] = $kv[ 'value' ];
|
$_indexed_attr[ $k ] = $v;
|
||||||
} elseif (is_string($kv[ 'value' ]) &&
|
|
||||||
in_array(trim($kv[ 'value' ], '\'"'), array('true', 'false'))
|
|
||||||
) {
|
|
||||||
if (trim($kv[ 'value' ]) == 'true') {
|
|
||||||
$_indexed_attr[ $kv[ 'key' ] ] = true;
|
|
||||||
} else {
|
} else {
|
||||||
$_indexed_attr[ $kv[ 'key' ] ] = false;
|
if (is_string($v)) {
|
||||||
}
|
$v = trim($v, '\'" ');
|
||||||
} elseif (is_numeric($kv[ 'value' ]) && in_array($kv[ 'value' ], array(0, 1))) {
|
}
|
||||||
if ($kv[ 'value' ] == 1) {
|
if (isset($this->optionMap[ $v ])) {
|
||||||
$_indexed_attr[ $kv[ 'key' ] ] = true;
|
$_indexed_attr[ $k ] = $this->optionMap[ $v ];
|
||||||
} else {
|
} else {
|
||||||
$_indexed_attr[ $kv[ 'key' ] ] = false;
|
$compiler->trigger_template_error("illegal value '" . var_export($v, true) .
|
||||||
|
"' for option flag '{$k}'", null, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// must be named attribute
|
||||||
} else {
|
} else {
|
||||||
$compiler->trigger_template_error("illegal value of option flag \"{$kv['key']}\"", null, true);
|
$_indexed_attr[ $k ] = $v;
|
||||||
}
|
}
|
||||||
// must be named attribute
|
|
||||||
} else {
|
|
||||||
reset($mixed);
|
|
||||||
$_indexed_attr[ key($mixed) ] = $mixed[ key($mixed) ];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check if all required attributes present
|
// check if all required attributes present
|
||||||
foreach ($this->required_attributes as $attr) {
|
foreach ($this->required_attributes as $attr) {
|
||||||
if (!array_key_exists($attr, $_indexed_attr)) {
|
if (!isset($_indexed_attr[ $attr ])) {
|
||||||
$compiler->trigger_template_error("missing \"" . $attr . "\" attribute", null, true);
|
$compiler->trigger_template_error("missing \"" . $attr . "\" attribute", null, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// check for not allowed attributes
|
// check for not allowed attributes
|
||||||
if ($this->optional_attributes != array('_any')) {
|
if ($this->optional_attributes != array('_any')) {
|
||||||
$tmp_array = array_merge($this->required_attributes, $this->optional_attributes, $this->option_flags);
|
if (!isset($this->mapCache[ 'all' ])) {
|
||||||
|
$this->mapCache[ 'all' ] =
|
||||||
|
array_fill_keys(array_merge($this->required_attributes, $this->optional_attributes,
|
||||||
|
$this->option_flags), true);
|
||||||
|
}
|
||||||
foreach ($_indexed_attr as $key => $dummy) {
|
foreach ($_indexed_attr as $key => $dummy) {
|
||||||
if (!in_array($key, $tmp_array) && $key !== 0) {
|
if (!isset($this->mapCache[ 'all' ][ $key ]) && $key !== 0) {
|
||||||
$compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", null, true);
|
$compiler->trigger_template_error("unexpected \"" . $key . "\" attribute", null, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,7 +129,9 @@ abstract class Smarty_Internal_CompileBase
|
|||||||
$_indexed_attr[ $flag ] = false;
|
$_indexed_attr[ $flag ] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (isset($_indexed_attr[ 'nocache' ]) && $_indexed_attr[ 'nocache' ]) {
|
||||||
|
$compiler->tag_nocache = true;
|
||||||
|
}
|
||||||
return $_indexed_attr;
|
return $_indexed_attr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -507,10 +507,18 @@ abstract class Smarty_Internal_TemplateCompilerBase
|
|||||||
$this->template->_cache[ 'used_tags' ][] = array($tag, $args);
|
$this->template->_cache[ 'used_tags' ][] = array($tag, $args);
|
||||||
}
|
}
|
||||||
// check nocache option flag
|
// check nocache option flag
|
||||||
if (in_array("'nocache'", $args) || in_array(array('nocache' => 'true'), $args) ||
|
foreach ($args as $arg) {
|
||||||
in_array(array('nocache' => '"true"'), $args) || in_array(array('nocache' => "'true'"), $args)
|
if (!is_array($arg)) {
|
||||||
) {
|
if ($arg == "'nocache'") {
|
||||||
$this->tag_nocache = true;
|
$this->tag_nocache = true;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
foreach ($arg as $k => $v) {
|
||||||
|
if ($k == "'nocache'" && (trim($v, "'\" ") == 'true')) {
|
||||||
|
$this->tag_nocache = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// compile the smarty tag (required compile classes to compile the tag are auto loaded)
|
// compile the smarty tag (required compile classes to compile the tag are auto loaded)
|
||||||
if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
|
if (($_output = $this->callTagCompiler($tag, $args, $parameter)) === false) {
|
||||||
|
Reference in New Issue
Block a user