Commit f0196747 by Seldaek

For can now iterate backwards if you input numbers, it won't work with variables though

git-svn-id: svn://dwoo.org/dwoo/trunk@262 0598d79b-80c4-4d41-97ba-ac86fbbd088b
parent ff735e11
......@@ -24,6 +24,8 @@
* Objects now act like arrays when you access non-existing properties on
them (i.e. it outputs a notice only if it's output straight, and none
when passed to a function)
* For can now iterate backwards if you input numbers, it won't work with
variables though
* Fixed a bug with parsing AND/OR keywords in conditionals when they were
followed by round brackets
* Fixed assignments not handling multi-line values correctly
......
......@@ -85,16 +85,36 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
$_for'.$cnt.'_to = is_numeric($_for'.$cnt.'_to) ? $_for'.$cnt.'_to - $_for'.$cnt.'_step : count($_for'.$cnt.'_from) - 1;
$_for'.$cnt.'_from = 0;
}';
// reverse from and to if needed
$out .= "\n\t".'if ($_for'.$cnt.'_from > $_for'.$cnt.'_to) {
$tmp = $_for'.$cnt.'_from;
$_for'.$cnt.'_from = $_for'.$cnt.'_to;
$_for'.$cnt.'_to = $tmp;
}
for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] <= $_for'.$cnt.'_to; $this->scope['.$name.'] += $_for'.$cnt.'_step)'."\n\t{";
// if input are pure numbers it shouldn't reorder them, if it's variables it gets too messy though so in that case a counter should be used
$reverse = false;
$condition = '<=';
$incrementer = '+';
if (preg_match('{^(["\']?)([0-9]+)\1$}', $from, $mN1) && preg_match('{^(["\']?)([0-9]+)\1$}', $to, $mN2)) {
$from = (int) $mN1[2];
$to = (int) $mN2[2];
if ($from > $to) {
$reverse = true;
$condition = '>=';
$incrementer = '-';
}
}
// reverse from and to if needed
if (!$reverse) {
$out .= "\n\t".'if ($_for'.$cnt.'_from > $_for'.$cnt.'_to) {
$tmp = $_for'.$cnt.'_from;
$_for'.$cnt.'_from = $_for'.$cnt.'_to;
$_for'.$cnt.'_to = $tmp;
}';
}
$out .= '
for ($this->scope['.$name.'] = $_for'.$cnt.'_from; $this->scope['.$name.'] '.$condition.' $_for'.$cnt.'_to; $this->scope['.$name.'] '.$incrementer.'= $_for'.$cnt.'_step)'."\n\t{";
// updates properties
if ($usesIndex) {
$out.="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
$out .="\n\t\t".'$_for'.$cnt.'_glob["index"] = $this->scope['.$name.'];';
}
if ($usesFirst) {
$out .= "\n\t\t".'$_for'.$cnt.'_glob["first"] = (string) ($_for'.$cnt.'_glob["iteration"] === 1);';
......@@ -109,7 +129,7 @@ class Dwoo_Plugin_for extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Bloc
$postOut = Dwoo_Compiler::PHP_OPEN . '/* -- for end output */';
// update properties
if ($usesIteration) {
$postOut.="\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
$postOut .= "\n\t\t".'$_for'.$cnt.'_glob["iteration"]+=1;';
}
// end loop
$postOut .= "\n\t}\n}\n".Dwoo_Compiler::PHP_CLOSE;
......
......@@ -307,6 +307,11 @@ baz"));
$tpl->forceCompilation();
$this->assertEquals('-1|-1-2|-1-2-3', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
$tpl = new Dwoo_Template_String('{for i 10 7}-{$i}{/for}');
$tpl->forceCompilation();
$this->assertEquals('-10-9-8-7', $this->dwoo->get($tpl, array('sub'=>array('foo','bar','baz','qux')), $this->compiler));
}
public function testForElse()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment