-
Notifications
You must be signed in to change notification settings - Fork 89
/
template.php
353 lines (325 loc) · 8.54 KB
/
template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/*
Copyright (c) 2009-2019 F3::Factory/Bong Cosca, All rights reserved.
This file is part of the Fat-Free Framework (http://fatfreeframework.com).
This is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or later.
Fat-Free Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
*/
//! XML-style template engine
class Template extends Preview {
//@{ Error messages
const
E_Method='Call to undefined method %s()';
//@}
protected
//! Template tags
$tags,
//! Custom tag handlers
$custom=[];
/**
* Template -set- tag handler
* @return string
* @param $node array
**/
protected function _set(array $node) {
$out='';
foreach ($node['@attrib'] as $key=>$val)
$out.='$'.$key.'='.
(preg_match('/\{\{(.+?)\}\}/',$val?:'')?
$this->token($val):
Base::instance()->stringify($val)).'; ';
return '<?php '.$out.'?>';
}
/**
* Template -include- tag handler
* @return string
* @param $node array
**/
protected function _include(array $node) {
$attrib=$node['@attrib'];
$hive=isset($attrib['with']) &&
($attrib['with']=$this->token($attrib['with'])) &&
preg_match_all('/(\w+)\h*=\h*(.+?)(?=,|$)/',
$attrib['with'],$pairs,PREG_SET_ORDER)?
('['.implode(',',
array_map(function($pair) {
return '\''.$pair[1].'\'=>'.
(preg_match('/^\'.*\'$/',$pair[2]) ||
preg_match('/\$/',$pair[2])?
$pair[2]:Base::instance()->stringify(
Base::instance()->cast($pair[2])));
},$pairs)).']+get_defined_vars()'):
'get_defined_vars()';
$ttl=isset($attrib['ttl'])?(int)$attrib['ttl']:0;
return
'<?php '.(isset($attrib['if'])?
('if ('.$this->token($attrib['if']).') '):'').
('echo $this->render('.
(preg_match('/^\{\{(.+?)\}\}$/',$attrib['href'])?
$this->token($attrib['href']):
Base::instance()->stringify($attrib['href'])).','.
'NULL,'.$hive.','.$ttl.'); ?>');
}
/**
* Template -exclude- tag handler
* @return string
**/
protected function _exclude() {
return '';
}
/**
* Template -ignore- tag handler
* @return string
* @param $node array
**/
protected function _ignore(array $node) {
return $node[0];
}
/**
* Template -loop- tag handler
* @return string
* @param $node array
**/
protected function _loop(array $node) {
$attrib=$node['@attrib'];
unset($node['@attrib']);
return
'<?php for ('.
$this->token($attrib['from']).';'.
$this->token($attrib['to']).';'.
$this->token($attrib['step']).'): ?>'.
$this->build($node).
'<?php endfor; ?>';
}
/**
* Template -repeat- tag handler
* @return string
* @param $node array
**/
protected function _repeat(array $node) {
$attrib=$node['@attrib'];
unset($node['@attrib']);
return
'<?php '.
(isset($attrib['counter'])?
(($ctr=$this->token($attrib['counter'])).'=0; '):'').
'foreach (('.
$this->token($attrib['group']).'?:[]) as '.
(isset($attrib['key'])?
($this->token($attrib['key']).'=>'):'').
$this->token($attrib['value']).'):'.
(isset($ctr)?(' '.$ctr.'++;'):'').' ?>'.
$this->build($node).
'<?php endforeach; ?>';
}
/**
* Template -check- tag handler
* @return string
* @param $node array
**/
protected function _check(array $node) {
$attrib=$node['@attrib'];
unset($node['@attrib']);
// Grab <true> and <false> blocks
foreach ($node as $pos=>$block)
if (isset($block['true']))
$true=[$pos,$block];
elseif (isset($block['false']))
$false=[$pos,$block];
if (isset($true,$false) && $true[0]>$false[0])
// Reverse <true> and <false> blocks
list($node[$true[0]],$node[$false[0]])=[$false[1],$true[1]];
return
'<?php if ('.$this->token($attrib['if']).'): ?>'.
$this->build($node).
'<?php endif; ?>';
}
/**
* Template -true- tag handler
* @return string
* @param $node array
**/
protected function _true(array $node) {
return $this->build($node);
}
/**
* Template -false- tag handler
* @return string
* @param $node array
**/
protected function _false(array $node) {
return '<?php else: ?>'.$this->build($node);
}
/**
* Template -switch- tag handler
* @return string
* @param $node array
**/
protected function _switch(array $node) {
$attrib=$node['@attrib'];
unset($node['@attrib']);
foreach ($node as $pos=>$block)
if (is_string($block) && !preg_replace('/\s+/','',$block))
unset($node[$pos]);
return
'<?php switch ('.$this->token($attrib['expr']).'): ?>'.
$this->build($node).
'<?php endswitch; ?>';
}
/**
* Template -case- tag handler
* @return string
* @param $node array
**/
protected function _case(array $node) {
$attrib=$node['@attrib'];
unset($node['@attrib']);
return
'<?php case '.(preg_match('/\{\{(.+?)\}\}/',$attrib['value'])?
$this->token($attrib['value']):
Base::instance()->stringify($attrib['value'])).': ?>'.
$this->build($node).
'<?php '.(isset($attrib['break'])?
'if ('.$this->token($attrib['break']).') ':'').
'break; ?>';
}
/**
* Template -default- tag handler
* @return string
* @param $node array
**/
protected function _default(array $node) {
return
'<?php default: ?>'.
$this->build($node).
'<?php break; ?>';
}
/**
* Assemble markup
* @return string
* @param $node array|string
**/
function build($node) {
if (is_string($node))
return parent::build($node);
$out='';
foreach ($node as $key=>$val)
$out.=is_int($key)?$this->build($val):$this->{'_'.$key}($val);
return $out;
}
/**
* Extend template with custom tag
* @return NULL
* @param $tag string
* @param $func callback
**/
function extend($tag,$func) {
$this->tags.='|'.$tag;
$this->custom['_'.$tag]=$func;
}
/**
* Call custom tag handler
* @return string|FALSE
* @param $func string
* @param $args array
**/
function __call($func,array $args) {
if ($func[0]=='_')
return call_user_func_array($this->custom[$func],$args);
if (method_exists($this,$func))
return call_user_func_array([$this,$func],$args);
user_error(sprintf(self::E_Method,$func),E_USER_ERROR);
}
/**
* Parse string for template directives and tokens
* @return array
* @param $text string
**/
function parse($text) {
$text=parent::parse($text);
// Build tree structure
for ($ptr=0,$w=5,$len=strlen($text),$tree=[],$tmp='';$ptr<$len;)
if (preg_match('/^(.{0,'.$w.'}?)<(\/?)(?:F3:)?'.
'('.$this->tags.')\b((?:\s+[\w.:@!\-]+'.
'(?:\h*=\h*(?:"(?:.*?)"|\'(?:.*?)\'))?|'.
'\h*\{\{.+?\}\})*)\s*(\/?)>/is',
substr($text,$ptr),$match)) {
if (strlen($tmp) || isset($match[1]))
$tree[]=$tmp.$match[1];
// Element node
if ($match[2]) {
// Find matching start tag
$stack=[];
for($i=count($tree)-1;$i>=0;--$i) {
$item=$tree[$i];
if (is_array($item) &&
array_key_exists($k=strtolower($match[3]),$item) &&
!isset($item[$k][0])) {
// Start tag found
$tree[$i][$k]+=array_reverse($stack);
$tree=array_slice($tree,0,$i+1);
break;
}
else $stack[]=$item;
}
}
else {
// Start tag
$node=&$tree[][strtolower($match[3])];
$node=[];
if ($match[4]) {
// Process attributes
preg_match_all(
'/(?:(\{\{.+?\}\})|([^\s\/"\'=]+))'.
'\h*(?:=\h*(?:"(.*?)"|\'(.*?)\'))?/s',
$match[4],$attr,PREG_SET_ORDER);
foreach ($attr as $kv)
if (!empty($kv[1]) && !isset($kv[3]) && !isset($kv[4]))
$node['@attrib'][]=$kv[1];
else
$node['@attrib'][$kv[1]?:$kv[2]]=
(isset($kv[3]) && $kv[3]!==''?
$kv[3]:
(isset($kv[4]) && $kv[4]!==''?
$kv[4]:NULL));
}
}
$tmp='';
$ptr+=strlen($match[0]);
$w=5;
}
else {
// Text node
$tmp.=substr($text,$ptr,$w);
$ptr+=$w;
if ($w<50)
++$w;
}
if (strlen($tmp))
// Append trailing text
$tree[]=$tmp;
// Break references
unset($node);
return $tree;
}
/**
* Class constructor
* return object
**/
function __construct() {
$ref=new ReflectionClass(get_called_class());
$this->tags='';
foreach ($ref->getmethods() as $method)
if (preg_match('/^_(?=[[:alpha:]])/',$method->name))
$this->tags.=(strlen($this->tags)?'|':'').
substr($method->name,1);
parent::__construct();
}
}