Source for file loop.php
Documentation is available at loop.php
* Loops over an array and moves the scope into each value, allowing for shorter loop constructs
* Note that to access the array key within a loop block, you have to use the {$_key} variable,
* you can not specify it yourself.
* * from : the array that you want to iterate over
* * name : loop name to access it's iterator variables through {$.loop.name.var} see {@link http://wiki.dwoo.org/index.php/IteratorVariables} for details
* instead of a foreach block such as :
* {foreach $variable value}
* {$value.foo} {$value.bar}
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
* This file is released under the LGPL
* "GNU Lesser General Public License"
* More information can be found here:
* {@link http://www.gnu.org/copyleft/lesser.html}
* @copyright Copyright (c) 2008, Jordi Boggiano
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
public function init($from, $name=
'default')
public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
$params =
$compiler->getCompiledParams($params);
$tpl =
$compiler->getTemplateSource(true);
// evaluates which global variables have to be computed
$varName =
'$dwoo.loop.'.
trim($name, '"\'').
'.';
$shortVarName =
'$.loop.'.
trim($name, '"\'').
'.';
$usesAny =
strpos($tpl, $varName) !==
false ||
strpos($tpl, $shortVarName) !==
false;
$usesFirst =
strpos($tpl, $varName.
'first') !==
false ||
strpos($tpl, $shortVarName.
'first') !==
false;
$usesLast =
strpos($tpl, $varName.
'last') !==
false ||
strpos($tpl, $shortVarName.
'last') !==
false;
$usesIndex =
$usesFirst ||
strpos($tpl, $varName.
'index') !==
false ||
strpos($tpl, $shortVarName.
'index') !==
false;
$usesIteration =
$usesLast ||
strpos($tpl, $varName.
'iteration') !==
false ||
strpos($tpl, $shortVarName.
'iteration') !==
false;
$usesShow =
strpos($tpl, $varName.
'show') !==
false ||
strpos($tpl, $shortVarName.
'show') !==
false;
$usesTotal =
$usesLast ||
strpos($tpl, $varName.
'total') !==
false ||
strpos($tpl, $shortVarName.
'total') !==
false;
// builds pre processing output
$out =
Dwoo_Compiler::PHP_OPEN .
"\n".
'$_loop'.
$cnt.
'_data = '.
$src.
';';
// adds foreach properties
$out .=
"\n".
'$this->globals["loop"]['.
$name.
'] = array'.
"\n(";
if ($usesIndex) $out .=
"\n\t".
'"index" => 0,';
if ($usesIteration) $out .=
"\n\t".
'"iteration" => 1,';
if ($usesFirst) $out .=
"\n\t".
'"first" => null,';
if ($usesLast) $out .=
"\n\t".
'"last" => null,';
if ($usesShow) $out .=
"\n\t".
'"show" => $this->isArray($_loop'.
$cnt.
'_data, true, true),';
if ($usesTotal) $out .=
"\n\t".
'"total" => $this->isArray($_loop'.
$cnt.
'_data) ? count($_loop'.
$cnt.
'_data) : 0,';
$out.=
"\n);\n".
'$_loop'.
$cnt.
'_glob =& $this->globals["loop"]['.
$name.
'];';
// checks if foreach must be looped
$out .=
"\n".
'if ($this->isArray($_loop'.
$cnt.
'_data, true, true) === true)'.
"\n{";
$out .=
"\n\t".
'foreach ($_loop'.
$cnt.
'_data as $tmp_key => $this->scope["-loop-"])'.
"\n\t{";
$out .=
"\n\t\t".
'$_loop'.
$cnt.
'_glob["first"] = (string) ($_loop'.
$cnt.
'_glob["index"] === 0);';
$out .=
"\n\t\t".
'$_loop'.
$cnt.
'_glob["last"] = (string) ($_loop'.
$cnt.
'_glob["iteration"] === $_loop'.
$cnt.
'_glob["total"]);';
$out .=
"\n\t\t".
'$_loop'.
$cnt.
'_scope = $this->setScope(array("-loop-"));' .
"\n/* -- loop start output */\n".
Dwoo_Compiler::PHP_CLOSE;
// build post processing output and cache it
$postOut =
Dwoo_Compiler::PHP_OPEN .
"\n".
'/* -- loop end output */'.
"\n\t\t".
'$this->setScope($_loop'.
$cnt.
'_scope, true);';
$postOut.=
"\n\t\t".
'$_loop'.
$cnt.
'_glob["index"]+=1;';
$postOut.=
"\n\t\t".
'$_loop'.
$cnt.
'_glob["iteration"]+=1;';
$postOut .=
"\n\t}\n}\n";
// get block params and save the post-processing output already
$currentBlock =
& $compiler->getCurrentBlock();
$currentBlock['params']['postOutput'] =
$postOut .
Dwoo_Compiler::PHP_CLOSE;
public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
return $content .
$params['postOutput'];
Documentation generated on Sun, 03 Aug 2008 15:12:41 +0200 by phpDocumentor 1.4.0