summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-10-28 18:09:50 +0100
committerKevin Chabowski <kevin@kch42.de>2013-10-28 18:09:50 +0100
commita77a2ec58947f5fc7cb4023db3b8f8298e2f01f2 (patch)
tree0aa5a38a23f8afc49c4c8bc50ad5edd51f4897d2
parent55d47c27247e3cdbd88818c0ba98646ee11fb315 (diff)
downloadste-a77a2ec58947f5fc7cb4023db3b8f8298e2f01f2.tar.gz
ste-a77a2ec58947f5fc7cb4023db3b8f8298e2f01f2.tar.bz2
ste-a77a2ec58947f5fc7cb4023db3b8f8298e2f01f2.zip
ste:foreach now has an else clause for empty arrays.
-rw-r--r--docu/language_definition.html8
-rw-r--r--ste.php23
-rw-r--r--tests/test_foreach/.gitignore3
-rw-r--r--tests/test_foreach/code.php9
-rw-r--r--tests/test_foreach/test.tpl8
-rw-r--r--tests/test_foreach/want5
6 files changed, 53 insertions, 3 deletions
diff --git a/docu/language_definition.html b/docu/language_definition.html
index a21a869..84232de 100644
--- a/docu/language_definition.html
+++ b/docu/language_definition.html
@@ -380,6 +380,14 @@
<code><pre>&lt;ste:foreach array="foo" key="k" value="v" counter="i"&gt;<br /> Number: $i&lt;br /&gt;<br /> Key: $k&lt;br /&gt;<br /> Value: $v&lt;br /&gt;<br /> &lt;br /&gt;<br />&lt;/ste:foreach&gt;</pre></code>
This code will loop through the array <code>foo</code> and return the counter <code>$i</code>, the key <code>$k</code> and the value <code>$v</code> of the current iteration.
</p>
+ <p>
+ There can also be an optional <code>&lt;ste:else&gt;</code> clause, that will be executed, if the input array was empty.
+ </p>
+ <p>
+ Example:<br />
+ <code><pre>&lt;ste:foreach array="foo" value="v"&gt;<br /> &lt;p&gt;$v&lt;/p&gt;<br /> &lt;ste:else&gt;<br /> Array \$foo is empty.<br /> &lt;/ste:else&gt;<br />&lt;/ste:foreach&gt;</pre></code>
+ This code will list all array elements or will display <code>Array $foo is empty</code> if the array <code>$foo</code> is empty.
+ </p>
<h3 id="builtin_infloop">ste:infloop</h3>
<p>Creates an infinitive loop. You can get out of the loop using the <a href="#builtin_break">ste:break</a> tag. Can be used to emulate other loop constructs like while loops.</p>
diff --git a/ste.php b/ste.php
index 3715435..018d754 100644
--- a/ste.php
+++ b/ste.php
@@ -887,14 +887,29 @@ $ste_builtins = array(
$loopbody .= "\$${loopname}_counter++;\n\$ste->set_var_by_name(\$${loopname}_countervar, \$${loopname}_counter);\n";
}
+ $loop = array();
+ $else = array();
+ foreach($ast->sub as $node) {
+ if(($node instanceof TagNode) && ($node->name == "else")) {
+ $else = array_merge($else, $node->sub);
+ } else {
+ $loop[] = $node;
+ }
+ }
+
$loopbody .= "\$ste->set_var_by_name(\$${loopname}_valuevar, \$${loopname}_value);\n";
if(!empty($ast->params["key"])) {
$loopbody .= "\$ste->set_var_by_name(\$${loopname}_keyvar, \$${loopname}_key);\n";
}
$loopbody .= "\n";
- $loopbody .= _transcompile($ast->sub);
+ $loopbody .= _transcompile($loop);
$loopbody = "{\n" . loopbody(indent_code($loopbody)) . "\n}\n";
+ if(!empty($else)) {
+ $code .= "if(empty(\$${loopname}_array))\n{\n";
+ $code .= indent_code(_transcompile($else));
+ $code .= "\n}\nelse ";
+ }
$code .= "foreach(\$${loopname}_array as \$${loopname}_key => \$${loopname}_value)\n$loopbody\n";
return $code;
@@ -1351,13 +1366,15 @@ class STECore {
}
private function &_get_var_reference(&$from, $name, $create_if_not_exist) {
+ $nullref = NULL;
+
$bracket_open = strpos($name, "[");
if($bracket_open === false) {
if(isset($from[$name]) or $create_if_not_exist) {
$ref = &$from[$name];
return $ref;
} else {
- return NULL;
+ return $nullref;
}
} else {
$old_varname = $varname;
@@ -1371,7 +1388,7 @@ class STECore {
if($create_if_not_exist) {
$from[$varname] = array();
} else {
- return NULL;
+ return $nullref;
}
}
try {
diff --git a/tests/test_foreach/.gitignore b/tests/test_foreach/.gitignore
new file mode 100644
index 0000000..de2a41b
--- /dev/null
+++ b/tests/test_foreach/.gitignore
@@ -0,0 +1,3 @@
+have
+*.ast
+*.transc.php
diff --git a/tests/test_foreach/code.php b/tests/test_foreach/code.php
new file mode 100644
index 0000000..380ee69
--- /dev/null
+++ b/tests/test_foreach/code.php
@@ -0,0 +1,9 @@
+<?php
+
+function test_func($ste) {
+ $ste->vars["foo"] = array(
+ "a" => array("a" => 100, "b" => 200),
+ "b" => array("a" => 1, "b" => 2),
+ "c" => array("a" => 42, "b" => 1337)
+ );
+}
diff --git a/tests/test_foreach/test.tpl b/tests/test_foreach/test.tpl
new file mode 100644
index 0000000..1aec8b4
--- /dev/null
+++ b/tests/test_foreach/test.tpl
@@ -0,0 +1,8 @@
+<ste:foreach array="foo" key="k" value="v" counter="i">
+ $i: $k -> $v[a], $v[b]
+</ste:foreach>
+---
+<ste:foreach array="bar" value="v">
+ $v
+ <ste:else>--empty--</ste:else>
+</ste:foreach>
diff --git a/tests/test_foreach/want b/tests/test_foreach/want
new file mode 100644
index 0000000..5710bf7
--- /dev/null
+++ b/tests/test_foreach/want
@@ -0,0 +1,5 @@
+0: a -> 100, 200
+1: b -> 1, 2
+2: c -> 42, 1337
+---
+--empty--