2009-11-06 14:35:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Smarty Internal Plugin Compile Foreach
|
|
|
|
* Compiles the {foreach} {foreachelse} {/foreach} tags
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2010-04-28 20:30:27 +00:00
|
|
|
* @subpackage Compiler
|
2014-06-06 02:40:04 +00:00
|
|
|
* @author Uwe Tews
|
2010-04-28 20:30:27 +00:00
|
|
|
*/
|
2010-08-17 15:39:51 +00:00
|
|
|
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Smarty Internal Plugin Compile Foreach Class
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2011-09-16 14:19:56 +00:00
|
|
|
* @subpackage Compiler
|
2010-04-28 20:30:27 +00:00
|
|
|
*/
|
2013-07-14 22:15:45 +00:00
|
|
|
class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2011-09-16 14:19:56 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2010-11-11 21:34:36 +00:00
|
|
|
public $required_attributes = array('from', 'item');
|
2015-07-01 03:30:08 +02:00
|
|
|
|
2011-09-16 14:19:56 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
|
|
|
public $optional_attributes = array('name', 'key');
|
2015-07-01 03:30:08 +02:00
|
|
|
|
2011-09-16 14:19:56 +00:00
|
|
|
/**
|
|
|
|
* Attribute definition: Overwrites base class.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @see Smarty_Internal_CompileBase
|
|
|
|
*/
|
2014-06-06 02:40:04 +00:00
|
|
|
public $shorttag_order = array('from', 'item', 'key', 'name');
|
2010-11-11 21:34:36 +00:00
|
|
|
|
2015-07-01 03:30:08 +02:00
|
|
|
/**
|
|
|
|
* Foreach counter
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $foreach_number = 0;
|
|
|
|
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Compiles code for the {foreach} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2015-07-01 03:30:08 +02:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2010-04-28 20:30:27 +00:00
|
|
|
* @return string compiled code
|
2015-07-01 03:30:08 +02:00
|
|
|
* @throws \SmartyCompilerException
|
2010-04-28 20:30:27 +00:00
|
|
|
*/
|
2015-07-01 03:30:08 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2009-11-06 14:35:00 +00:00
|
|
|
{
|
|
|
|
// check and get attributes
|
2011-09-16 14:19:56 +00:00
|
|
|
$_attr = $this->getAttributes($compiler, $args);
|
2009-11-06 14:35:00 +00:00
|
|
|
$from = $_attr['from'];
|
2015-07-01 03:30:08 +02:00
|
|
|
$item = $compiler->getId($_attr['item']);
|
|
|
|
if ($item === false) {
|
|
|
|
$item = $compiler->getVariableName($_attr['item']);
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
$attributes = array('item' => $item);
|
2009-11-06 14:35:00 +00:00
|
|
|
if (isset($_attr['key'])) {
|
2015-07-01 03:30:08 +02:00
|
|
|
$key = $compiler->getId($_attr['key']);
|
|
|
|
if ($key === false) {
|
|
|
|
$key = $compiler->getVariableName($_attr['key']);
|
|
|
|
}
|
|
|
|
$attributes['key'] = $key;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($_attr['name'])) {
|
|
|
|
$attributes['name'] = $compiler->getId($_attr['name']);
|
|
|
|
}
|
|
|
|
foreach ($attributes as $a => $v) {
|
|
|
|
if ($v === false) {
|
|
|
|
$compiler->trigger_template_error("'{$a}' attribute/variable has illegal value", $compiler->lex->taglineno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$fromName = $compiler->getVariableName($_attr['from']);
|
|
|
|
if ($fromName) {
|
|
|
|
foreach (array('item', 'key') as $a) {
|
|
|
|
if (isset($attributes[$a]) && $attributes[$a] == $fromName) {
|
|
|
|
$compiler->trigger_template_error("'{$a}' and 'from' may not have same variable name '{$fromName}'", $compiler->lex->taglineno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$attributes['no'] = $this->foreach_number ++ . '_' . (isset($attributes['name']) ? $attributes['name'] : $attributes['item']);
|
|
|
|
$this->openTag($compiler, 'foreach', array('foreach', $compiler->nocache, $attributes, true));
|
2010-05-10 20:08:24 +00:00
|
|
|
// maybe nocache because of nocache variables
|
2011-09-16 14:19:56 +00:00
|
|
|
$compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
|
2010-05-10 20:08:24 +00:00
|
|
|
|
2015-07-01 03:30:08 +02:00
|
|
|
// prepare preg
|
|
|
|
if ($has_name = isset($attributes['name'])) {
|
|
|
|
$smartyPreg = "|([\$]smarty[.]foreach[.]{$attributes['name']}[.]((first)|(last)|(index)|(iteration)|(show)|(total)))";
|
2009-11-06 14:35:00 +00:00
|
|
|
} else {
|
2015-07-01 03:30:08 +02:00
|
|
|
$smartyPreg = '';
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
$itemPreg = "([\$]{$attributes['item']}[@]((first)|(last)|(index)|(iteration)|(show)|(total)|(key)))";
|
|
|
|
$preg = '~(' . $itemPreg . $smartyPreg . ')\W~i';
|
|
|
|
$itemAttr = array();
|
|
|
|
$smartyAttr = array();
|
|
|
|
|
|
|
|
// search template source
|
|
|
|
preg_match_all($preg, $compiler->lex->data, $match, PREG_SET_ORDER);
|
|
|
|
foreach ($match as $m) {
|
|
|
|
if (isset($m[3]) && !empty($m[3])) {
|
|
|
|
$itemAttr[strtolower($m[3])] = true;
|
|
|
|
}
|
|
|
|
if ($has_name && isset($m[12]) && !empty($m[12])) {
|
|
|
|
$smartyAttr[strtolower($m[12])] = true;
|
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 03:30:08 +02:00
|
|
|
// search {block} sources
|
|
|
|
foreach ($compiler->template->block_data as $b) {
|
|
|
|
if (isset($b['source'])) {
|
|
|
|
preg_match_all($preg, $b['source'], $match, PREG_SET_ORDER);
|
|
|
|
foreach ($match as $m) {
|
|
|
|
if (isset($m[3]) && !empty($m[3])) {
|
|
|
|
$itemAttr[strtolower($m[3])] = true;
|
|
|
|
}
|
|
|
|
if ($has_name && isset($m[12]) && !empty($m[12])) {
|
|
|
|
$smartyAttr[strtolower($m[12])] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (class_exists('Smarty_Internal_Compile_Block', false)) {
|
|
|
|
foreach (Smarty_Internal_Compile_Block::$block_data as $b) {
|
|
|
|
if (isset($b['source'])) {
|
|
|
|
preg_match_all($preg, $b['source'], $match, PREG_SET_ORDER);
|
|
|
|
foreach ($match as $m) {
|
|
|
|
if (isset($m[3]) && !empty($m[3])) {
|
|
|
|
$itemAttr[strtolower($m[3])] = true;
|
|
|
|
}
|
|
|
|
if ($has_name && isset($m[12]) && !empty($m[12])) {
|
|
|
|
$smartyAttr[strtolower($m[12])] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 13:01:16 +02:00
|
|
|
// search parent compiler template source
|
|
|
|
$nextCompiler = $compiler;
|
|
|
|
while ($nextCompiler !== $nextCompiler->parent_compiler) {
|
|
|
|
$nextCompiler = $nextCompiler->parent_compiler;
|
2015-07-06 03:25:03 +02:00
|
|
|
preg_match_all($preg, $nextCompiler->template->source->getContent(), $match, PREG_SET_ORDER);
|
2015-07-01 13:01:16 +02:00
|
|
|
foreach ($match as $m) {
|
|
|
|
if (isset($m[3]) && !empty($m[3])) {
|
|
|
|
$itemAttr[strtolower($m[3])] = true;
|
|
|
|
}
|
|
|
|
if ($has_name && isset($m[12]) && !empty($m[12])) {
|
|
|
|
$smartyAttr[strtolower($m[12])] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 03:30:08 +02:00
|
|
|
if (!isset($itemAttr['index']) && (isset($itemAttr['first']) || isset($itemAttr['last']))) {
|
|
|
|
$itemAttr['iteration'] = true;
|
|
|
|
}
|
|
|
|
if (isset($itemAttr['last'])) {
|
|
|
|
$itemAttr['total'] = true;
|
|
|
|
}
|
|
|
|
if ($has_name) {
|
|
|
|
if (!isset($smartyAttr['index']) && (isset($smartyAttr['first']) || isset($smartyAttr['last']))) {
|
|
|
|
$smartyAttr['iteration'] = true;
|
|
|
|
}
|
|
|
|
if (isset($smartyAttr['last'])) {
|
|
|
|
$smartyAttr['total'] = true;
|
|
|
|
}
|
|
|
|
}
|
2015-02-15 16:58:42 +01:00
|
|
|
|
|
|
|
$keyTerm = '';
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['key'])) {
|
|
|
|
$keyTerm = "\$_smarty_tpl->tpl_vars['{$item}']->key => ";
|
|
|
|
} elseif (isset($attributes['key'])) {
|
|
|
|
$keyTerm = "\$_smarty_tpl->tpl_vars['{$key}']->value => ";
|
2015-02-15 16:58:42 +01:00
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
// generate output code
|
2015-02-15 16:58:42 +01:00
|
|
|
$output = "<?php\n";
|
2015-07-01 03:30:08 +02:00
|
|
|
foreach (array('item', 'key') as $a) {
|
|
|
|
if (isset($attributes[$a])) {
|
|
|
|
$output .= "\$foreach_{$attributes['no']}_sav['s_{$a}'] = isset(\$_smarty_tpl->tpl_vars['{$attributes[$a]}']) ? \$_smarty_tpl->tpl_vars['{$attributes[$a]}'] : false;\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($attributes['name'])) {
|
|
|
|
$output .= "\$foreach_{$attributes['no']}_sav['s_name'] = isset(\$_smarty_tpl->tpl_vars['__foreach_{$attributes['name']}']) ? \$_smarty_tpl->tpl_vars['__foreach_{$attributes['name']}'] : false;\n";
|
|
|
|
}
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "\$_from = $from;\n";
|
|
|
|
$output .= "if (!is_array(\$_from) && !is_object(\$_from)) {\n";
|
|
|
|
$output .= "settype(\$_from, 'array');\n";
|
|
|
|
$output .= "}\n";
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}'] = new Smarty_Variable;\n";
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->_loop = false;\n";
|
|
|
|
if (isset($attributes['key'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$key}'] = new Smarty_Variable;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['total'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->total= \$_smarty_tpl->_count(\$_from);\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['iteration'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->iteration=0;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['index'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->index=-1;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['show'])) {
|
|
|
|
if (isset($itemAttr['total'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->show = (\$_smarty_tpl->tpl_vars['{$item}']->total > 0);\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
} else {
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->show = (\$_smarty_tpl->_count(\$_from) > 0);\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
if ($has_name) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop = array();
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['total'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop['total'] = "'total' => ";
|
2015-07-01 03:30:08 +02:00
|
|
|
$prop['total'] .= isset($smartyAttr['show']) ? '$total = ' : '';
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop['total'] .= '$_smarty_tpl->_count($_from)';
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['iteration'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop['iteration'] = "'iteration' => 0";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['index'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop['index'] = "'index' => -1";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['show'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop['show'] = "'show' => ";
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['total'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$prop['show'] .= "(\$total > 0)";
|
|
|
|
} else {
|
|
|
|
$prop['show'] .= "(\$_smarty_tpl->_count(\$_from) > 0)";
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (!empty($smartyAttr)) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$_vars = 'array(' . join(', ', $prop) . ')';
|
2015-07-01 03:30:08 +02:00
|
|
|
$foreachVar = "'__foreach_{$attributes['name']}'";
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar] = new Smarty_Variable({$_vars});\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "foreach (\$_from as {$keyTerm}\$_smarty_tpl->tpl_vars['{$item}']->value) {\n";
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->_loop = true;\n";
|
|
|
|
if (isset($attributes['key']) && isset($itemAttr['key'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$key}']->value = \$_smarty_tpl->tpl_vars['{$item}']->key;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['iteration'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->iteration++;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['index'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->index++;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['first'])) {
|
|
|
|
if (isset($itemAttr['index'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->first = \$_smarty_tpl->tpl_vars['{$item}']->index == 0;\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
} else {
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->first = \$_smarty_tpl->tpl_vars['{$item}']->iteration == 1;\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($itemAttr['last'])) {
|
|
|
|
if (isset($itemAttr['index'])) {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->last = \$_smarty_tpl->tpl_vars['{$item}']->index + 1 == \$_smarty_tpl->tpl_vars['{$item}']->total;\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
} else {
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$item}']->last = \$_smarty_tpl->tpl_vars['{$item}']->iteration == \$_smarty_tpl->tpl_vars['{$item}']->total;\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
if ($has_name) {
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['iteration'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration']++;\n";
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['index'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['index']++;\n";
|
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['first'])) {
|
|
|
|
if (isset($smartyAttr['index'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] == 0;\n";
|
|
|
|
} else {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['first'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == 1;\n";
|
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
if (isset($smartyAttr['last'])) {
|
|
|
|
if (isset($smartyAttr['index'])) {
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['index'] + 1 == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
|
|
|
|
} else {
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars[$foreachVar]->value['last'] = \$_smarty_tpl->tpl_vars[$foreachVar]->value['iteration'] == \$_smarty_tpl->tpl_vars[$foreachVar]->value['total'];\n";
|
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$foreach_{$attributes['no']}_sav['item'] = \$_smarty_tpl->tpl_vars['{$item}'];\n";
|
2009-11-06 14:35:00 +00:00
|
|
|
$output .= "?>";
|
|
|
|
|
|
|
|
return $output;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2015-07-01 10:46:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compiles code for the {$smarty.foreach} tag
|
|
|
|
*
|
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
|
|
|
*
|
|
|
|
* @return string compiled code
|
|
|
|
* @throws \SmartyCompilerException
|
|
|
|
*/
|
|
|
|
public static function compileSpecialVariable($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
|
|
|
{
|
|
|
|
if (!isset($parameter[1]) || false === $name = $compiler->getId($parameter[1])) {
|
|
|
|
$compiler->trigger_template_error("missing or illegal \$Smarty.foreach name attribute", $compiler->lex->taglineno);
|
|
|
|
}
|
|
|
|
if ((!isset($parameter[2]) || false === $property = $compiler->getId($parameter[2])) || !in_array(strtolower($property), array('first', 'last', 'index', 'iteration', 'show', 'total'))) {
|
|
|
|
$compiler->trigger_template_error("missing or illegal \$Smarty.foreach property attribute", $compiler->lex->taglineno);
|
|
|
|
}
|
|
|
|
$property = strtolower($property);
|
|
|
|
$foreachVar = "'__foreach_{$name}'";
|
|
|
|
return "(isset(\$_smarty_tpl->tpl_vars[{$foreachVar}]->value['{$property}']) ? \$_smarty_tpl->tpl_vars[{$foreachVar}]->value['{$property}'] : null)";
|
|
|
|
}
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Smarty Internal Plugin Compile Foreachelse Class
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2011-09-16 14:19:56 +00:00
|
|
|
* @subpackage Compiler
|
2010-04-28 20:30:27 +00:00
|
|
|
*/
|
2013-07-14 22:15:45 +00:00
|
|
|
class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Compiles code for the {foreachelse} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2015-07-01 03:30:08 +02:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2010-04-28 20:30:27 +00:00
|
|
|
* @return string compiled code
|
|
|
|
*/
|
2015-07-01 03:30:08 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2009-11-06 14:35:00 +00:00
|
|
|
{
|
|
|
|
// check and get attributes
|
2011-09-16 14:19:56 +00:00
|
|
|
$_attr = $this->getAttributes($compiler, $args);
|
2009-11-06 14:35:00 +00:00
|
|
|
|
2015-07-01 03:30:08 +02:00
|
|
|
list($openTag, $nocache, $attributes, $foo) = $this->closeTag($compiler, array('foreach'));
|
|
|
|
$this->openTag($compiler, 'foreachelse', array('foreachelse', $nocache, $attributes, false));
|
2015-02-15 16:58:42 +01:00
|
|
|
$output = "<?php\n";
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$attributes['item']}'] = \$foreach_{$attributes['no']}_sav['item'];\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
$output .= "}\n";
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "if (!\$_smarty_tpl->tpl_vars['{$attributes['item']}']->_loop) {\n?>";
|
2015-02-15 16:58:42 +01:00
|
|
|
return $output;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
|
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Smarty Internal Plugin Compile Foreachclose Class
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2014-06-06 02:40:04 +00:00
|
|
|
* @package Smarty
|
2011-09-16 14:19:56 +00:00
|
|
|
* @subpackage Compiler
|
2010-04-28 20:30:27 +00:00
|
|
|
*/
|
2013-07-14 22:15:45 +00:00
|
|
|
class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase
|
|
|
|
{
|
2009-11-06 14:35:00 +00:00
|
|
|
/**
|
2010-04-28 20:30:27 +00:00
|
|
|
* Compiles code for the {/foreach} tag
|
2011-09-16 14:19:56 +00:00
|
|
|
*
|
2015-07-01 03:30:08 +02:00
|
|
|
* @param array $args array with attributes from parser
|
|
|
|
* @param \Smarty_Internal_TemplateCompilerBase $compiler compiler object
|
|
|
|
* @param array $parameter array with compilation parameter
|
2014-06-06 02:40:04 +00:00
|
|
|
*
|
2010-04-28 20:30:27 +00:00
|
|
|
* @return string compiled code
|
|
|
|
*/
|
2015-07-01 03:30:08 +02:00
|
|
|
public function compile($args, Smarty_Internal_TemplateCompilerBase $compiler, $parameter)
|
2009-11-06 14:35:00 +00:00
|
|
|
{
|
|
|
|
// check and get attributes
|
2011-09-16 14:19:56 +00:00
|
|
|
$_attr = $this->getAttributes($compiler, $args);
|
2010-04-28 20:30:27 +00:00
|
|
|
// must endblock be nocache?
|
2011-09-16 14:19:56 +00:00
|
|
|
if ($compiler->nocache) {
|
|
|
|
$compiler->tag_nocache = true;
|
|
|
|
}
|
2009-11-06 14:35:00 +00:00
|
|
|
|
2015-07-01 03:30:08 +02:00
|
|
|
list($openTag, $compiler->nocache, $attributes, $restore) = $this->closeTag($compiler, array('foreach',
|
|
|
|
'foreachelse'));
|
2015-02-15 16:58:42 +01:00
|
|
|
$output = "<?php\n";
|
|
|
|
if ($restore) {
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$attributes['item']}'] = \$foreach_{$attributes['no']}_sav['item'];\n";
|
2015-02-15 16:58:42 +01:00
|
|
|
}
|
2015-07-01 03:30:08 +02:00
|
|
|
$output .= "}\n";
|
|
|
|
foreach (array('item', 'key') as $a) {
|
|
|
|
if (isset($attributes[$a])) {
|
|
|
|
$output .= "if (\$foreach_{$attributes['no']}_sav['s_{$a}']) {\n";
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['{$attributes[$a]}'] = \$foreach_{$attributes['no']}_sav['s_{$a}'];\n";
|
|
|
|
$output .= "}\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isset($attributes['name'])) {
|
|
|
|
$output .= "if (\$foreach_{$attributes['no']}_sav['s_name']) {\n";
|
|
|
|
$output .= "\$_smarty_tpl->tpl_vars['__foreach_{$attributes['name']}'] = \$foreach_{$attributes['no']}_sav['s_name'];\n";
|
|
|
|
$output .= "}\n";
|
|
|
|
}
|
|
|
|
$output .= "?>";
|
2011-09-16 14:19:56 +00:00
|
|
|
|
2015-02-15 16:58:42 +01:00
|
|
|
return $output;
|
2011-09-16 14:19:56 +00:00
|
|
|
}
|
|
|
|
}
|