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.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  34.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  35.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  36.  * @link       http://dwoo.org/
  37.  * @version    1.1.0
  38.  * @date       2009-07-18
  39.  * @package    Dwoo
  40.  */
  41. {
  42.     public static $cnt=0;
  43.  
  44.     public function init($from, $name='default')
  45.     {
  46.     }
  47.  
  48.     public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
  49.     {
  50.         // get block params and save the current template pointer to use it in the postProcessing method
  51.         $currentBlock =& $compiler->getCurrentBlock();
  52.         $currentBlock['params']['tplPointer'] = $compiler->getPointer();
  53.  
  54.         return '';
  55.     }
  56.  
  57.     public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
  58.     {
  59.         $params = $compiler->getCompiledParams($params);
  60.         $tpl = $compiler->getTemplateSource($params['tplPointer']);
  61.  
  62.         // assigns params
  63.         $src = $params['from'];
  64.         $name = $params['name'];
  65.  
  66.         // evaluates which global variables have to be computed
  67.         $varName = '$dwoo.loop.'.trim($name, '"\'').'.';
  68.         $shortVarName = '$.loop.'.trim($name, '"\'').'.';
  69.         $usesAny = strpos($tpl, $varName) !== false || strpos($tpl, $shortVarName) !== false;
  70.         $usesFirst = strpos($tpl, $varName.'first') !== false || strpos($tpl, $shortVarName.'first') !== false;
  71.         $usesLast = strpos($tpl, $varName.'last') !== false || strpos($tpl, $shortVarName.'last') !== false;
  72.         $usesIndex = $usesFirst || strpos($tpl, $varName.'index') !== false || strpos($tpl, $shortVarName.'index') !== false;
  73.         $usesIteration = $usesLast || strpos($tpl, $varName.'iteration') !== false || strpos($tpl, $shortVarName.'iteration') !== false;
  74.         $usesShow = strpos($tpl, $varName.'show') !== false || strpos($tpl, $shortVarName.'show') !== false;
  75.         $usesTotal = $usesLast || strpos($tpl, $varName.'total') !== false || strpos($tpl, $shortVarName.'total') !== false;
  76.  
  77.         if (strpos($name, '$this->scope[') !== false) {
  78.             $usesAny = $usesFirst = $usesLast = $usesIndex = $usesIteration = $usesShow = $usesTotal = true;
  79.         }
  80.  
  81.         // gets foreach id
  82.         $cnt = self::$cnt++;
  83.  
  84.         // builds pre processing output
  85.         $pre = Dwoo_Compiler::PHP_OPEN . "\n".'$_loop'.$cnt.'_data = '.$src.';';
  86.         // adds foreach properties
  87.         if ($usesAny) {
  88.             $pre .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n(";
  89.             if ($usesIndex) $pre .="\n\t".'"index"        => 0,';
  90.             if ($usesIteration) $pre .="\n\t".'"iteration"        => 1,';
  91.             if ($usesFirst) $pre .="\n\t".'"first"        => null,';
  92.             if ($usesLast) $pre .="\n\t".'"last"        => null,';
  93.             if ($usesShow) $pre .="\n\t".'"show"        => $this->isArray($_loop'.$cnt.'_data, true),';
  94.             if ($usesTotal) $pre .="\n\t".'"total"        => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
  95.             $pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
  96.         }
  97.         // checks if the loop must be looped
  98.         $pre .= "\n".'if ($this->isArray($_loop'.$cnt.'_data'.(isset($params['hasElse']) ? ', true' : '').') === true)'."\n{";
  99.         // iterates over keys
  100.         $pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
  101.         // updates properties
  102.         if ($usesFirst) {
  103.             $pre .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
  104.         }
  105.         if ($usesLast) {
  106.             $pre .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
  107.         }
  108.         $pre .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope(array("-loop-"));' . "\n/* -- loop start output */\n".Dwoo_Compiler::PHP_CLOSE;
  109.  
  110.         // build post processing output and cache it
  111.         $post = Dwoo_Compiler::PHP_OPEN . "\n".'/* -- loop end output */'."\n\t\t".'$this->setScope($_loop'.$cnt.'_scope, true);';
  112.         // update properties
  113.         if ($usesIndex) {
  114.             $post.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;';
  115.         }
  116.         if ($usesIteration) {
  117.             $post.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;';
  118.         }
  119.         // end loop
  120.         $post .= "\n\t}\n}\n" . Dwoo_Compiler::PHP_CLOSE;
  121.         if (isset($params['hasElse'])) {
  122.             $post .= $params['hasElse'];
  123.         }
  124.  
  125.         return $pre . $content . $post;
  126.     }
  127. }

Documentation generated on Sun, 07 Feb 2010 17:53:52 +0000 by phpDocumentor 1.4.0