- replaced new array looping syntax {for $foo in $array} with {foreach $foo in $array} to avoid confusion

This commit is contained in:
Uwe.Tews
2009-04-05 17:56:04 +00:00
parent 827e9b08d8
commit fa3a23dace
6 changed files with 44 additions and 61 deletions

View File

@@ -1,4 +1,5 @@
04/05/2009 04/05/2009
- replaced new array looping syntax {for $foo in $array} with {foreach $foo in $array} to avoid confusion
- added append array for short form of assign {$foo[]='bar'} and allow assignments to nested arrays {$foo['bla']['blue']='bar'} - added append array for short form of assign {$foo[]='bar'} and allow assignments to nested arrays {$foo['bla']['blue']='bar'}
04/04/2009 04/04/2009

View File

@@ -89,33 +89,33 @@ td {
<h2>included templates &amp; config files (load time in seconds)</h2> <h2>included templates &amp; config files (load time in seconds)</h2>
<div> <div>
{for $template in $template_data} {foreach $template in $template_data}
<font color=brown>{$template.name}</font> <font color=brown>{$template.name}</font>
<span class="exectime"> <span class="exectime">
(compile {$template['compile_time']|string_format:"%.5f"}) (render {$template['render_time']|string_format:"%.5f"}) (cache {$template['cache_time']|string_format:"%.5f"}) (compile {$template['compile_time']|string_format:"%.5f"}) (render {$template['render_time']|string_format:"%.5f"}) (cache {$template['cache_time']|string_format:"%.5f"})
</span> </span>
<br> <br>
{/for} {/foreach}
</div> </div>
<h2>assigned template variables</h2> <h2>assigned template variables</h2>
<table id="table_assigned_vars"> <table id="table_assigned_vars">
{for $vars in $assigned_vars} {foreach $vars in $assigned_vars}
<tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}"> <tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}">
<th>${$vars@key|escape:'html'}</th> <th>${$vars@key|escape:'html'}</th>
<td>{$vars|debug_print_var}</td></tr> <td>{$vars|debug_print_var}</td></tr>
{/for} {/foreach}
</table> </table>
<h2>assigned config file variables (outer template scope)</h2> <h2>assigned config file variables (outer template scope)</h2>
<table id="table_config_vars"> <table id="table_config_vars">
{for $vars in $config_vars} {foreach $vars in $config_vars}
<tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}"> <tr class="{if $vars@iteration % 2 eq 0}odd{else}even{/if}">
<th>{$vars@key|escape:'html'}</th> <th>{$vars@key|escape:'html'}</th>
<td>{$vars|debug_print_var}</td></tr> <td>{$vars|debug_print_var}</td></tr>
{/for} {/foreach}
</table> </table>
</body> </body>

View File

@@ -33,33 +33,6 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
public function compile($args, $compiler) public function compile($args, $compiler)
{ {
$this->compiler = $compiler; $this->compiler = $compiler;
if (isset($args['from'])) {
// {for $var in $array} syntax
$this->required_attributes = array('from', 'item');
// check and get attributes
$_attr = $this->_get_attributes($args);
$this->_open_tag('forarray');
$from = $_attr['from'];
$item = $_attr['item'];
$output = "<?php ";
$output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
$output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->total=count(\$_from);\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
$output .= "if (\$_smarty_tpl->tpl_vars[$item]->total > 0){\n";
$output .= " foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->iteration === 0;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
$output .= "?>";
// return compiled code
return $output;
} else {
// {for $x=0; $x<$y; $x++} syntax // {for $x=0; $x<$y; $x++} syntax
$this->required_attributes = array('ifexp', 'start', 'loop', 'varloop'); $this->required_attributes = array('ifexp', 'start', 'loop', 'varloop');
// check and get attributes // check and get attributes
@@ -77,6 +50,6 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
// return compiled code // return compiled code
return $output; return $output;
} }
}
} }
?> ?>

View File

@@ -25,7 +25,7 @@ class Smarty_Internal_Compile_ForClose extends Smarty_Internal_CompileBase {
// check and get attributes // check and get attributes
$_attr = $this->_get_attributes($args); $_attr = $this->_get_attributes($args);
$_open_tag = $this->_close_tag(array('for', 'forarray', 'forelse')); $_open_tag = $this->_close_tag(array('for', 'forelse'));
if ($_open_tag == 'forelse') if ($_open_tag == 'forelse')
return "<?php } ?>"; return "<?php } ?>";
else else

View File

@@ -3,6 +3,7 @@
* Smarty Internal Plugin Compile Foreach * Smarty Internal Plugin Compile Foreach
* *
* Compiles the {foreach} tag * Compiles the {foreach} tag
*
* @package Smarty * @package Smarty
* @subpackage Compiler * @subpackage Compiler
* @author Uwe Tews * @author Uwe Tews
@@ -33,10 +34,8 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
if (isset($_attr['key'])) { if (isset($_attr['key'])) {
$key = $_attr['key']; $key = $_attr['key'];
$key_part = "\$_smarty_tpl->tpl_vars[$key]->value => ";
} else { } else {
$key = null; $key = null;
$key_part = '';
} }
if (isset($_attr['name'])) { if (isset($_attr['name'])) {
@@ -50,18 +49,28 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
$output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n"; $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
} }
$output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n"; $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->total=count(\$_from);\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
if ($name != null) { if ($name != null) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = count(\$_from);\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
} }
$output .= "if (count(\$_from) > 0){\n"; $output .= "if (count(\$_from) > 0){\n";
$output .= " foreach (\$_from as " . $key_part . "\$_smarty_tpl->tpl_vars[$item]->value){\n"; $output .= " foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
if ($key != null) {
$output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
}
$output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->iteration === 0;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
$output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
if ($name != null) { if ($name != null) {
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']===0;\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=== \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'];\n"; $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
} }
$output .= "?>"; $output .= "?>";

View File

@@ -25,7 +25,7 @@ class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase {
// check and get attributes // check and get attributes
$_attr = $this->_get_attributes($args); $_attr = $this->_get_attributes($args);
$this->_close_tag(array('for', 'forarray')); $this->_close_tag(array('for'));
$this->_open_tag('forelse'); $this->_open_tag('forelse');
return "<?php }} else { ?>"; return "<?php }} else { ?>";
} }