From fa3a23daceafa2f3332429f3cc9cef2d64cd34b2 Mon Sep 17 00:00:00 2001 From: "Uwe.Tews" Date: Sun, 5 Apr 2009 17:56:04 +0000 Subject: [PATCH] - replaced new array looping syntax {for $foo in $array} with {foreach $foo in $array} to avoid confusion --- change_log.txt | 1 + libs/debug.tpl | 12 ++-- libs/sysplugins/internal.compile_for.php | 55 +++++-------------- libs/sysplugins/internal.compile_forclose.php | 2 +- libs/sysplugins/internal.compile_foreach.php | 33 +++++++---- libs/sysplugins/internal.compile_forelse.php | 2 +- 6 files changed, 44 insertions(+), 61 deletions(-) diff --git a/change_log.txt b/change_log.txt index 32942ab0..af57575a 100644 --- a/change_log.txt +++ b/change_log.txt @@ -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 diff --git a/libs/debug.tpl b/libs/debug.tpl index 585573c4..97fd8deb 100644 --- a/libs/debug.tpl +++ b/libs/debug.tpl @@ -89,33 +89,33 @@ td {

included templates & config files (load time in seconds)

-{for $template in $template_data} +{foreach $template in $template_data} {$template.name} (compile {$template['compile_time']|string_format:"%.5f"}) (render {$template['render_time']|string_format:"%.5f"}) (cache {$template['cache_time']|string_format:"%.5f"})
-{/for} +{/foreach}

assigned template variables

- {for $vars in $assigned_vars} + {foreach $vars in $assigned_vars} - {/for} + {/foreach}
${$vars@key|escape:'html'} {$vars|debug_print_var}

assigned config file variables (outer template scope)

- {for $vars in $config_vars} + {foreach $vars in $config_vars} - {/for} + {/foreach}
{$vars@key|escape:'html'} {$vars|debug_print_var}
diff --git a/libs/sysplugins/internal.compile_for.php b/libs/sysplugins/internal.compile_for.php index f37f44c0..17055a37 100644 --- a/libs/sysplugins/internal.compile_for.php +++ b/libs/sysplugins/internal.compile_for.php @@ -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 = "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 = "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 = "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; } } + ?> diff --git a/libs/sysplugins/internal.compile_forclose.php b/libs/sysplugins/internal.compile_forclose.php index 9b35b45b..7b20e0d1 100644 --- a/libs/sysplugins/internal.compile_forclose.php +++ b/libs/sysplugins/internal.compile_forclose.php @@ -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 ""; else diff --git a/libs/sysplugins/internal.compile_foreach.php b/libs/sysplugins/internal.compile_foreach.php index 99ed37bc..207762f1 100644 --- a/libs/sysplugins/internal.compile_foreach.php +++ b/libs/sysplugins/internal.compile_foreach.php @@ -1,15 +1,16 @@ -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 .= "?>"; diff --git a/libs/sysplugins/internal.compile_forelse.php b/libs/sysplugins/internal.compile_forelse.php index 467e40a4..f017d01d 100644 --- a/libs/sysplugins/internal.compile_forelse.php +++ b/libs/sysplugins/internal.compile_forelse.php @@ -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 ""; }