From 6afb7c18a829d542f8f02f9117fa9d87f908e3e0 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Tue, 20 Sep 2011 20:10:14 +0300 Subject: [PATCH] add foreach_counter0, foreach_counter, foreach_first variables to each foreach loop in a template --- ipf/template/compiler.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ipf/template/compiler.php b/ipf/template/compiler.php index ed7ce4b..86584b2 100644 --- a/ipf/template/compiler.php +++ b/ipf/template/compiler.php @@ -292,7 +292,11 @@ class IPF_Template_Compiler $res = 'elseif('.$this->_parseFinal($args, $this->_allowedInExpr).'):'; break; case 'foreach': - $res = 'foreach ('.$this->_parseFinal($args, array(T_AS, T_DOUBLE_ARROW, T_STRING, T_OBJECT_OPERATOR), array(';','!')).'): '; + $res = + '$t->_vars[\'foreach_counter0\'] = 0;' . + '$t->_vars[\'foreach_counter\'] = 1;' . + '$t->_vars[\'foreach_first\'] = true;' . + 'foreach ('.$this->_parseFinal($args, array(T_AS, T_DOUBLE_ARROW, T_STRING, T_OBJECT_OPERATOR), array(';','!')).'): '; array_push($this->_blockStack, 'foreach'); break; case 'while': @@ -300,6 +304,16 @@ class IPF_Template_Compiler array_push($this->_blockStack, 'while'); break; case '/foreach': + if(end($this->_blockStack) != 'foreach'){ + trigger_error(sprintf(__('End tag of a block missing: %s'), end($this->_blockStack)), E_USER_ERROR); + } + array_pop($this->_blockStack); + $res = + '$t->_vars[\'foreach_counter0\'] = $t->_vars[\'foreach_counter0\'] + 1;' . + '$t->_vars[\'foreach_counter\'] = $t->_vars[\'foreach_counter\'] + 1;' . + '$t->_vars[\'foreach_first\'] = false;' . + 'endforeach; '; + break; case '/if': case '/while': $short = substr($name,1); -- 2.49.0