Фильтры и Циклы

This commit is contained in:
a.a.pozdina 2015-12-02 00:39:27 +03:00
parent 8a2eead4a0
commit f02a0bac42
3 changed files with 20 additions and 30 deletions

View file

@ -2,11 +2,14 @@
require_once 'klein.php'; require_once 'klein.php';
function title($x) {
return ucfirst($x);
}
$tpl = new Klein(); $tpl = new Klein();
$tpl->compile('index.html'); $tpl->compile('index.html');
echo $tpl->render(array( echo $tpl->render(array(
'style' => array('style.css'), 'pagename' => 'awesome people',
'script' => array(array('name' => 'script1.js'), array('name' => 'script2.js')), 'authors' => ['Paul', 'Jim', 'Jane']
'content' => 'test'
)); ));

View file

@ -1,21 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
{% for file in style %}
<link type="text/css" rel="stylesheet" href="{{ file }}"/>
{% endfor %}
</head>
<body>
{% if content %}
{{ content }}
{% endif %}
{% for file in script %} <h1>{{ pagename|title }}</h1>
<script type="text/javascript" src="{{ file.name }}"></script> <ul>
{% endfor %} {% for author in authors %}
</body> <li{% if loop.first %} class="first"{% endif %}>
</html> {{ author }}
</li>
{% endfor %}
</ul>

View file

@ -7,12 +7,12 @@ class Klein {
$arg = function($name) { $arg = function($name) {
$list = explode(".", $name); $list = explode(".", $name);
$first = array_shift($list); $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) { $rx = function ($grammar) use ($arg) {
list($pattern, $result) = $grammar; 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/", return ["/$body/",
function ($x) use ($result, $arg) { function ($x) use ($result, $arg) {
$body = preg_replace_callback("/#(\d+)/", function ($s) use($x, $arg) { $body = preg_replace_callback("/#(\d+)/", function ($s) use($x, $arg) {
@ -23,20 +23,19 @@ class Klein {
}; };
$pattern = array_map($rx, [ $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;"], ["{% endfor %}", "endforeach;"],
["{% if+:v %}", "if(isset(#1)):"], ["{% if+:v %}", "if(isset(\$#1) && \$#1 ):"],
["{% endif %}", "endif;"], ["{% endif %}", "endif;"],
["{{ :v }}", "echo #1;"] ["{{ :v }}", "echo \$#1;"],
["{{ :v\|:x }}", "echo #3(\$#1);"]
]); ]);
$result = file_get_contents($html); $result = file_get_contents($html);
foreach($pattern as $arg) { foreach($pattern as $arg) {
list($key, $value) = $arg; list($key, $value) = $arg;
$result = preg_replace_callback($key, $value, $result); $result = preg_replace_callback($key, $value, $result);
} }
$this->code = $result; $this->code = $result;
} }