initial commmit

This commit is contained in:
Федор Подлеснов 2015-12-01 15:01:10 +03:00
commit 88fc338b55
3 changed files with 57 additions and 0 deletions

9
example.php Normal file
View file

@ -0,0 +1,9 @@
<?php
require_once 'klein.php';
echo Klein::render('index.html', array(
'style' => array('style.css'),
'script' => array('script.js'),
'content' => 'test'
));

21
index.html Normal file
View file

@ -0,0 +1,21 @@
<!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 %}
<script type="text/javascript" src="{{ file.name }}"></script>
{% endfor %}
</body>
</html>

27
klein.php Normal file
View file

@ -0,0 +1,27 @@
<?php
class Klein {
static function render($html, $vars) {
$data = file_get_contents($html);
$pattern = array(
"/{%\s*for\s+(\w+)\s+in\s+(\w+)\s*%}/" => "<?php foreach(\$$2 as \$$1): ?>",
"/{%\s*if\s+(\w+)\s*%}/" => "<?php if(isset(\$$1)): ?>",
"/{%\s*block\s+(\w+)\s*%}/" => "<?php function $1() { ?>",
"/{%\s*endblock\s*%}/" => "<?php } ?>",
"/{%\s*endfor\s*%}/" => "<?php endforeach; ?>",
"/{%\s*endif\s*%}/" => "<?php endif; ?>",
"/{{\s*(\w+)\s*}}/" => "<?php echo \$$1; ?>",
);
$result = $data;
foreach($pattern as $key => $value) {
$result = preg_replace($key, $value, $result);
}
extract($vars);
ob_start();
eval(" ?>".$result."<?php ");
return ob_get_clean();
}
}