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.  * This file is released under the LGPL
  25.  * "GNU Lesser General Public License"
  26.  * More information can be found here:
  27.  * {@link http://www.gnu.org/copyleft/lesser.html}
  28.  *
  29.  * @author     Jordi Boggiano <j.boggiano@seld.be>
  30.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  31.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  32.  * @link       http://dwoo.org/
  33.  * @version    0.9.1
  34.  * @date       2008-05-30
  35.  * @package    Dwoo
  36.  */
  37. {
  38.     public static $cnt=0;
  39.  
  40.     public function init($from$key=null$item=null$name='default'$implode=null)
  41.     {
  42.     }
  43.  
  44.     public static function preProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$type)
  45.     {
  46.         // get block params and save the current template pointer to use it in the postProcessing method
  47.         $currentBlock =$compiler->getCurrentBlock();
  48.         $currentBlock['params']['tplPointer'$compiler->getPointer();
  49.  
  50.         return '';
  51.     }
  52.  
  53.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  54.     {
  55.         $params $compiler->getCompiledParams($params);
  56.         $tpl $compiler->getTemplateSource($params['tplPointer']);
  57.  
  58.         // assigns params
  59.         $src $params['from'];
  60.  
  61.         if ($params['item'!== 'null'{
  62.             if ($params['key'!== 'null'{
  63.                 $key $params['key'];
  64.             }
  65.             $val $params['item'];
  66.         elseif ($params['key'!== 'null'{
  67.             $val $params['key'];
  68.         else {
  69.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>item</em> parameter missing');
  70.         }
  71.         $name $params['name'];
  72.  
  73.         if (substr($val01!== '"' && substr($val01!== '\''{
  74.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>item</em> parameter must be of type string');
  75.         }
  76.         if (isset($key&& substr($val01!== '"' && substr($val01!== '\''{
  77.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>key</em> parameter must be of type string');
  78.         }
  79.  
  80.         // evaluates which global variables have to be computed
  81.         $varName '$dwoo.foreach.'.trim($name'"\'').'.';
  82.         $shortVarName '$.foreach.'.trim($name'"\'').'.';
  83.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  84.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  85.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  86.         $usesIndex $usesFirst || strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  87.         $usesIteration $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  88.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  89.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  90.  
  91.         // override globals vars if implode is used
  92.         if ($params['implode'!== 'null'{
  93.             $implode $params['implode'];
  94.             $usesAny true;
  95.             $usesLast true;
  96.         }
  97.  
  98.         // gets foreach id
  99.         $cnt self::$cnt++;
  100.  
  101.         // build pre content output
  102.         $pre Dwoo_Compiler::PHP_OPEN "\n".'$_fh'.$cnt.'_data = '.$src.';';
  103.         // adds foreach properties
  104.         if ($usesAny{
  105.             $pre .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n(";
  106.             if ($usesIndex$pre .="\n\t".'"index"        => 0,';
  107.             if ($usesIteration$pre .="\n\t".'"iteration"        => 1,';
  108.             if ($usesFirst$pre .="\n\t".'"first"        => null,';
  109.             if ($usesLast$pre .="\n\t".'"last"        => null,';
  110.             if ($usesShow$pre .="\n\t".'"show"        => $this->isArray($_fh'.$cnt.'_data, true, true),';
  111.             if ($usesTotal$pre .="\n\t".'"total"        => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
  112.             $pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
  113.         }
  114.         // checks if foreach must be looped
  115.         $pre .= "\n".'if ($this->isArray($_fh'.$cnt.'_data'.(isset($params['hasElse']', true, true' '').') === true)'."\n{";
  116.         // iterates over keys
  117.         $pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
  118.         // updates properties
  119.         if ($usesFirst{
  120.             $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);';
  121.         }
  122.         if ($usesLast{
  123.             $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);';
  124.         }
  125.         $pre .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE;
  126.  
  127.         // build post content output
  128.         $post Dwoo_Compiler::PHP_OPEN "\n";
  129.  
  130.         if (isset($implode)) {
  131.             $post .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'.
  132.                 "\n\t".'echo '.$implode.";\n}\n";
  133.         }
  134.         $post .= '/* -- foreach end output */';
  135.         // update properties
  136.         if ($usesIndex{
  137.             $post.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;';
  138.         }
  139.         if ($usesIteration{
  140.             $post.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;';
  141.         }
  142.         // end loop
  143.         $post .= "\n\t}\n}".Dwoo_Compiler::PHP_CLOSE;
  144.         if (isset($params['hasElse'])) {
  145.             $post .= $params['hasElse'];
  146.         }
  147.  
  148.         return $pre $content $post;
  149.     }
  150. }

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