mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 02:44:27 +02:00
optimize compiled code of {foreach}
This commit is contained in:
@@ -111,7 +111,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
/**
|
||||
* smarty version
|
||||
*/
|
||||
const SMARTY_VERSION = '3.1.22-dev/8';
|
||||
const SMARTY_VERSION = '3.1.22-dev/9';
|
||||
|
||||
/**
|
||||
* define variable scopes
|
||||
@@ -1493,6 +1493,14 @@ class Smarty extends Smarty_Internal_TemplateBase
|
||||
$this->auto_literal = $auto_literal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $force_compile
|
||||
*/
|
||||
public function setForceCompile($force_compile)
|
||||
{
|
||||
$this->force_compile = $force_compile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $merge_compiled_includes
|
||||
*/
|
||||
|
@@ -64,103 +64,151 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
|
||||
$key = null;
|
||||
}
|
||||
|
||||
$this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key));
|
||||
$this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $item, $key, true));
|
||||
// maybe nocache because of nocache variables
|
||||
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
|
||||
|
||||
if (isset($_attr['name'])) {
|
||||
$name = $_attr['name'];
|
||||
$name = trim($_attr['name'], '\'"');
|
||||
$has_name = true;
|
||||
$SmartyVarName = '$smarty.foreach.' . trim($name, '\'"') . '.';
|
||||
$SmartyVarName = "\$smarty.foreach.{$name}.";
|
||||
} else {
|
||||
$name = null;
|
||||
$has_name = false;
|
||||
}
|
||||
$ItemVarName = '$' . trim($item, '\'"') . '@';
|
||||
// evaluates which Smarty variables and properties have to be computed
|
||||
|
||||
if ($has_name) {
|
||||
$usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
|
||||
$usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false;
|
||||
$usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false;
|
||||
$usesSmartyIteration = strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false;
|
||||
$usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false;
|
||||
$usesSmartyTotal = strpos($compiler->lex->data, $SmartyVarName . 'total') !== false;
|
||||
$useSmartyForeach = $usesSmartyFirst = strpos($compiler->lex->data, $SmartyVarName . 'first') !== false;
|
||||
$useSmartyForeach = ($usesSmartyLast = strpos($compiler->lex->data, $SmartyVarName . 'last') !== false) || $useSmartyForeach;
|
||||
$useSmartyForeach = ($usesSmartyIndex = strpos($compiler->lex->data, $SmartyVarName . 'index') !== false) || $useSmartyForeach;
|
||||
$useSmartyForeach = ($usesSmartyIteration = (!$usesSmartyIndex && ($usesSmartyFirst || $usesSmartyLast)) || strpos($compiler->lex->data, $SmartyVarName . 'iteration') !== false) || $useSmartyForeach;
|
||||
$useSmartyForeach = ($usesSmartyShow = strpos($compiler->lex->data, $SmartyVarName . 'show') !== false) || $useSmartyForeach;
|
||||
$useSmartyForeach = ($usesSmartyTotal = $usesSmartyLast ||strpos($compiler->lex->data, $SmartyVarName . 'total') !== false) || $useSmartyForeach;
|
||||
} else {
|
||||
$usesSmartyFirst = false;
|
||||
$usesSmartyLast = false;
|
||||
$usesSmartyTotal = false;
|
||||
$usesSmartyShow = false;
|
||||
$useSmartyForeach = false;
|
||||
}
|
||||
|
||||
$usesPropFirst = $usesSmartyFirst || strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
|
||||
$usesPropLast = $usesSmartyLast || strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
|
||||
$usesPropIndex = $usesPropFirst || strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
|
||||
$usesPropIteration = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
|
||||
$usesPropKey = strpos($compiler->lex->data, $ItemVarName . 'key') !== false;
|
||||
$usesPropFirst = strpos($compiler->lex->data, $ItemVarName . 'first') !== false;
|
||||
$usesPropLast = strpos($compiler->lex->data, $ItemVarName . 'last') !== false;
|
||||
$usesPropIndex = strpos($compiler->lex->data, $ItemVarName . 'index') !== false;
|
||||
$usesPropIteration = (!$usesPropIndex && ($usesPropFirst || $usesPropLast)) || strpos($compiler->lex->data, $ItemVarName . 'iteration') !== false;
|
||||
$usesPropShow = strpos($compiler->lex->data, $ItemVarName . 'show') !== false;
|
||||
$usesPropTotal = $usesSmartyTotal || $usesSmartyShow || $usesPropShow || $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
|
||||
// generate output code
|
||||
$output = "<?php ";
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable; \$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
|
||||
if ($key != null) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
|
||||
$usesPropTotal = $usesPropLast || strpos($compiler->lex->data, $ItemVarName . 'total') !== false;
|
||||
|
||||
$keyTerm = '';
|
||||
if ($usesPropKey) {
|
||||
$keyTerm = "\$_smarty_tpl->tpl_vars[$item]->key => ";
|
||||
} elseif ($key != null) {
|
||||
$keyTerm = "\$_smarty_tpl->tpl_vars[$key]->value => ";
|
||||
}
|
||||
// generate output code
|
||||
$output = "<?php\n";
|
||||
$output .= "\$_from = $from;\n";
|
||||
$output .= "if (!is_array(\$_from) && !is_object(\$_from)) {\n";
|
||||
$output .= "settype(\$_from, 'array');\n";
|
||||
$output .= "}\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = false;\n";
|
||||
if ($key != null) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
|
||||
}
|
||||
$output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
|
||||
if ($usesPropTotal) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->total= \$_smarty_tpl->_count(\$_from);\n";
|
||||
}
|
||||
if ($usesPropIteration) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
|
||||
}
|
||||
if ($usesPropIndex) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
|
||||
}
|
||||
if ($usesPropShow) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
|
||||
if ($usesPropTotal) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
|
||||
} else {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->show = (\$_smarty_tpl->_count(\$_from) > 0);\n";
|
||||
}
|
||||
}
|
||||
if ($has_name) {
|
||||
$prop = array();
|
||||
if ($usesSmartyTotal) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
|
||||
$prop['total'] = "'total' => ";
|
||||
$prop['total'] .= $usesSmartyShow ? '$total = ' : '';
|
||||
$prop['total'] .= '$_smarty_tpl->_count($_from)';
|
||||
}
|
||||
if ($usesSmartyIteration) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
|
||||
$prop['iteration'] = "'iteration' => 0";
|
||||
}
|
||||
if ($usesSmartyIndex) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
|
||||
$prop['index'] = "'index' => -1";
|
||||
}
|
||||
if ($usesSmartyShow) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['show']=(\$_smarty_tpl->tpl_vars[$item]->total > 0);\n";
|
||||
$prop['show'] = "'show' => ";
|
||||
if ($usesSmartyTotal) {
|
||||
$prop['show'] .= "(\$total > 0)";
|
||||
} else {
|
||||
$prop['show'] .= "(\$_smarty_tpl->_count(\$_from) > 0)";
|
||||
}
|
||||
}
|
||||
if ($useSmartyForeach) {
|
||||
$_vars = 'array(' . join(', ', $prop) . ')';
|
||||
$foreachVar = "'__foreach_{$name}'";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar] = new Smarty_Variable({$_vars});\n";
|
||||
}
|
||||
}
|
||||
$output .= "foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value) {\n\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
|
||||
if ($key != null) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
|
||||
$output .= "foreach (\$_from as {$keyTerm}\$_smarty_tpl->tpl_vars[$item]->value) {\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->_loop = true;\n";
|
||||
if ($key != null && $usesPropKey) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
|
||||
}
|
||||
if ($usesPropIteration) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
|
||||
}
|
||||
if ($usesPropIndex) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->index++;\n";
|
||||
}
|
||||
if ($usesPropFirst) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
|
||||
if ($usesPropIndex) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index == 0;\n";
|
||||
} else {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->iteration == 1;\n";
|
||||
}
|
||||
}
|
||||
if ($usesPropLast) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
|
||||
if ($usesPropIndex) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->index + 1 == \$_smarty_tpl->tpl_vars[$item]->total;\n";
|
||||
} else {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration == \$_smarty_tpl->tpl_vars[$item]->total;\n";
|
||||
}
|
||||
}
|
||||
if ($has_name) {
|
||||
if ($usesSmartyFirst) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
|
||||
}
|
||||
if ($usesSmartyIteration) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration']++;\n";
|
||||
}
|
||||
if ($usesSmartyIndex) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['index']++;\n";
|
||||
}
|
||||
if ($usesSmartyFirst) {
|
||||
if ($usesSmartyIndex) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] == 0;\n";
|
||||
} else {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == 1;\n";
|
||||
}
|
||||
}
|
||||
if ($usesSmartyLast) {
|
||||
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
|
||||
if ($usesSmartyIndex) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] + 1 == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
|
||||
} else {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
$output .= "\$foreachItemSav = \$_smarty_tpl->tpl_vars[$item];\n";
|
||||
$output .= "?>";
|
||||
|
||||
return $output;
|
||||
@@ -189,10 +237,13 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
|
||||
// check and get attributes
|
||||
$_attr = $this->getAttributes($compiler, $args);
|
||||
|
||||
list($openTag, $nocache, $item, $key) = $this->closeTag($compiler, array('foreach'));
|
||||
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key));
|
||||
|
||||
return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
|
||||
list($openTag, $nocache, $item, $key, $foo) = $this->closeTag($compiler, array('foreach'));
|
||||
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $item, $key, false));
|
||||
$output = "<?php\n";
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreachItemSav;\n";
|
||||
$output .= "}\n";
|
||||
$output .= "if (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,8 +273,13 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
|
||||
$compiler->tag_nocache = true;
|
||||
}
|
||||
|
||||
list($openTag, $compiler->nocache, $item, $key) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
|
||||
list($openTag, $compiler->nocache, $item, $key, $restore) = $this->closeTag($compiler, array('foreach', 'foreachelse'));
|
||||
$output = "<?php\n";
|
||||
if ($restore) {
|
||||
$output .= "\$_smarty_tpl->tpl_vars[$item] = \$foreachItemSav;\n";
|
||||
}
|
||||
$output .= "}\n?>";
|
||||
|
||||
return "<?php } ?>";
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
@@ -33,7 +33,9 @@ class Smarty_Internal_Compile_Private_Special_Variable extends Smarty_Internal_C
|
||||
if (!isset($compiler->smarty->security_policy) || $compiler->smarty->security_policy->isTrustedSpecialSmartyVar($variable, $compiler)) {
|
||||
switch ($variable) {
|
||||
case 'foreach':
|
||||
return "\$_smarty_tpl->getVariable('smarty')->value$parameter";
|
||||
$name = trim($_index[1], "'");
|
||||
$foreachVar = "'__foreach_{$name}'";
|
||||
return "isset(\$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}]) ? \$_smarty_tpl->tpl_vars[$foreachVar]->value[{$_index[2]}] : null";
|
||||
case 'section':
|
||||
return "\$_smarty_tpl->getVariable('smarty')->value$parameter";
|
||||
case 'capture':
|
||||
|
Reference in New Issue
Block a user