Обьекты в качесте значений
This commit is contained in:
parent
16fdce2f75
commit
a488b6085a
3 changed files with 58 additions and 7 deletions
32
Klein.php
32
Klein.php
|
|
@ -1,7 +1,13 @@
|
|||
<?php
|
||||
|
||||
class Klein {
|
||||
private $code;
|
||||
public $code;
|
||||
|
||||
function __construct($html = null) {
|
||||
if ($html) {
|
||||
$this->compile($html);
|
||||
}
|
||||
}
|
||||
|
||||
function compile($html) {
|
||||
$pattern = array_map(function ($grammar) {
|
||||
|
|
@ -12,7 +18,11 @@ class Klein {
|
|||
$code = "<?php " . preg_replace_callback("/#(\d+)(s?)/", function ($s) use($x) {
|
||||
$pref = $s[2] == 's' ? '$' : '';
|
||||
return implode(", ", array_map(function ($s) use($pref) {
|
||||
return $pref.preg_replace("/\.\s*(\w+)/", "['$1']", $s); }, preg_split("/\s*,\s*/", $x[$s[1]])));
|
||||
$list = preg_split('/\.\s*/', $s);
|
||||
$first = array_shift($list);
|
||||
|
||||
return count($list) > 0 ? "Klein::get(".$pref.$first.", [".implode(",", array_map(function ($x) { return "'$x'"; }, $list))."])" : $pref.$first;
|
||||
}, preg_split("/\s*,\s*/", $x[$s[1]])));
|
||||
}, $result) . " ?>";
|
||||
return ($x[0][2] == '!') ? "<!-- $code -->" : $code;
|
||||
}];
|
||||
|
|
@ -20,8 +30,8 @@ class Klein {
|
|||
["{% 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;"],
|
||||
["{% ifset+:var %}", "if(isset(#1s)):"],
|
||||
["{% if+:var %}", "if(isset(#1s) && #1s):"],
|
||||
["{% ifset+:var %}", "if(#1s !== null):"],
|
||||
["{% if+:var %}", "if(#1s !== null && #1s):"],
|
||||
["{% if+:id \(( :var (, :var )*)?\) %}", "if(macro_#1(#2s)):"],
|
||||
["{% unless+:var %}", "if(!(isset(#1s) && #1s)):"],
|
||||
["{% else %}", "else:"],
|
||||
|
|
@ -41,6 +51,18 @@ class Klein {
|
|||
$this->code = $result;
|
||||
}
|
||||
|
||||
static function get($arr, $items) {
|
||||
$result = $arr;
|
||||
foreach ($items as $key) {
|
||||
if (is_array($result)) {
|
||||
$result = $result[$key];
|
||||
} else {
|
||||
$result = $result->{$key};
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
static function loop($idx, &$array) {
|
||||
$is_even = $idx % 2;
|
||||
return ['first' => $idx == 0
|
||||
|
|
@ -55,4 +77,4 @@ class Klein {
|
|||
eval(" ?>".$this->code."<?php ");
|
||||
return ob_get_clean();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,7 @@ function title($x) {
|
|||
return ucfirst($x);
|
||||
}
|
||||
|
||||
$tpl = new Klein();
|
||||
$tpl->compile('tempalte.html');
|
||||
$tpl = new Klein('tempalte.html');
|
||||
|
||||
echo $tpl->render(array(
|
||||
'pagename' => 'awesome people',
|
||||
|
|
|
|||
30
examples/example4.php
Normal file
30
examples/example4.php
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
require_once '../klein.php';
|
||||
|
||||
function title($x) {
|
||||
return ucfirst($x);
|
||||
}
|
||||
|
||||
$tpl = new Klein('tempalte.html');
|
||||
|
||||
$u1 = new stdClass();
|
||||
$u1->name = 'Paul';
|
||||
$u1->age = 10;
|
||||
|
||||
$u2 = new stdClass();
|
||||
$u2->name = 'Jim';
|
||||
$u2->age = 11;
|
||||
|
||||
$u3 = new stdClass();
|
||||
$u3->name = 'Jane';
|
||||
$u3->age = 12;
|
||||
|
||||
echo $tpl->render(array(
|
||||
'pagename' => 'awesome people',
|
||||
'authors' => [$u1, $u2, $u3],
|
||||
'city'=> [
|
||||
'Yaroslaval' => 1,
|
||||
'Moscow' => 2
|
||||
]
|
||||
));
|
||||
Loading…
Add table
Add a link
Reference in a new issue