Добавил ifset которая реагирует только на установленность.
This commit is contained in:
parent
dd95d4aeb0
commit
1e4513c26d
1 changed files with 10 additions and 9 deletions
19
klein.php
19
klein.php
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
class Klein {
|
||||
class Klein {
|
||||
private $code;
|
||||
|
||||
function compile($html) {
|
||||
|
|
@ -10,16 +10,17 @@ class Klein {
|
|||
return ["/$body/",
|
||||
function ($x) use ($result) {
|
||||
$code = "<?php " . preg_replace_callback("/#(\d+)(s?)/", function ($s) use($x) {
|
||||
$pref = $s[2] == 's' ? '$' : '';
|
||||
return implode(", ", array_map(function ($s) use($pref) {
|
||||
$pref = $s[2] == 's' ? '$' : '';
|
||||
return implode(", ", array_map(function ($s) use($pref) {
|
||||
return $pref.preg_replace("/\.\s*(\w+)/", "['$1']", $s); }, preg_split("/\s*,\s*/", $x[$s[1]])));
|
||||
}, $result) . " ?>";
|
||||
}, $result) . " ?>";
|
||||
return ($x[0][2] == '!') ? "<!-- $code -->" : $code;
|
||||
}];
|
||||
}, [
|
||||
["{% for+:id+in+:var %}", "foreach(#2s as \$index => #1s): \$loop = Klein::loop(\$index, #2s);"],
|
||||
["{% for+:id , :id+in+:var %}", "foreach(#3s as #1s => #2s):"],
|
||||
["{% endfor %}", "endforeach;"],
|
||||
["{% ifset+:var %}", "if(isset(#1s)):"],
|
||||
["{% if+:var %}", "if(isset(#1s) && #1s):"],
|
||||
["{% unless+:var %}", "if(!(isset(#1s) && #1s)):"],
|
||||
["{% else %}", "else:"],
|
||||
|
|
@ -30,7 +31,7 @@ class Klein {
|
|||
["{% macro+:id \(( :id (, :id )*)?\) %}", "function macro_#1(#2s) {"],
|
||||
["{% endmacro %}", "}"]
|
||||
]);
|
||||
|
||||
|
||||
$result = file_get_contents($html);
|
||||
foreach($pattern as $arg) {
|
||||
$result = preg_replace_callback($arg[0], $arg[1], $result);
|
||||
|
|
@ -42,10 +43,10 @@ class Klein {
|
|||
static function loop($idx, &$array) {
|
||||
$is_even = $idx % 2;
|
||||
return ['first' => $idx == 0
|
||||
, 'last' => $idx == count($array) - 1
|
||||
, 'odd' => !$is_even
|
||||
, 'even' => $is_even];
|
||||
}
|
||||
, 'last' => $idx == count($array) - 1
|
||||
, 'odd' => !$is_even
|
||||
, 'even' => $is_even];
|
||||
}
|
||||
|
||||
function render($vars) {
|
||||
extract($vars);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue