mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-04 18:34:27 +02:00
- optimization {foreach} compiler and processing
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
===== 3.1.30-dev ===== (xx.xx.xx)
|
===== 3.1.30-dev ===== (xx.xx.xx)
|
||||||
|
16.05.2016
|
||||||
|
- optimization {foreach} compiler and processing
|
||||||
|
|
||||||
15.05.2016
|
15.05.2016
|
||||||
- optimization and cleanup of resource code
|
- optimization and cleanup of resource code
|
||||||
|
|
||||||
|
@@ -121,7 +121,7 @@ class Smarty extends Smarty_Internal_TemplateBase
|
|||||||
/**
|
/**
|
||||||
* smarty version
|
* smarty version
|
||||||
*/
|
*/
|
||||||
const SMARTY_VERSION = '3.1.30-dev/69';
|
const SMARTY_VERSION = '3.1.30-dev/70';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* define variable scopes
|
* define variable scopes
|
||||||
|
@@ -169,20 +169,28 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
|
|||||||
$itemAttr[ 'iteration' ] = true;
|
$itemAttr[ 'iteration' ] = true;
|
||||||
$itemAttr[ 'total' ] = true;
|
$itemAttr[ 'total' ] = true;
|
||||||
}
|
}
|
||||||
|
if (isset($namedAttr[ 'show' ])) {
|
||||||
|
$namedAttr[ 'total' ] = true;
|
||||||
|
}
|
||||||
|
if (isset($itemAttr[ 'show' ])) {
|
||||||
|
$itemAttr[ 'total' ] = true;
|
||||||
|
}
|
||||||
$keyTerm = '';
|
$keyTerm = '';
|
||||||
if (isset($itemAttr[ 'key' ])) {
|
if (isset($itemAttr[ 'key' ])) {
|
||||||
$keyTerm = "{$itemVar}->key => ";
|
$keyTerm = "{$itemVar}->key => ";
|
||||||
|
unset($itemAttr[ 'key' ]);
|
||||||
} elseif (isset($attributes[ 'key' ])) {
|
} elseif (isset($attributes[ 'key' ])) {
|
||||||
$keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
|
$keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
|
||||||
}
|
}
|
||||||
if ($this->isNamed) {
|
if ($this->isNamed) {
|
||||||
$foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
|
$foreachVar = "\$_smarty_tpl->tpl_vars['__smarty_foreach_{$attributes['name']}']";
|
||||||
}
|
}
|
||||||
$this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $local, $itemVar, true));
|
$needTotal = isset($itemAttr[ 'total' ]);
|
||||||
|
// Register tag
|
||||||
|
$this->openTag($compiler, 'foreach',
|
||||||
|
array('foreach', $compiler->nocache, $local, $itemVar, empty($itemAttr) ? 1 : 2));
|
||||||
// maybe nocache because of nocache variables
|
// maybe nocache because of nocache variables
|
||||||
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
|
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
|
||||||
$needTotal = isset($itemAttr[ 'show' ]) || isset($itemAttr[ 'total' ]) || isset($namedAttr[ 'total' ]) ||
|
|
||||||
isset($namedAttr[ 'show' ]) || isset($itemAttr[ 'last' ]) || isset($namedAttr[ 'last' ]);
|
|
||||||
// generate output code
|
// generate output code
|
||||||
$output = "<?php\n";
|
$output = "<?php\n";
|
||||||
$output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
|
$output .= "\$_from = \$_smarty_tpl->smarty->ext->_foreach->init(\$_smarty_tpl, $from, " .
|
||||||
@@ -206,8 +214,8 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
|
|||||||
if (isset($itemAttr[ 'index' ])) {
|
if (isset($itemAttr[ 'index' ])) {
|
||||||
$output .= "{$itemVar}->index = -1;\n";
|
$output .= "{$itemVar}->index = -1;\n";
|
||||||
}
|
}
|
||||||
|
$output .= "if (\$_from !== null) {\n";
|
||||||
$output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
|
$output .= "foreach (\$_from as {$keyTerm}{$itemVar}->value) {\n";
|
||||||
$output .= "{$itemVar}->_loop = true;\n";
|
|
||||||
if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) {
|
if (isset($attributes[ 'key' ]) && isset($itemAttr[ 'key' ])) {
|
||||||
$output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
|
$output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = {$itemVar}->key;\n";
|
||||||
}
|
}
|
||||||
@@ -237,7 +245,9 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_Compile_Private_Fo
|
|||||||
$output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n";
|
$output .= "{$foreachVar}->value['last'] = {$foreachVar}->value['iteration'] == {$foreachVar}->value['total'];\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$output .= "{$local}saved = {$itemVar};\n";
|
if (!empty($itemAttr)) {
|
||||||
|
$output .= "{$local}saved = {$itemVar};\n";
|
||||||
|
}
|
||||||
$output .= "?>";
|
$output .= "?>";
|
||||||
|
|
||||||
return $output;
|
return $output;
|
||||||
@@ -266,12 +276,13 @@ class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
|
|||||||
// check and get attributes
|
// check and get attributes
|
||||||
$_attr = $this->getAttributes($compiler, $args);
|
$_attr = $this->getAttributes($compiler, $args);
|
||||||
|
|
||||||
list($openTag, $nocache, $local, $itemVar, $foo) = $this->closeTag($compiler, array('foreach'));
|
list($openTag, $nocache, $local, $itemVar, $restore) = $this->closeTag($compiler, array('foreach'));
|
||||||
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, false));
|
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $local, $itemVar, 0));
|
||||||
$output = "<?php\n";
|
$output = "<?php\n";
|
||||||
$output .= "{$itemVar} = {$local}saved;\n";
|
if ($restore == 2) {
|
||||||
$output .= "}\n";
|
$output .= "{$itemVar} = {$local}saved;\n";
|
||||||
$output .= "if (!{$itemVar}->_loop) {\n?>";
|
}
|
||||||
|
$output .= "}\n} else {\n?>\n";
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -305,12 +316,15 @@ class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
|
|||||||
$this->closeTag($compiler, array('foreach', 'foreachelse'));
|
$this->closeTag($compiler, array('foreach', 'foreachelse'));
|
||||||
$output = "<?php\n";
|
$output = "<?php\n";
|
||||||
|
|
||||||
if ($restore) {
|
if ($restore == 2) {
|
||||||
$output .= "{$itemVar} = {$local}saved;\n";
|
$output .= "{$itemVar} = {$local}saved;\n";
|
||||||
}
|
}
|
||||||
|
if ($restore > 0) {
|
||||||
|
$output .= "}\n";
|
||||||
|
}
|
||||||
$output .= "}\n";
|
$output .= "}\n";
|
||||||
$output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n";
|
$output .= "\$_smarty_tpl->smarty->ext->_foreach->restore(\$_smarty_tpl);\n";
|
||||||
$output .= "?>";
|
$output .= "?>\n";
|
||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -38,24 +38,35 @@ class Smarty_Internal_Runtime_Foreach
|
|||||||
$properties = array())
|
$properties = array())
|
||||||
{
|
{
|
||||||
$saveVars = array();
|
$saveVars = array();
|
||||||
if (isset($tpl->tpl_vars[ $item ])) {
|
|
||||||
$saveVars[ $item ] = $tpl->tpl_vars[ $item ];
|
|
||||||
}
|
|
||||||
if ($key) {
|
|
||||||
if (isset($tpl->tpl_vars[ $key ])) {
|
|
||||||
$saveVars[ $key ] = $tpl->tpl_vars[ $key ];
|
|
||||||
}
|
|
||||||
$tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache);
|
|
||||||
}
|
|
||||||
if (!is_array($from) && !is_object($from)) {
|
if (!is_array($from) && !is_object($from)) {
|
||||||
settype($from, 'array');
|
settype($from, 'array');
|
||||||
}
|
}
|
||||||
$total = $needTotal ? $this->count($from) : 1;
|
$total = ($needTotal || isset($properties[ 'total' ])) ? $this->count($from) : 1;
|
||||||
$tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache);
|
if (empty($from)) {
|
||||||
if ($needTotal) {
|
$from = null;
|
||||||
$tpl->tpl_vars[ $item ]->total = $total;
|
$total = 0;
|
||||||
|
if ($needTotal) {
|
||||||
|
if (isset($tpl->tpl_vars[ $item ])) {
|
||||||
|
$saveVars[ $item ] = $tpl->tpl_vars[ $item ];
|
||||||
|
}
|
||||||
|
$tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache);
|
||||||
|
$tpl->tpl_vars[ $item ]->total = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isset($tpl->tpl_vars[ $item ])) {
|
||||||
|
$saveVars[ $item ] = $tpl->tpl_vars[ $item ];
|
||||||
|
}
|
||||||
|
if ($key) {
|
||||||
|
if (isset($tpl->tpl_vars[ $key ])) {
|
||||||
|
$saveVars[ $key ] = $tpl->tpl_vars[ $key ];
|
||||||
|
}
|
||||||
|
$tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache);
|
||||||
|
}
|
||||||
|
$tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache);
|
||||||
|
if ($needTotal) {
|
||||||
|
$tpl->tpl_vars[ $item ]->total = $total;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$tpl->tpl_vars[ $item ]->_loop = false;
|
|
||||||
if ($name) {
|
if ($name) {
|
||||||
$namedVar = "__smarty_foreach_{$name}";
|
$namedVar = "__smarty_foreach_{$name}";
|
||||||
if (isset($tpl->tpl_vars[ $namedVar ])) {
|
if (isset($tpl->tpl_vars[ $namedVar ])) {
|
||||||
@@ -77,7 +88,6 @@ class Smarty_Internal_Runtime_Foreach
|
|||||||
$tpl->tpl_vars[ $namedVar ] = new Smarty_Variable($namedProp);
|
$tpl->tpl_vars[ $namedVar ] = new Smarty_Variable($namedProp);
|
||||||
}
|
}
|
||||||
$this->stack[] = $saveVars;
|
$this->stack[] = $saveVars;
|
||||||
|
|
||||||
return $from;
|
return $from;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user