diff --git a/klein.php b/klein.php index bb0d56a..a7a01f7 100644 --- a/klein.php +++ b/klein.php @@ -9,12 +9,12 @@ class Klein { $body = strtr($grammar[0], [" " => "\s*", "+" => "\s+", ":var" => "(\w+(\s*\.\s*\w+)*)", ":id" => "(\w+)"]); return ["/$body/", function ($x) use ($result) { - return ""; }]; }, [ - ["{% for+:id+in+:var %}", "foreach(\$#2 as \$index => \$#1): \$loop = ['first' => \$index == 0, 'last' => \$index == count(\$#2) - 1];"], + ["{% for+:id+in+:var %}", "foreach(\$#2 as \$index => \$#1): \$loop = Klein::loop(\$index, \$#2);"], ["{% for+:id , :id+in+:var %}", "foreach(\$#3 as \$#1 => \$#2):"], ["{% endfor %}", "endforeach;"], ["{% if+:var %}", "if(isset(\$#1) && \$#1 ):"], @@ -31,6 +31,14 @@ class Klein { $this->code = $result; } + static function loop($idx, &$array) { + $is_even = $idx % 2; + return ['first' => $idx == 0 + , 'last' => $idx == count($array) - 1 + , 'odd' => !$is_even + , 'even' => $is_even]; + } + function render($vars) { extract($vars); ob_start(); diff --git a/template2.html b/template2.html index 338692e..57ad54a 100644 --- a/template2.html +++ b/template2.html @@ -1,7 +1,7 @@