Source for file foreach.php

Documentation is available at foreach.php

  1. <?php
  2.  
  3. /**
  4.  * Similar to the php foreach block, loops over an array
  5.  *
  6.  * Note that if you don't provide the item parameter, the key will act as item
  7.  * <pre>
  8.  *  * from : the array that you want to iterate over
  9.  *  * key : variable name for the key (or for the item if item is not defined)
  10.  *  * item : variable name for each item
  11.  *  * name : foreach name to access it's iterator variables through {$.foreach.name.var} see {@link http://wiki.dwoo.org/index.php/IteratorVariables} for details
  12.  * </pre>
  13.  * Example :
  14.  *
  15.  * <code>
  16.  * {foreach $array val}
  17.  *   {$val.something}
  18.  * {/foreach}
  19.  * </code>
  20.  *
  21.  * This software is provided 'as-is', without any express or implied warranty.
  22.  * In no event will the authors be held liable for any damages arising from the use of this software.
  23.  *
  24.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  25.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  26.  * @license    http://dwoo.org/LICENSE   Modified BSD License
  27.  * @link       http://dwoo.org/
  28.  * @version    1.1.0
  29.  * @date       2009-07-18
  30.  * @package    Dwoo
  31.  */
  32. {
  33.     public static $cnt=0;
  34.  
  35.     public function init($from$key=null$item=null$name='default'$implode=null)
  36.     {
  37.     }
  38.  
  39.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  40.     {
  41.         // get block params and save the current template pointer to use it in the postProcessing method
  42.         $currentBlock =$compiler->getCurrentBlock();
  43.         $currentBlock['params']['tplPointer'$compiler->getPointer();
  44.  
  45.         return '';
  46.     }
  47.  
  48.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  49.     {
  50.         $params $compiler->getCompiledParams($params);
  51.         $tpl $compiler->getTemplateSource($params['tplPointer']);
  52.  
  53.         // assigns params
  54.         $src $params['from'];
  55.  
  56.         if ($params['item'!== 'null'{
  57.             if ($params['key'!== 'null'{
  58.                 $key $params['key'];
  59.             }
  60.             $val $params['item'];
  61.         elseif ($params['key'!== 'null'{
  62.             $val $params['key'];
  63.         else {
  64.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>item</em> parameter missing');
  65.         }
  66.         $name $params['name'];
  67.  
  68.         if (substr($val01!== '"' && substr($val01!== '\''{
  69.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>item</em> parameter must be of type string');
  70.         }
  71.         if (isset($key&& substr($val01!== '"' && substr($val01!== '\''{
  72.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>key</em> parameter must be of type string');
  73.         }
  74.  
  75.         // evaluates which global variables have to be computed
  76.         $varName '$dwoo.foreach.'.trim($name'"\'').'.';
  77.         $shortVarName '$.foreach.'.trim($name'"\'').'.';
  78.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  79.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  80.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  81.         $usesIndex $usesFirst || strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  82.         $usesIteration $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  83.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  84.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  85.  
  86.         if (strpos($name'$this->scope['!== false{
  87.             $usesAny $usesFirst $usesLast $usesIndex $usesIteration $usesShow $usesTotal true;
  88.         }
  89.  
  90.         // override globals vars if implode is used
  91.         if ($params['implode'!== 'null'{
  92.             $implode $params['implode'];
  93.             $usesAny true;
  94.             $usesLast true;
  95.             $usesIteration true;
  96.             $usesTotal true;
  97.         }
  98.  
  99.         // gets foreach id
  100.         $cnt self::$cnt++;
  101.  
  102.         // build pre content output
  103.         $pre Dwoo_Compiler::PHP_OPEN "\n".'$_fh'.$cnt.'_data = '.$src.';';
  104.         // adds foreach properties
  105.         if ($usesAny{
  106.             $pre .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n(";
  107.             if ($usesIndex$pre .="\n\t".'"index"        => 0,';
  108.             if ($usesIteration$pre .="\n\t".'"iteration"        => 1,';
  109.             if ($usesFirst$pre .="\n\t".'"first"        => null,';
  110.             if ($usesLast$pre .="\n\t".'"last"        => null,';
  111.             if ($usesShow$pre .="\n\t".'"show"        => $this->isArray($_fh'.$cnt.'_data, true),';
  112.             if ($usesTotal$pre .="\n\t".'"total"        => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
  113.             $pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
  114.         }
  115.         // checks if foreach must be looped
  116.         $pre .= "\n".'if ($this->isArray($_fh'.$cnt.'_data'.(isset($params['hasElse']', true' '').') === true)'."\n{";
  117.         // iterates over keys
  118.         $pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
  119.         // updates properties
  120.         if ($usesFirst{
  121.             $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);';
  122.         }
  123.         if ($usesLast{
  124.             $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);';
  125.         }
  126.         $pre .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE;
  127.  
  128.         // build post content output
  129.         $post Dwoo_Compiler::PHP_OPEN "\n";
  130.  
  131.         if (isset($implode)) {
  132.             $post .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'.
  133.                 "\n\t".'echo '.$implode.";\n}\n";
  134.         }
  135.         $post .= '/* -- foreach end output */';
  136.         // update properties
  137.         if ($usesIndex{
  138.             $post.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;';
  139.         }
  140.         if ($usesIteration{
  141.             $post.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;';
  142.         }
  143.         // end loop
  144.         $post .= "\n\t}\n}".Dwoo_Compiler::PHP_CLOSE;
  145.         if (isset($params['hasElse'])) {
  146.             $post .= $params['hasElse'];
  147.         }
  148.  
  149.         return $pre $content $post;
  150.     }
  151. }

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