From a488b6085a15ff4bf5bde115bad2b1bf0a850bc2 Mon Sep 17 00:00:00 2001 From: origami11 Date: Tue, 28 Mar 2017 17:29:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D1=8C=D0=B5=D0=BA=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=BA=D0=B0=D1=87=D0=B5=D1=81=D1=82=D0=B5=20=D0=B7?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B5=D0=BD=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Klein.php | 32 +++++++++++++++++++++++++++----- examples/example.php | 3 +-- examples/example4.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 examples/example4.php diff --git a/Klein.php b/Klein.php index 13a36bc..09eddc9 100644 --- a/Klein.php +++ b/Klein.php @@ -1,7 +1,13 @@ compile($html); + } + } function compile($html) { $pattern = array_map(function ($grammar) { @@ -12,7 +18,11 @@ class Klein { $code = " 0 ? "Klein::get(".$pref.$first.", [".implode(",", array_map(function ($x) { return "'$x'"; }, $list))."])" : $pref.$first; + }, preg_split("/\s*,\s*/", $x[$s[1]]))); }, $result) . " ?>"; return ($x[0][2] == '!') ? "" : $code; }]; @@ -20,8 +30,8 @@ class Klein { ["{% 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):"], + ["{% ifset+:var %}", "if(#1s !== null):"], + ["{% if+:var %}", "if(#1s !== null && #1s):"], ["{% if+:id \(( :var (, :var )*)?\) %}", "if(macro_#1(#2s)):"], ["{% unless+:var %}", "if(!(isset(#1s) && #1s)):"], ["{% else %}", "else:"], @@ -41,6 +51,18 @@ class Klein { $this->code = $result; } + static function get($arr, $items) { + $result = $arr; + foreach ($items as $key) { + if (is_array($result)) { + $result = $result[$key]; + } else { + $result = $result->{$key}; + } + } + return $result; + } + static function loop($idx, &$array) { $is_even = $idx % 2; return ['first' => $idx == 0 @@ -55,4 +77,4 @@ class Klein { eval(" ?>".$this->code."compile('tempalte.html'); +$tpl = new Klein('tempalte.html'); echo $tpl->render(array( 'pagename' => 'awesome people', diff --git a/examples/example4.php b/examples/example4.php new file mode 100644 index 0000000..5e1f739 --- /dev/null +++ b/examples/example4.php @@ -0,0 +1,30 @@ +name = 'Paul'; +$u1->age = 10; + +$u2 = new stdClass(); +$u2->name = 'Jim'; +$u2->age = 11; + +$u3 = new stdClass(); +$u3->name = 'Jane'; +$u3->age = 12; + +echo $tpl->render(array( + 'pagename' => 'awesome people', + 'authors' => [$u1, $u2, $u3], + 'city'=> [ + 'Yaroslaval' => 1, + 'Moscow' => 2 + ] +)); \ No newline at end of file