Немного документации
This commit is contained in:
parent
bb834a0f2d
commit
999fe634de
3 changed files with 67 additions and 5 deletions
|
|
@ -6,13 +6,14 @@ class Klein {
|
||||||
function compile($html) {
|
function compile($html) {
|
||||||
$pattern = array_map(function ($grammar) {
|
$pattern = array_map(function ($grammar) {
|
||||||
list(, $result) = $grammar;
|
list(, $result) = $grammar;
|
||||||
$body = strtr($grammar[0], [' ' => "\s*", '+' => "\s+", ':var' => "(\w+(\s*\.\s*\w+)*)", ':id' => "(\w+)"]);
|
$body = strtr($grammar[0], [' ' => "\s*", '+' => "\s+", ':var' => "(\w+(\s*\.\s*\w+)*)", ':id' => "(\w+)", '{{' => "{{\!?"]);
|
||||||
return ["/$body/",
|
return ["/$body/",
|
||||||
function ($x) use ($result) {
|
function ($x) use ($result) {
|
||||||
return "<?php " . preg_replace_callback("/#(\d+)(s?)/", function ($s) use($x) {
|
$code = "<?php " . preg_replace_callback("/#(\d+)(s?)/", function ($s) use($x) {
|
||||||
$pref = $s[2] == 's' ? '$' : '';
|
$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]])));
|
return implode(", ", array_map(function ($s) use($pref) { return $pref.preg_replace("/\.\s*(\w+)/", "['$1']", $s); }, preg_split("/\s*,\s*/", $x[$s[1]])));
|
||||||
}, $result) . " ?>";
|
}, $result) . " ?>";
|
||||||
|
return ($x[0][2] == '!') ? "<!-- $code -->" : $code;
|
||||||
}];
|
}];
|
||||||
}, [
|
}, [
|
||||||
["{% for+:id+in+:var %}", "foreach(#2s as \$index => #1s): \$loop = Klein::loop(\$index, #2s);"],
|
["{% for+:id+in+:var %}", "foreach(#2s as \$index => #1s): \$loop = Klein::loop(\$index, #2s);"],
|
||||||
|
|
@ -22,9 +23,9 @@ class Klein {
|
||||||
["{% else %}", "else:"],
|
["{% else %}", "else:"],
|
||||||
["{% endif %}", "endif;"],
|
["{% endif %}", "endif;"],
|
||||||
["{{ :var }}", "echo #1s;"],
|
["{{ :var }}", "echo #1s;"],
|
||||||
["{{ :id\(( :var (, :var )*)?\) }}", "echo macro_#1(#2s);"],
|
["{{ :id \(( :var (, :var )*)?\) }}", "echo macro_#1(#2s);"],
|
||||||
["{{ :var\|:id }}", "echo #3(#1s);"],
|
["{{ :var\|:id }}", "echo #3(#1s);"],
|
||||||
["{% macro+:id\(( :id (, :id )*)?\) %}", "function macro_#1(#2s) {"],
|
["{% macro+:id \(( :id (, :id )*)?\) %}", "function macro_#1(#2s) {"],
|
||||||
["{% endmacro %}", "}"]
|
["{% endmacro %}", "}"]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
||||||
61
readme.md
Normal file
61
readme.md
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
#Простой шаблонизатор
|
||||||
|
|
||||||
|
#Переменные
|
||||||
|
|
||||||
|
Для переменной name=Андрей шаблон
|
||||||
|
```html
|
||||||
|
<b>Hello, {{ name }}</div>
|
||||||
|
```
|
||||||
|
преобразуется в
|
||||||
|
```html
|
||||||
|
<b>Hello, Андрей</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
```html
|
||||||
|
<b>Hello, {{! name }} </div>
|
||||||
|
```
|
||||||
|
преобразуется в
|
||||||
|
```html
|
||||||
|
<b>Hello, <!-- Андрей --></div>
|
||||||
|
```
|
||||||
|
|
||||||
|
#Циклы
|
||||||
|
|
||||||
|
```html
|
||||||
|
<ul>
|
||||||
|
{% for user in users %}
|
||||||
|
<li>Hello, {{ user.name }}</li>
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
#Условия
|
||||||
|
|
||||||
|
```html
|
||||||
|
<ul>
|
||||||
|
{% for user in users %}
|
||||||
|
{% if user.is_russian %}
|
||||||
|
<li>Привет, {{ user.name }}</li>
|
||||||
|
{% else %}
|
||||||
|
<li>Hello, {{ user.name }}</li>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
||||||
|
#Макрос
|
||||||
|
|
||||||
|
```html
|
||||||
|
{% macro hello(user) %}
|
||||||
|
{% if user.is_russian %}
|
||||||
|
<li>Привет, {{ user.name }}</li>
|
||||||
|
{% else %}
|
||||||
|
<li>Hello, {{ user.name }}</li>
|
||||||
|
{% endif %}
|
||||||
|
{% endmacro %}
|
||||||
|
<ul>
|
||||||
|
{% for user in users %}
|
||||||
|
{% hello(user) %}
|
||||||
|
{% endfor %}
|
||||||
|
</ul>
|
||||||
|
```
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<div>
|
<div>
|
||||||
{% macro hello(name) %}
|
{% macro hello(name) %}
|
||||||
<em>hello, {{ name }}<em>
|
<em>hello, {{ name }} {{! name }}<em>
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% for name in names %}
|
{% for name in names %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue