Переписал компиляцию
This commit is contained in:
parent
a993c96f33
commit
8a2eead4a0
1 changed files with 24 additions and 33 deletions
57
klein.php
57
klein.php
|
|
@ -10,47 +10,38 @@ class Klein {
|
|||
return "\$$first" . implode("", array_map(function($x) { return "['$x']"; }, $list));
|
||||
};
|
||||
|
||||
$var = "(\w+(\s*\.\s*\w+)*)";
|
||||
$rx = function ($grammar) use ($arg) {
|
||||
list($pattern, $result) = $grammar;
|
||||
$body = strtr($pattern, [" " => "\s*", "+" => "\s+", ":v" => "(\w+(\s*\.\s*\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 "<?php $body ?>";
|
||||
}];
|
||||
};
|
||||
|
||||
$pattern = array_map($rx, [
|
||||
["{% for+:v+in+:v %}", "foreach(#3 as #1):"],
|
||||
["{% endfor %}", "endforeach;"],
|
||||
["{% if+:v %}", "if(isset(#1)):"],
|
||||
["{% endif %}", "endif;"],
|
||||
["{{ :v }}", "echo #1;"]
|
||||
]);
|
||||
|
||||
list($for, $endfor, $if, $endif) = array_map(
|
||||
function ($args) {
|
||||
$body = implode($args, "\s*");
|
||||
return "/{%\s*$body\s*%}/";
|
||||
},[
|
||||
['for', $var, 'in', $var],
|
||||
['endfor'],
|
||||
['if', $var],
|
||||
['endif']
|
||||
]);
|
||||
|
||||
$pattern = [
|
||||
$for => function ($x) use($arg) {
|
||||
return "<?php foreach({$arg($x[3])} as {$arg($x[1])}): ?>";
|
||||
},
|
||||
$endfor => function ($x) {
|
||||
return "<?php endforeach; ?>";
|
||||
},
|
||||
$if => function ($x) use ($arg) {
|
||||
return "<?php if(isset({$arg($x[1])})): ?>";
|
||||
},
|
||||
$endif => function ($x) {
|
||||
return "<?php endif; ?>";
|
||||
},
|
||||
"/{{\s*$var\s*}}/" => function ($x) use ($arg) {
|
||||
return "<?php echo {$arg($x[1])}; ?>";
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
$result = file_get_contents($html);
|
||||
foreach($pattern as $key => $value) {
|
||||
foreach($pattern as $arg) {
|
||||
list($key, $value) = $arg;
|
||||
$result = preg_replace_callback($key, $value, $result);
|
||||
}
|
||||
|
||||
$this->code = $result;
|
||||
}
|
||||
|
||||
function render($vars) {
|
||||
extract($vars);
|
||||
function render($vars) {
|
||||
extract($vars);
|
||||
ob_start();
|
||||
eval(" ?>".$this->code."<?php ");
|
||||
return ob_get_clean();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue