From a49f10601b13c1fe1a8d41b31b8420e6a73f17b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D0=B5=D0=B4=D0=BE=D1=80=20=D0=9F=D0=BE=D0=B4=D0=BB?= =?UTF-8?q?=D0=B5=D1=81=D0=BD=D0=BE=D0=B2?= Date: Wed, 2 Dec 2015 11:06:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BE=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example.php | 2 +- index.html | 5 +++-- klein.php | 39 +++++++++++++++------------------------ 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/example.php b/example.php index f1b5555..19d442c 100644 --- a/example.php +++ b/example.php @@ -11,5 +11,5 @@ $tpl->compile('index.html'); echo $tpl->render(array( 'pagename' => 'awesome people', - 'authors' => ['Paul', 'Jim', 'Jane'] + 'authors' => [['name' => 'Paul', 'age' => 10], ['name' => 'Jim', 'age' => 11], ['name' => 'Jane', 'age' => 12]] )); \ No newline at end of file diff --git a/index.html b/index.html index 6200efa..8270d6c 100644 --- a/index.html +++ b/index.html @@ -2,8 +2,9 @@

{{ pagename|title }}

\ No newline at end of file diff --git a/klein.php b/klein.php index 5b82f79..99a43cb 100644 --- a/klein.php +++ b/klein.php @@ -4,38 +4,29 @@ class Klein { private $code; function compile($html) { - $arg = function($name) { - $list = explode(".", $name); - $first = array_shift($list); - return "$first" . implode("", array_map(function($x) { return "['$x']"; }, $list)); - }; - - $rx = function ($grammar) use ($arg) { - list($pattern, $result) = $grammar; - $body = strtr($pattern, [" " => "\s*", "+" => "\s+", ":v" => "(\w+(\s*\.\s*\w+)*)", ":x" => "(\w+)"]); + $pattern = array_map(function ($grammar) { + list(, $result) = $grammar; + $body = strtr($grammar[0], [" " => "\s*", "+" => "\s+", ":var" => "(\w+(\s*\.\s*\w+)*)", ":id" => "(\w+)"]); return ["/$body/", - function ($x) use ($result, $arg) { - $body = preg_replace_callback("/#(\d+)/", function ($s) use($x, $arg) { - return $arg($x[$s[1]]); - }, $result); - return ""; + function ($x) use ($result) { + return ""; }]; - }; - - $pattern = array_map($rx, [ - ["{% for+:v+in+:v %}", "foreach(\$#3 as \$n => \$#1): \$loop = ['first' => \$n == 0];"], + }, [ + ["{% for+:id+in+:var %}", "foreach(\$#2 as \$index => \$#1): \$loop = ['first' => \$index == 0, 'last' => \$index == count(\$#2) - 1];"], ["{% endfor %}", "endforeach;"], - ["{% if+:v %}", "if(isset(\$#1) && \$#1 ):"], + ["{% if+:var %}", "if(isset(\$#1) && \$#1 ):"], ["{% endif %}", "endif;"], - ["{{ :v }}", "echo \$#1;"], - ["{{ :v\|:x }}", "echo #3(\$#1);"] + ["{{ :var }}", "echo \$#1;"], + ["{{ :var\|:id }}", "echo #3(\$#1);"] ]); - + $result = file_get_contents($html); foreach($pattern as $arg) { - list($key, $value) = $arg; - $result = preg_replace_callback($key, $value, $result); + $result = preg_replace_callback($arg[0], $arg[1], $result); } + $this->code = $result; }