Source for file loop.php

Documentation is available at loop.php

  1. <?php
  2.  
  3. /**
  4.  * Loops over an array and moves the scope into each value, allowing for shorter loop constructs
  5.  *
  6.  * Note that to access the array key within a loop block, you have to use the {$_key} variable,
  7.  * you can not specify it yourself.
  8.  * <pre>
  9.  *  * from : the array that you want to iterate over
  10.  *  * name : loop name to access it's iterator variables through {$.loop.name.var} see {@link http://wiki.dwoo.org/index.php/IteratorVariables} for details
  11.  * </pre>
  12.  * Example :
  13.  *
  14.  * instead of a foreach block such as :
  15.  *
  16.  * <code>
  17.  * {foreach $variable value}
  18.  *   {$value.foo} {$value.bar}
  19.  * {/foreach}
  20.  * </code>
  21.  *
  22.  * you can do :
  23.  *
  24.  * <code>
  25.  * {loop $variable}
  26.  *   {$foo} {$bar}
  27.  * {/loop}
  28.  * </code>
  29.  *
  30.  * This software is provided 'as-is', without any express or implied warranty.
  31.  * In no event will the authors be held liable for any damages arising from the use of this software.
  32.  *
  33.  * This file is released under the LGPL
  34.  * "GNU Lesser General Public License"
  35.  * More information can be found here:
  36.  * {@link http://www.gnu.org/copyleft/lesser.html}
  37.  *
  38.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  39.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  40.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  41.  * @link       http://dwoo.org/
  42.  * @version    0.9.1
  43.  * @date       2008-05-30
  44.  * @package    Dwoo
  45.  */
  46. {
  47.     public static $cnt=0;
  48.  
  49.     public function init($from$name='default')
  50.     {
  51.     }
  52.  
  53.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  54.     {
  55.         // get block params and save the current template pointer to use it in the postProcessing method
  56.         $currentBlock =$compiler->getCurrentBlock();
  57.         $currentBlock['params']['tplPointer'$compiler->getPointer();
  58.  
  59.         return '';
  60.     }
  61.  
  62.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  63.     {
  64.         $params $compiler->getCompiledParams($params);
  65.         $tpl $compiler->getTemplateSource($params['tplPointer']);
  66.  
  67.         // assigns params
  68.         $src $params['from'];
  69.         $name $params['name'];
  70.  
  71.         // evaluates which global variables have to be computed
  72.         $varName '$dwoo.loop.'.trim($name'"\'').'.';
  73.         $shortVarName '$.loop.'.trim($name'"\'').'.';
  74.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  75.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  76.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  77.         $usesIndex $usesFirst || strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  78.         $usesIteration $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  79.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  80.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  81.  
  82.         // gets foreach id
  83.         $cnt self::$cnt++;
  84.  
  85.         // builds pre processing output
  86.         $pre Dwoo_Compiler::PHP_OPEN "\n".'$_loop'.$cnt.'_data = '.$src.';';
  87.         // adds foreach properties
  88.         if ($usesAny{
  89.             $pre .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n(";
  90.             if ($usesIndex$pre .="\n\t".'"index"        => 0,';
  91.             if ($usesIteration$pre .="\n\t".'"iteration"        => 1,';
  92.             if ($usesFirst$pre .="\n\t".'"first"        => null,';
  93.             if ($usesLast$pre .="\n\t".'"last"        => null,';
  94.             if ($usesShow$pre .="\n\t".'"show"        => $this->isArray($_loop'.$cnt.'_data, true, true),';
  95.             if ($usesTotal$pre .="\n\t".'"total"        => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
  96.             $pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
  97.         }
  98.         // checks if the loop must be looped
  99.         $pre .= "\n".'if ($this->isArray($_loop'.$cnt.'_data'.(isset($params['hasElse']', true, true' '').') === true)'."\n{";
  100.         // iterates over keys
  101.         $pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
  102.         // updates properties
  103.         if ($usesFirst{
  104.             $pre .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
  105.         }
  106.         if ($usesLast{
  107.             $pre .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
  108.         }
  109.         $pre .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope(array("-loop-"));' "\n/* -- loop start output */\n".Dwoo_Compiler::PHP_CLOSE;
  110.  
  111.         // build post processing output and cache it
  112.         $post Dwoo_Compiler::PHP_OPEN "\n".'/* -- loop end output */'."\n\t\t".'$this->setScope($_loop'.$cnt.'_scope, true);';
  113.         // update properties
  114.         if ($usesIndex{
  115.             $post.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;';
  116.         }
  117.         if ($usesIteration{
  118.             $post.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;';
  119.         }
  120.         // end loop
  121.         $post .= "\n\t}\n}\n" Dwoo_Compiler::PHP_CLOSE;
  122.         if (isset($params['hasElse'])) {
  123.             $post .= $params['hasElse'];
  124.         }
  125.  
  126.         return $pre $content $post;
  127.     }
  128. }

Documentation generated on Sun, 07 Sep 2008 23:57:54 +0200 by phpDocumentor 1.4.0