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