cvs: smarty /libs Smarty_Compiler.class.php

"Messju Mohr" <[email protected]>
Newsgroups gmane.comp.php.cvs.smarty
Message-ID <cvsmessju1105032277@cvsserver>
messju		Thu Jan  6 12:24:37 2005 EDT

  Modified files:              
    /smarty/libs	Smarty_Compiler.class.php 
  Log:
  reduced the code that is generated on a {foreach}-block that has a
  name.
  
  instead of pre-computing all foreach-properties (like first, last,
  show) on each iteration, they are computed on demand as soon as
  {$smarty.foreach.*}-variables are used.
  
  
  
http://cvs.php.net/diff.php/smarty/libs/Smarty_Compiler.class.php?r1=1.355&r2=1.356&ty=u
Index: smarty/libs/Smarty_Compiler.class.php
diff -u smarty/libs/Smarty_Compiler.class.php:1.355 smarty/libs/Smarty_Compiler.class.php:1.356
--- smarty/libs/Smarty_Compiler.class.php:1.355	Thu Jan  6 12:17:05 2005
+++ smarty/libs/Smarty_Compiler.class.php	Thu Jan  6 12:24:36 2005
@@ -26,7 +26,7 @@
  * @package Smarty
  */
 
-/* $Id: Smarty_Compiler.class.php,v 1.355 2005/01/06 17:17:05 messju Exp $ */
+/* $Id: Smarty_Compiler.class.php,v 1.356 2005/01/06 17:24:36 messju Exp $ */
 
 /**
  * Template compiling class
@@ -498,14 +498,14 @@
 
             case 'foreachelse':
                 $this->_push_tag('foreachelse');
-                return "<?php endforeach; unset(\$_from); else: ?>";
+                return "<?php endforeach; else: ?>";
 
             case '/foreach':
                 $_open_tag = $this->_pop_tag('foreach');
                 if ($_open_tag == 'foreachelse')
-                    return "<?php endif; ?>";
+                    return "<?php endif; unset(\$_from); ?>";
                 else
-                    return "<?php endforeach; unset(\$_from); endif; ?>";
+                    return "<?php endforeach; endif; unset(\$_from); ?>";
                 break;
 
             case 'strip':
@@ -1159,15 +1159,10 @@
         $output = '<?php ';
         if (isset($name)) {
             $foreach_props = "\$this->_foreach[$name]";
-            $output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n";
-            $output .= "{$foreach_props}['total'] = count(\$_from = (array)$from);\n";
-            $output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n";
-            $output .= "if ({$foreach_props}['show']):\n";
-            $output .= "{$foreach_props}['iteration'] = 0;\n";
+            $output .= "{$foreach_props} = array('total' => count(\$_from = (array)$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";
-            $output .= "        {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n";
-            $output .= "        {$foreach_props}['last']  = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n";
         } else {
             $output .= "if (count(\$_from = (array)$from)):\n";
             $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
@@ -1965,13 +1960,41 @@
                 break;
 
             case 'foreach':
+                array_shift($indexes);
+                $_var = $this->_parse_var_props(substr($indexes[0], 1));
+                $_propname = substr($indexes[1], 1);
+                $_max_index = 1;
+                switch ($_propname) {
+                    case 'index':
+                        array_shift($indexes);
+                        $compiled_ref = "(\$this->_foreach[$_var]['iteration']-1)";
+                        break;
+                        
+                    case 'first':
+                        array_shift($indexes);
+                        $compiled_ref = "(\$this->_foreach[$_var]['iteration'] <= 1)";
+                        break;
+
+                    case 'last':
+                        array_shift($indexes);
+                        $compiled_ref = "(\$this->_foreach[$_var]['iteration'] == \$this->_foreach[$_var]['total'])";
+                        break;
+                        
+                    case 'show':
+                        array_shift($indexes);
+                        $compiled_ref = "(\$this->_foreach[$_var]['total'] > 0)";
+                        break;
+                        
+                    default:
+                        unset($_max_index);
+                        $compiled_ref = "\$this->_foreach[$_var]";
+                }
+                break;
+
             case 'section':
                 array_shift($indexes);
                 $_var = $this->_parse_var_props(substr($indexes[0], 1));
-                if ($_ref == 'foreach')
-                    $compiled_ref = "\$this->_foreach[$_var]";
-                else
-                    $compiled_ref = "\$this->_sections[$_var]";
+                $compiled_ref = "\$this->_sections[$_var]";
                 break;
 
             case 'get':

-- 
Smarty CVS Mailing List (http://cvs.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.