From bb834a0f2dee696be8e5267f83f4f59c32d6f41d 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: Thu, 10 Dec 2015 16:09:22 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BC=D0=B0=D0=BA=D1=80=D0=BE=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example3.php | 11 +++++++++++ klein.php | 21 +++++++++++++-------- template3.html | 9 +++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 example3.php create mode 100644 template3.html diff --git a/example3.php b/example3.php new file mode 100644 index 0000000..fba906a --- /dev/null +++ b/example3.php @@ -0,0 +1,11 @@ +compile('template3.html'); + +echo $tpl->render(array('names' => $data)); diff --git a/klein.php b/klein.php index a7a01f7..82aad66 100644 --- a/klein.php +++ b/klein.php @@ -6,21 +6,26 @@ class Klein { function compile($html) { $pattern = array_map(function ($grammar) { list(, $result) = $grammar; - $body = strtr($grammar[0], [" " => "\s*", "+" => "\s+", ":var" => "(\w+(\s*\.\s*\w+)*)", ":id" => "(\w+)"]); + $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 = Klein::loop(\$index, \$#2);"], - ["{% for+:id , :id+in+:var %}", "foreach(\$#3 as \$#1 => \$#2):"], + ["{% 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;"], - ["{% if+:var %}", "if(isset(\$#1) && \$#1 ):"], + ["{% if+:var %}", "if(isset(#1s) && #1s):"], + ["{% else %}", "else:"], ["{% endif %}", "endif;"], - ["{{ :var }}", "echo \$#1;"], - ["{{ :var\|:id }}", "echo #3(\$#1);"] + ["{{ :var }}", "echo #1s;"], + ["{{ :id\(( :var (, :var )*)?\) }}", "echo macro_#1(#2s);"], + ["{{ :var\|:id }}", "echo #3(#1s);"], + ["{% macro+:id\(( :id (, :id )*)?\) %}", "function macro_#1(#2s) {"], + ["{% endmacro %}", "}"] ]); $result = file_get_contents($html); diff --git a/template3.html b/template3.html new file mode 100644 index 0000000..ff98917 --- /dev/null +++ b/template3.html @@ -0,0 +1,9 @@ +
+{% macro hello(name) %} + hello, {{ name }} +{% endmacro %} + +{% for name in names %} + {{ hello(name) }} +{% endfor %} +
\ No newline at end of file