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 <[email protected]>
  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.         $params $compiler->getCompiledParams($params);
  47.         $tpl $compiler->getTemplateSource(true);
  48.  
  49.         // assigns params
  50.         $src $params['from'];
  51.  
  52.         if ($params['item'!== 'null'{
  53.             if ($params['key'!== 'null'{
  54.                 $key $params['key'];
  55.             }
  56.             $val $params['item'];
  57.         elseif ($params['key'!== 'null'{
  58.             $val $params['key'];
  59.         else {
  60.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>item</em> parameter missing');
  61.         }
  62.         $name $params['name'];
  63.  
  64.         if (substr($val01!== '"' && substr($val01!== '\''{
  65.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>item</em> parameter must be of type string');
  66.         }
  67.         if (isset($key&& substr($val01!== '"' && substr($val01!== '\''{
  68.             throw new Dwoo_Compilation_Exception($compiler'Foreach <em>key</em> parameter must be of type string');
  69.         }
  70.  
  71.         // evaluates which global variables have to be computed
  72.         $varName '$dwoo.foreach.'.trim($name'"\'').'.';
  73.         $shortVarName '$.foreach.'.trim($name'"\'').'.';
  74.         $usesAny strpos($tpl$varName!== false || strpos($tpl$shortVarName!== false;
  75.         $usesFirst strpos($tpl$varName.'first'!== false || strpos($tpl$shortVarName.'first'!== false;
  76.         $usesLast strpos($tpl$varName.'last'!== false || strpos($tpl$shortVarName.'last'!== false;
  77.         $usesIndex $usesFirst || strpos($tpl$varName.'index'!== false || strpos($tpl$shortVarName.'index'!== false;
  78.         $usesIteration $usesLast || strpos($tpl$varName.'iteration'!== false || strpos($tpl$shortVarName.'iteration'!== false;
  79.         $usesShow strpos($tpl$varName.'show'!== false || strpos($tpl$shortVarName.'show'!== false;
  80.         $usesTotal $usesLast || strpos($tpl$varName.'total'!== false || strpos($tpl$shortVarName.'total'!== false;
  81.  
  82.         // override globals vars if implode is used
  83.         if ($params['implode'!== 'null'{
  84.             $implode $params['implode'];
  85.             $usesAny true;
  86.             $usesLast true;
  87.         }
  88.  
  89.         // gets foreach id
  90.         $cnt self::$cnt++;
  91.  
  92.         // builds pre processing output
  93.         $out Dwoo_Compiler::PHP_OPEN "\n".'$_fh'.$cnt.'_data = '.$src.';';
  94.         // adds foreach properties
  95.         if ($usesAny{
  96.             $out .= "\n".'$this->globals["foreach"]['.$name.'] = array'."\n(";
  97.             if ($usesIndex$out .="\n\t".'"index"        => 0,';
  98.             if ($usesIteration$out .="\n\t".'"iteration"        => 1,';
  99.             if ($usesFirst$out .="\n\t".'"first"        => null,';
  100.             if ($usesLast$out .="\n\t".'"last"        => null,';
  101.             if ($usesShow$out .="\n\t".'"show"        => $this->isArray($_fh'.$cnt.'_data, true, true),';
  102.             if ($usesTotal$out .="\n\t".'"total"        => $this->isArray($_fh'.$cnt.'_data) ? count($_fh'.$cnt.'_data) : 0,';
  103.             $out.="\n);\n".'$_fh'.$cnt.'_glob =& $this->globals["foreach"]['.$name.'];';
  104.         }
  105.         // checks if foreach must be looped
  106.         $out .= "\n".'if ($this->isArray($_fh'.$cnt.'_data, true, true) === true)'."\n{";
  107.         // iterates over keys
  108.         $out .= "\n\t".'foreach ($_fh'.$cnt.'_data as '.(isset($key)?'$this->scope['.$key.']=>':'').'$this->scope['.$val.'])'."\n\t{";
  109.         // updates properties
  110.         if ($usesFirst{
  111.             $out .= "\n\t\t".'$_fh'.$cnt.'_glob["first"] = (string) ($_fh'.$cnt.'_glob["index"] === 0);';
  112.         }
  113.         if ($usesLast{
  114.             $out .= "\n\t\t".'$_fh'.$cnt.'_glob["last"] = (string) ($_fh'.$cnt.'_glob["iteration"] === $_fh'.$cnt.'_glob["total"]);';
  115.         }
  116.         $out .= "\n/* -- foreach start output */\n".Dwoo_Compiler::PHP_CLOSE;
  117.  
  118.         // build post processing output and cache it
  119.         $postOut Dwoo_Compiler::PHP_OPEN "\n";
  120.  
  121.         if (isset($implode)) {
  122.             $postOut .= '/* -- implode */'."\n".'if (!$_fh'.$cnt.'_glob["last"]) {'.
  123.                 "\n\t".'echo '.$implode.";\n}\n";
  124.         }
  125.         $postOut .= '/* -- foreach end output */';
  126.         // update properties
  127.         if ($usesIndex{
  128.             $postOut.="\n\t\t".'$_fh'.$cnt.'_glob["index"]+=1;';
  129.         }
  130.         if ($usesIteration{
  131.             $postOut.="\n\t\t".'$_fh'.$cnt.'_glob["iteration"]+=1;';
  132.         }
  133.         // end loop
  134.         $postOut .= "\n\t}\n}\n";
  135.  
  136.         // get block params and save the post-processing output already
  137.         $currentBlock =$compiler->getCurrentBlock();
  138.         $currentBlock['params']['postOutput'$postOut Dwoo_Compiler::PHP_CLOSE;
  139.  
  140.         return $out;
  141.     }
  142.  
  143.     public static function postProcessing(Dwoo_Compiler $compilerarray $params$prepend$append$content)
  144.     {
  145.         return $content $params['postOutput'];
  146.     }
  147. }

Documentation generated on Sun, 03 Aug 2008 15:12:36 +0200 by phpDocumentor 1.4.0