From f02a0bac42ba65c69edc5e759adba05bc59d845d Mon Sep 17 00:00:00 2001 From: "a.a.pozdina" Date: Wed, 2 Dec 2015 00:39:27 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A4=D0=B8=D0=BB=D1=8C=D1=82=D1=80=D1=8B=20?= =?UTF-8?q?=D0=B8=20=D0=A6=D0=B8=D0=BA=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example.php | 9 ++++++--- index.html | 28 ++++++++-------------------- klein.php | 13 ++++++------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/example.php b/example.php index 75d9587..f1b5555 100644 --- a/example.php +++ b/example.php @@ -2,11 +2,14 @@ require_once 'klein.php'; +function title($x) { + return ucfirst($x); +} + $tpl = new Klein(); $tpl->compile('index.html'); echo $tpl->render(array( - 'style' => array('style.css'), - 'script' => array(array('name' => 'script1.js'), array('name' => 'script2.js')), - 'content' => 'test' + 'pagename' => 'awesome people', + 'authors' => ['Paul', 'Jim', 'Jane'] )); \ No newline at end of file diff --git a/index.html b/index.html index a8f521b..6200efa 100644 --- a/index.html +++ b/index.html @@ -1,21 +1,9 @@ - - - - - - {% for file in style %} - - {% endfor %} - - - {% if content %} - {{ content }} - {% endif %} - {% for file in script %} - - {% endfor %} - - - - +

{{ pagename|title }}

+ \ No newline at end of file diff --git a/klein.php b/klein.php index 14be0c5..5b82f79 100644 --- a/klein.php +++ b/klein.php @@ -7,12 +7,12 @@ class Klein { $arg = function($name) { $list = explode(".", $name); $first = array_shift($list); - return "\$$first" . implode("", array_map(function($x) { return "['$x']"; }, $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+)*)"]); + $body = strtr($pattern, [" " => "\s*", "+" => "\s+", ":v" => "(\w+(\s*\.\s*\w+)*)", ":x" => "(\w+)"]); return ["/$body/", function ($x) use ($result, $arg) { $body = preg_replace_callback("/#(\d+)/", function ($s) use($x, $arg) { @@ -23,20 +23,19 @@ class Klein { }; $pattern = array_map($rx, [ - ["{% for+:v+in+:v %}", "foreach(#3 as #1):"], + ["{% for+:v+in+:v %}", "foreach(\$#3 as \$n => \$#1): \$loop = ['first' => \$n == 0];"], ["{% endfor %}", "endforeach;"], - ["{% if+:v %}", "if(isset(#1)):"], + ["{% if+:v %}", "if(isset(\$#1) && \$#1 ):"], ["{% endif %}", "endif;"], - ["{{ :v }}", "echo #1;"] + ["{{ :v }}", "echo \$#1;"], + ["{{ :v\|:x }}", "echo #3(\$#1);"] ]); - $result = file_get_contents($html); foreach($pattern as $arg) { list($key, $value) = $arg; $result = preg_replace_callback($key, $value, $result); } - $this->code = $result; }