2009-04-05 17:56:04 +00:00
|
|
|
<?php
|
2009-03-22 16:09:05 +00:00
|
|
|
/**
|
|
|
|
* Smarty Internal Plugin Compile Foreach
|
2009-04-05 17:56:04 +00:00
|
|
|
*
|
|
|
|
* Compiles the {foreach} tag
|
|
|
|
*
|
2009-03-22 16:09:05 +00:00
|
|
|
* @package Smarty
|
|
|
|
* @subpackage Compiler
|
2009-04-05 17:56:04 +00:00
|
|
|
* @author Uwe Tews
|
2009-03-22 16:09:05 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Smarty Internal Plugin Compile Foreach Class
|
2009-04-05 17:56:04 +00:00
|
|
|
*/
|
2009-03-22 16:09:05 +00:00
|
|
|
class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
|
|
|
|
/**
|
|
|
|
* Compiles code for the {foreach} tag
|
|
|
|
*
|
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param object $compiler compiler object
|
|
|
|
* @return string compiled code
|
|
|
|
*/
|
|
|
|
public function compile($args, $compiler)
|
|
|
|
{
|
2009-04-05 17:56:04 +00:00
|
|
|
$this->compiler = $compiler;
|
2009-03-22 16:09:05 +00:00
|
|
|
$this->required_attributes = array('from', 'item');
|
|
|
|
$this->optional_attributes = array('name', 'key');
|
|
|
|
// check and get attributes
|
|
|
|
$_attr = $this->_get_attributes($args);
|
|
|
|
|
|
|
|
$this->_open_tag('foreach');
|
|
|
|
|
|
|
|
$from = $_attr['from'];
|
|
|
|
$item = $_attr['item'];
|
|
|
|
|
|
|
|
if (isset($_attr['key'])) {
|
|
|
|
$key = $_attr['key'];
|
|
|
|
} else {
|
|
|
|
$key = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_attr['name'])) {
|
|
|
|
$name = $_attr['name'];
|
|
|
|
} else {
|
|
|
|
$name = null;
|
|
|
|
}
|
|
|
|
$output = "<?php ";
|
|
|
|
$output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
|
|
|
|
if ($key != null) {
|
|
|
|
$output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
|
|
|
|
}
|
2009-03-23 15:29:50 +00:00
|
|
|
$output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
|
2009-04-05 17:56:04 +00:00
|
|
|
$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";
|
2009-03-22 16:09:05 +00:00
|
|
|
if ($name != null) {
|
2009-04-05 17:56:04 +00:00
|
|
|
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
|
2009-03-22 16:09:05 +00:00
|
|
|
$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";
|
2009-04-05 17:56:04 +00:00
|
|
|
$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";
|
2009-03-22 16:09:05 +00:00
|
|
|
if ($name != null) {
|
2009-04-05 17:56:04 +00:00
|
|
|
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
|
2009-03-22 16:09:05 +00:00
|
|
|
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
|
|
|
|
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
|
2009-04-05 17:56:04 +00:00
|
|
|
$output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
|
2009-03-22 16:09:05 +00:00
|
|
|
}
|
|
|
|
$output .= "?>";
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|