mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-07 03:44:26 +02:00
- replaced new array looping syntax {for $foo in $array} with {foreach $foo in $array} to avoid confusion
This commit is contained in:
@@ -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
|
||||
|
@@ -89,33 +89,33 @@ td {
|
||||
<h2>included templates & 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>
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
@@ -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
|
||||
|
@@ -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 .= "?>";
|
||||
|
||||
|
@@ -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 { ?>";
|
||||
}
|
||||
|
Reference in New Issue
Block a user