- 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
- 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'}
04/04/2009

View File

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

View File

@@ -33,50 +33,23 @@ class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase {
public function compile($args, $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);
// {for $x=0; $x<$y; $x++} syntax
$this->required_attributes = array('ifexp', 'start', 'loop', 'varloop');
// check and get attributes
$_attr = $this->_get_attributes($args);
$this->_open_tag('forarray');
$this->_open_tag('for');
$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
$this->required_attributes = array('ifexp', 'start', 'loop', 'varloop');
// check and get attributes
$_attr = $this->_get_attributes($args);
$this->_open_tag('for');
$output = "<?php ";
foreach ($_attr['start'] as $_statement) {
$output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
$output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
}
$output .= " if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[varloop]]->value$_attr[loop]){\n";
$output .= "?>";
// return compiled code
return $output;
$output = "<?php ";
foreach ($_attr['start'] as $_statement) {
$output .= " \$_smarty_tpl->tpl_vars[$_statement[var]] = new Smarty_Variable;";
$output .= " \$_smarty_tpl->tpl_vars[$_statement[var]]->value = $_statement[value];\n";
}
$output .= " if ($_attr[ifexp]){ for (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$_attr[varloop]]->value$_attr[loop]){\n";
$output .= "?>";
// return compiled code
return $output;
}
}
?>

View File

@@ -25,7 +25,7 @@ class Smarty_Internal_Compile_ForClose extends Smarty_Internal_CompileBase {
// check and get attributes
$_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')
return "<?php } ?>";
else

View File

@@ -1,15 +1,16 @@
<?php
<?php
/**
* Smarty Internal Plugin Compile Foreach
*
* Compiles the {foreach} tag
*
* Compiles the {foreach} tag
*
* @package Smarty
* @subpackage Compiler
* @author Uwe Tews
* @author Uwe Tews
*/
/**
* Smarty Internal Plugin Compile Foreach Class
*/
*/
class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {foreach} tag
@@ -20,7 +21,7 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
*/
public function compile($args, $compiler)
{
$this->compiler = $compiler;
$this->compiler = $compiler;
$this->required_attributes = array('from', 'item');
$this->optional_attributes = array('name', 'key');
// check and get attributes
@@ -33,10 +34,8 @@ class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
if (isset($_attr['key'])) {
$key = $_attr['key'];
$key_part = "\$_smarty_tpl->tpl_vars[$key]->value => ";
} else {
$key = null;
$key_part = '';
}
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 .= " \$_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) {
$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]['index']=-1;\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) {
$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]['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 .= "?>";

View File

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