|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\RenderContext; |
|
4
|
|
|
|
|
5
|
|
|
use League\Plates; |
|
6
|
|
|
|
|
7
|
|
|
function layoutFunc() { |
|
8
|
|
|
return function(FuncArgs $args) { |
|
9
|
|
|
list($name, $data) = $args->args; |
|
10
|
|
|
|
|
11
|
|
|
$layout = $args->template->fork($name, $data ?: []); |
|
12
|
|
|
Plates\Template\setLayout($args->template, $layout); |
|
13
|
|
|
|
|
14
|
|
|
return $layout; |
|
15
|
|
|
}; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
function sectionFunc() { |
|
19
|
|
|
return function(FuncArgs $args) { |
|
20
|
|
|
list($name) = $args->args; |
|
21
|
|
|
return Plates\Template\getSections($args->template)->get($name); |
|
22
|
|
|
}; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
const START_APPEND = 0; |
|
26
|
|
|
const START_PREPEND = 1; |
|
27
|
|
|
const START_REPLACE = 2; |
|
28
|
|
|
|
|
29
|
|
|
/** Starts the output buffering for a section, update of 0 = replace, 1 = append, 2 = prepend */ |
|
30
|
|
|
function startFunc($update = START_REPLACE) { |
|
31
|
|
|
return function(FuncArgs $args) use ($update) { |
|
32
|
|
|
$section_def = $args->template->getContextItem('_section_def') ?: []; |
|
33
|
|
|
|
|
34
|
|
|
ob_start(); |
|
35
|
|
|
$section_def[] = [$args->args[0], $update, ob_get_level()]; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$args->template->context['_section_def'] = $section_def; |
|
39
|
|
|
}; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function endFunc() { |
|
43
|
|
|
return function(FuncArgs $args) { |
|
44
|
|
|
$section_def = $args->template->getContextItem('_section_def') ?: []; |
|
45
|
|
|
if (!count($section_def)) { |
|
46
|
|
|
throw new Plates\Exception\FuncException('Cannot end a section definition because no section has been started.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
list($name, $update, $ob_level) = array_pop($section_def); |
|
50
|
|
|
|
|
51
|
|
|
if ($ob_level != ob_get_level()) { |
|
52
|
|
|
throw new Plates\Exception\FuncException('Output buffering level does not match when section was started.'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$contents = ob_get_clean(); |
|
56
|
|
|
|
|
57
|
|
|
$sections = Plates\Template\getSections($args->template); |
|
58
|
|
|
|
|
59
|
|
|
if ($update === START_APPEND) { |
|
60
|
|
|
$sections->append($name, $contents); |
|
61
|
|
|
} else if ($update === START_PREPEND) { |
|
62
|
|
|
$sections->prepend($name, $contents); |
|
63
|
|
|
} else { |
|
64
|
|
|
$sections->add($name, $contents); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$args->template->context['_section_def'] = $section_def; |
|
68
|
|
|
}; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function insertFunc($echo = null) { |
|
72
|
|
|
$echo = $echo ?: function($v) { echo $v; }; |
|
73
|
|
|
|
|
74
|
|
|
return function(FuncArgs $args) use ($echo) { |
|
75
|
|
|
list($name, $data) = $args->args; |
|
76
|
|
|
$child = $args->template->fork($name, $data ?: []); |
|
77
|
|
|
$echo($args->render->renderTemplate($child)); |
|
78
|
|
|
}; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
function escapeFunc($flags = ENT_COMPAT | ENT_HTML401, $encoding = 'UTF-8') { |
|
82
|
|
|
return function(FuncArgs $args) use ($flags, $encoding) { |
|
83
|
|
|
return htmlspecialchars($args->args[0], $flags, $encoding); |
|
84
|
|
|
}; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
function assertArgsFunc($num_required, $num_default = 0) { |
|
88
|
|
|
return function(FuncArgs $args, $next) use ($num_required, $num_default) { |
|
89
|
|
|
if (count($args->args) < $num_required) { |
|
90
|
|
|
throw new Plates\Exception\FuncException("Func {$args->func_name} has {$num_required} argument(s)."); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (count($args->args) >= $num_required + $num_default) { |
|
94
|
|
|
return $next($args); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$args = $args->withArgs(array_merge($args->args, array_fill( |
|
98
|
|
|
0, |
|
99
|
|
|
$num_required + $num_default - count($args->args), |
|
100
|
|
|
null |
|
101
|
|
|
))); |
|
102
|
|
|
|
|
103
|
|
|
return $next($args); |
|
104
|
|
|
}; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
function assertTemplateArgsFunc() { |
|
108
|
|
|
return assertArgsFunc(1, 1); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** Creates aliases for certain functions */ |
|
112
|
|
|
function aliasNameFunc(array $aliases) { |
|
113
|
|
|
return function(FuncArgs $args, $next) use ($aliases) { |
|
114
|
|
|
if (!isset($aliases[$args->func_name])) { |
|
115
|
|
|
return $next($args); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
while (isset($aliases[$args->func_name])) { |
|
119
|
|
|
$args = $args->withName($aliases[$args->func_name]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $next($args); |
|
123
|
|
|
}; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** Allows splitting of the handlers from the args name */ |
|
127
|
|
|
function splitByNameFunc(array $handlers) { |
|
128
|
|
|
return function(FuncArgs $args, $next) use ($handlers) { |
|
129
|
|
|
$name = $args->func_name ?: '__invoke'; |
|
130
|
|
|
if (isset($handlers[$name])) { |
|
131
|
|
|
$handler = Plates\Util\stackGroup($handlers[$name]); |
|
132
|
|
|
return $handler($args, $next); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $next($args); |
|
136
|
|
|
}; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
function platesFunc() { |
|
140
|
|
|
return Plates\Util\stackGroup([ |
|
141
|
|
|
aliasNameFunc([ |
|
142
|
|
|
'e' => 'escape', |
|
143
|
|
|
'__invoke' => 'escape', |
|
144
|
|
|
'stop' => 'end', |
|
145
|
|
|
]), |
|
146
|
|
|
splitByNameFunc([ |
|
147
|
|
|
'layout' => [assertTemplateArgsFunc(), layoutFunc()], |
|
148
|
|
|
'section' => [assertArgsFunc(1), sectionFunc()], |
|
149
|
|
|
'insert' => [assertTemplateArgsFunc(), insertFunc()], |
|
150
|
|
|
'escape' => [assertArgsFunc(1), escapeFunc()], |
|
151
|
|
|
'start' => [assertArgsFunc(1), startFunc()], |
|
152
|
|
|
'push' => [assertArgsFunc(1), startFunc(START_APPEND)], |
|
153
|
|
|
'unshift' => [assertArgsFunc(1), startFunc(START_PREPEND)], |
|
154
|
|
|
'end' => [endFunc()] |
|
155
|
|
|
]) |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
|