function Smarty_Compiler::_compile_foreach_start in Quiz 6.5
Same name and namespace in other branches
- 6.6 includes/moodle/lib/smarty/Smarty_Compiler.class.php \Smarty_Compiler::_compile_foreach_start()
Compile {foreach ...} tag.
Parameters
string $tag_args:
Return value
string
1 call to Smarty_Compiler::_compile_foreach_start()
- Smarty_Compiler::_compile_tag in includes/
moodle/ lib/ smarty/ Smarty_Compiler.class.php - Compile a template tag
File
- includes/
moodle/ lib/ smarty/ Smarty_Compiler.class.php, line 1142
Class
- Smarty_Compiler
- Template compiling class @package Smarty
Code
function _compile_foreach_start($tag_args) {
$attrs = $this
->_parse_attrs($tag_args);
$arg_list = array();
if (empty($attrs['from'])) {
return $this
->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
$from = $attrs['from'];
if (empty($attrs['item'])) {
return $this
->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
$item = $this
->_dequote($attrs['item']);
if (!preg_match('~^\\w+$~', $item)) {
return $this
->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
if (isset($attrs['key'])) {
$key = $this
->_dequote($attrs['key']);
if (!preg_match('~^\\w+$~', $key)) {
return $this
->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
$key_part = "\$this->_tpl_vars['{$key}'] => ";
}
else {
$key = null;
$key_part = '';
}
if (isset($attrs['name'])) {
$name = $attrs['name'];
}
else {
$name = null;
}
$output = '<?php ';
$output .= "\$_from = {$from}; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
if (isset($name)) {
$foreach_props = "\$this->_foreach[{$name}]";
$output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
$output .= "if ({$foreach_props}['total'] > 0):\n";
$output .= " foreach (\$_from as {$key_part}\$this->_tpl_vars['{$item}']):\n";
$output .= " {$foreach_props}['iteration']++;\n";
}
else {
$output .= "if (count(\$_from)):\n";
$output .= " foreach (\$_from as {$key_part}\$this->_tpl_vars['{$item}']):\n";
}
$output .= '?>';
return $output;
}