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.0.0
  38.  * @date       2008-10-23
  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 $compilerarray $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 $compilerarray $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.         // gets foreach id
  78.         $cnt self::$cnt++;
  79.  
  80.         // builds pre processing output
  81.         $pre Dwoo_Compiler::PHP_OPEN "\n".'$_loop'.$cnt.'_data = '.$src.';';
  82.         // adds foreach properties
  83.         if ($usesAny{
  84.             $pre .= "\n".'$this->globals["loop"]['.$name.'] = array'."\n(";
  85.             if ($usesIndex$pre .="\n\t".'"index"        => 0,';
  86.             if ($usesIteration$pre .="\n\t".'"iteration"        => 1,';
  87.             if ($usesFirst$pre .="\n\t".'"first"        => null,';
  88.             if ($usesLast$pre .="\n\t".'"last"        => null,';
  89.             if ($usesShow$pre .="\n\t".'"show"        => $this->isArray($_loop'.$cnt.'_data, true, true),';
  90.             if ($usesTotal$pre .="\n\t".'"total"        => $this->isArray($_loop'.$cnt.'_data) ? count($_loop'.$cnt.'_data) : 0,';
  91.             $pre.="\n);\n".'$_loop'.$cnt.'_glob =& $this->globals["loop"]['.$name.'];';
  92.         }
  93.         // checks if the loop must be looped
  94.         $pre .= "\n".'if ($this->isArray($_loop'.$cnt.'_data'.(isset($params['hasElse']', true, true' '').') === true)'."\n{";
  95.         // iterates over keys
  96.         $pre .= "\n\t".'foreach ($_loop'.$cnt.'_data as $tmp_key => $this->scope["-loop-"])'."\n\t{";
  97.         // updates properties
  98.         if ($usesFirst{
  99.             $pre .= "\n\t\t".'$_loop'.$cnt.'_glob["first"] = (string) ($_loop'.$cnt.'_glob["index"] === 0);';
  100.         }
  101.         if ($usesLast{
  102.             $pre .= "\n\t\t".'$_loop'.$cnt.'_glob["last"] = (string) ($_loop'.$cnt.'_glob["iteration"] === $_loop'.$cnt.'_glob["total"]);';
  103.         }
  104.         $pre .= "\n\t\t".'$_loop'.$cnt.'_scope = $this->setScope(array("-loop-"));' "\n/* -- loop start output */\n".Dwoo_Compiler::PHP_CLOSE;
  105.  
  106.         // build post processing output and cache it
  107.         $post Dwoo_Compiler::PHP_OPEN "\n".'/* -- loop end output */'."\n\t\t".'$this->setScope($_loop'.$cnt.'_scope, true);';
  108.         // update properties
  109.         if ($usesIndex{
  110.             $post.="\n\t\t".'$_loop'.$cnt.'_glob["index"]+=1;';
  111.         }
  112.         if ($usesIteration{
  113.             $post.="\n\t\t".'$_loop'.$cnt.'_glob["iteration"]+=1;';
  114.         }
  115.         // end loop
  116.         $post .= "\n\t}\n}\n" Dwoo_Compiler::PHP_CLOSE;
  117.         if (isset($params['hasElse'])) {
  118.             $post .= $params['hasElse'];
  119.         }
  120.  
  121.         return $pre $content $post;
  122.     }
  123. }

Documentation generated on Wed, 24 Dec 2008 02:13:30 +0100 by phpDocumentor 1.4.0