PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/smarty3/lib/libs/sysplugins/smarty_internal_compile_foreach.php

http://zoop.googlecode.com/
PHP | 192 lines | 132 code | 14 blank | 46 comment | 39 complexity | a3ff9981aca6a21cbe3a41e969b0c7b8 MD5 | raw file
  1<?php
  2/**
  3* Smarty Internal Plugin Compile Foreach
  4* 
  5* Compiles the {foreach} {foreachelse} {/foreach} tags
  6* 
  7* @package Smarty
  8* @subpackage Compiler
  9* @author Uwe Tews 
 10*/
 11/**
 12* Smarty Internal Plugin Compile Foreach Class
 13*/
 14class Smarty_Internal_Compile_Foreach extends Smarty_Internal_CompileBase {
 15    /**
 16    * Compiles code for the {foreach} tag
 17    * 
 18    * @param array $args array with attributes from parser
 19    * @param object $compiler compiler object
 20    * @return string compiled code
 21    */
 22    public function compile($args, $compiler)
 23    {
 24        $this->compiler = $compiler;
 25        $this->required_attributes = array('from', 'item');
 26        $this->optional_attributes = array('name', 'key');
 27        $tpl = $compiler->template; 
 28        // check and get attributes
 29        $_attr = $this->_get_attributes($args);
 30
 31        $this->_open_tag('foreach', array('foreach',$this->compiler->nocache));
 32		// maybe nocache because of nocache variables
 33		$this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
 34
 35        $from = $_attr['from'];
 36        $item = $_attr['item'];
 37
 38        if (isset($_attr['key'])) {
 39            $key = $_attr['key'];
 40        } else {
 41            $key = null;
 42        } 
 43
 44        if (isset($_attr['name'])) {
 45            $name = $_attr['name'];
 46            $has_name = true;
 47            $SmartyVarName = '$smarty.foreach.' . trim($name,'\'"') . '.';
 48        } else {
 49            $name = null;
 50            $has_name = false;
 51        } 
 52        $ItemVarName = '$' . trim($item,'\'"') . '@';
 53        // evaluates which Smarty variables and properties have to be computed
 54        if ($has_name) {
 55            $usesSmartyFirst = strpos($tpl->template_source, $SmartyVarName . 'first') !== false;
 56            $usesSmartyLast = strpos($tpl->template_source, $SmartyVarName . 'last') !== false;
 57            $usesSmartyIndex = strpos($tpl->template_source, $SmartyVarName . 'index') !== false;
 58            $usesSmartyIteration = strpos($tpl->template_source, $SmartyVarName . 'iteration') !== false;
 59            $usesSmartyShow = strpos($tpl->template_source, $SmartyVarName . 'show') !== false;
 60            $usesSmartyTotal = $usesSmartyLast || strpos($tpl->template_source, $SmartyVarName . 'total') !== false;
 61        } else {
 62            $usesSmartyFirst = false;
 63            $usesSmartyLast = false;
 64            $usesSmartyTotal = false;
 65        }
 66
 67        $usesPropFirst = $usesSmartyFirst || strpos($tpl->template_source, $ItemVarName . 'first') !== false;
 68        $usesPropLast = $usesSmartyLast || strpos($tpl->template_source, $ItemVarName . 'last') !== false;
 69        $usesPropIndex = $usesPropFirst || strpos($tpl->template_source, $ItemVarName . 'index') !== false;
 70        $usesPropIteration = $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'iteration') !== false;
 71        $usesPropShow = strpos($tpl->template_source, $ItemVarName . 'show') !== false;
 72        $usesPropTotal = $usesSmartyTotal || $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'total') !== false; 
 73        // generate output code
 74        $output = "<?php ";
 75        $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
 76        if ($key != null) {
 77            $output .= " \$_smarty_tpl->tpl_vars[$key] = new Smarty_Variable;\n";
 78        } 
 79        $output .= " \$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array');}\n";
 80        if ($usesPropTotal) {
 81            $output .= " \$_smarty_tpl->tpl_vars[$item]->total=count(\$_from);\n";
 82        } 
 83        if ($usesPropIteration) {
 84            $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration=0;\n";
 85        } 
 86        if ($usesPropIndex) {
 87            $output .= " \$_smarty_tpl->tpl_vars[$item]->index=-1;\n";
 88        } 
 89        if ($has_name) {
 90            if ($usesSmartyTotal) {
 91                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['total'] = \$_smarty_tpl->tpl_vars[$item]->total;\n";
 92            } 
 93            if ($usesSmartyIteration) {
 94                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']=0;\n";
 95            } 
 96            if ($usesSmartyIndex) {
 97                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']=-1;\n";
 98            } 
 99        } 
100        $output .= "if (count(\$_from) > 0){\n";
101        $output .= "    foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
102        if ($key != null) {
103            $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
104        } 
105        if ($usesPropIteration) {
106            $output .= " \$_smarty_tpl->tpl_vars[$item]->iteration++;\n";
107        } 
108        if ($usesPropIndex) {
109            $output .= " \$_smarty_tpl->tpl_vars[$item]->index++;\n";
110        } 
111        if ($usesPropFirst) {
112            $output .= " \$_smarty_tpl->tpl_vars[$item]->first = \$_smarty_tpl->tpl_vars[$item]->index === 0;\n";
113        } 
114        if ($usesPropLast) {
115            $output .= " \$_smarty_tpl->tpl_vars[$item]->last = \$_smarty_tpl->tpl_vars[$item]->iteration === \$_smarty_tpl->tpl_vars[$item]->total;\n";
116        } 
117        if ($has_name) {
118            if ($usesSmartyFirst) {
119                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['first'] = \$_smarty_tpl->tpl_vars[$item]->first;\n";
120            } 
121            if ($usesSmartyIteration) {
122                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['iteration']++;\n";
123            } 
124            if ($usesSmartyIndex) {
125                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['index']++;\n";
126            } 
127            if ($usesSmartyLast) {
128                $output .= " \$_smarty_tpl->tpl_vars['smarty']->value['foreach'][$name]['last'] = \$_smarty_tpl->tpl_vars[$item]->last;\n";
129            } 
130        } 
131        $output .= "?>";
132
133        return $output;
134    } 
135} 
136
137/**
138* Smarty Internal Plugin Compile Foreachelse Class
139*/ 
140class Smarty_Internal_Compile_Foreachelse extends Smarty_Internal_CompileBase {
141    /**
142    * Compiles code for the {foreachelse} tag
143    * 
144    * @param array $args array with attributes from parser
145    * @param object $compiler compiler object
146    * @return string compiled code
147    */
148    public function compile($args, $compiler)
149    {
150        $this->compiler = $compiler; 
151        // check and get attributes
152        $_attr = $this->_get_attributes($args);
153
154        list($_open_tag, $nocache) = $this->_close_tag(array('foreach'));
155        $this->_open_tag('foreachelse',array('foreachelse', $nocache));
156
157        return "<?php }} else { ?>";
158    } 
159} 
160
161/**
162* Smarty Internal Plugin Compile Foreachclose Class
163*/ 
164class Smarty_Internal_Compile_Foreachclose extends Smarty_Internal_CompileBase {
165    /**
166    * Compiles code for the {/foreach} tag
167    * 
168    * @param array $args array with attributes from parser
169    * @param object $compiler compiler object
170    * @return string compiled code
171    */
172    public function compile($args, $compiler)
173    {
174        $this->compiler = $compiler; 
175        // check and get attributes
176        $_attr = $this->_get_attributes($args);
177
178		// must endblock be nocache?
179		if ($this->compiler->nocache) {
180               $this->compiler->tag_nocache = true;
181        }
182
183        list($_open_tag, $this->compiler->nocache) = $this->_close_tag(array('foreach', 'foreachelse'));
184
185        if ($_open_tag == 'foreachelse')
186            return "<?php } ?>";
187        else
188            return "<?php }} ?>";
189    } 
190}
191
192?>