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.0.1
  29.  * @date       2008-12-24
  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.         // override globals vars if implode is used
  87.         if ($params['implode'!== 'null'{
  88.             $implode $params['implode'];
  89.             $usesAny true;
  90.             $usesLast true;
  91.             $usesIteration true;
  92.             $usesTotal true;
  93.         }
  94.  
  95.         // gets foreach id
  96.         $cnt self::$cnt++;
  97.  
  98.         // build pre content output
  99.         $pre Dwoo_Compiler::PHP_OPEN "\n".'$_fh'.$cnt.'_data = '.$src.';';
  100.         // adds foreach properties
  101.         if ($usesAny{
  102.             $pre .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n(";
  103.             if ($usesIndex$pre .="\n\t".'"index"        => 0,';
  104.             if ($usesIteration$pre .="\n\t".'"iteration"        => 1,';
  105.             if ($usesFirst$pre .="\n\t".'"first"        => null,';
  106.             if ($usesLast$pre .="\n\t".'"last"        => null,';
  107.             if ($usesShow$pre .="\n\t".'"show"        => $this->isArray($_fh'.$cnt.'_data, true, true),';
  108.             if ($usesTotal$pre .="\n\t".'"total"        => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
  109.             $pre.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
  110.         }
  111.         // checks if foreach must be looped
  112.         $pre .= "\n".'if ($this->isArray($_fh'.$cnt.'_data'.(isset($params['hasElse']', true, true' '').') === true)'."\n{";
  113.         // iterates over keys
  114.         $pre .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
  115.         // updates properties
  116.         if ($usesFirst{
  117.             $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);';
  118.         }
  119.         if ($usesLast{
  120.             $pre .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);';
  121.         }
  122.         $pre .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE;
  123.  
  124.         // build post content output
  125.         $post Dwoo_Compiler::PHP_OPEN "\n";
  126.  
  127.         if (isset($implode)) {
  128.             $post .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'.
  129.                 "\n\t".'echo '.$implode.";\n}\n";
  130.         }
  131.         $post .= '/* -- foreach end output */';
  132.         // update properties
  133.         if ($usesIndex{
  134.             $post.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;';
  135.         }
  136.         if ($usesIteration{
  137.             $post.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;';
  138.         }
  139.         // end loop
  140.         $post .= "\n\t}\n}".Dwoo_Compiler::PHP_CLOSE;
  141.         if (isset($params['hasElse'])) {
  142.             $post .= $params['hasElse'];
  143.         }
  144.  
  145.         return $pre $content $post;
  146.     }
  147. }

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