Newer
Older
<?php
/**
* HTML template rendering functions.
*
* @author Taddeus Kroes
* @version 1.0
* @date 14-07-2012
*/
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
require_once 'node.php';
/**
* A Template object represents a template file.
*
* A template file contains 'blocks' that can be rendered zero or more times.
* Each block has a set of properties that can be accessed using curly
* brackets ('{' and '}'). Curly brackets may contain macro's to minimize
* common view logic in controllers.
*
* Example template 'page.tpl':
* <code>
* <html>
* <head>
* <title>{page_title}</title>
* </head>
* <body>
* <h1>{page_title}</h1>
* <div id="content">{page_content}</div>
* <div id="ads">
* {block:ad}
* <div class="ad">{ad_content}</div>
* {end}
* </div>
* </body>
* </html>
* </code>
* And the corresponding PHP code:
* <code>
* $tpl = new Template('page');
* $tpl->set(array(
* 'page_title' => 'Some title',
* 'page_content' => 'Lorem ipsum ...'
* ));
*
* foreach( array('Some ad', 'Another ad', 'More ads') as $ad )
* $tpl->add('ad')->set('ad_content', $ad);
*
* echo $tpl->render();
* </code>
* The output will be:
* <code>
* <html>
* <head>
* <title>Some title</title>
* </head>
* <body>
* <h1>Some title</h1>
* <div id="content">Some content</div>
* <div id="ads">
* <div class="ad">Some ad</div>
* <div class="ad">Another ad</div>
* <div class="ad">More ads</div>
* </div>
* </body>
* </html>
* </code>
*
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
*/
class Template extends Node {
/**
* Default extension of template files.
*
* @var array
*/
const DEFAULT_EXTENSION = '.tpl';
/**
* Root directories from which template files are included.
*
* @var array
*/
private static $include_path = array();
/**
* The path the template was found in.
*
* @var string
*/
private $path;
/**
* The content of the template file.
*
* @var string
*/
private $file_content;
/**
* The block structure of the template file.
*
* @var Node
*/
private $root_block;
/**
* Create a new Template object, representing a template file.
*
* Template files are assumed to have the .tpl extension. If no extension
* is specified, '.tpl' is appended to the filename.
*
* @param string $filename The path to the template file from one of the root directories.
*/
function __construct($filename) {
// Add default extension if none is found
strpos($filename, '.') === false && $filename .= self::DEFAULT_EXTENSION;
$look_in = count(self::$include_path) ? self::$include_path : array('.');
$found = false;
foreach( $look_in as $root ) {
$path = $root.$filename;
if( file_exists($path) ) {
$this->path = $path;
$this->file_content = file_get_contents($path);
$found = true;
break;
}
}
if( !$found ) {
throw new \RuntimeException(
sprintf("Could not find template file \"%s\", looked in folders:\n%s",
$filename, implode("\n", $look_in))
);
}
$this->parse_blocks();
}
/**
* Get the path to the template file (including one of the include paths).
*
* @return string The path to the template file.
*/
function get_path() {
return $this->path;
}
/**
* Parse the content of the template file into a tree structure of blocks
* and variables.
*
* @throws ParseError If an {end} tag is not used properly.
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
354
355
356
357
358
359
360
361
362
*/
private function parse_blocks() {
$current = $root = new Node('block');
$after = $this->file_content;
$line_count = 0;
while( preg_match('/(.*?)\{([^}]+)}(.*)/s', $after, $matches) ) {
list($before, $brackets_content, $after) = array_slice($matches, 1);
$line_count += substr_count($before, "\n");
// Everything before the new block belongs to its parent
$current->add('html')->set('content', $before);
if( $brackets_content == 'end' ) {
// {end} encountered, go one level up in the tree
if( $current->is_root() )
throw new ParseError($this, 'unexpected {end}', $line_count + 1);
$current = $current->get_parent();
} elseif( substr($brackets_content, 0, 6) == 'block:' ) {
// {block:...} encountered
$block_name = substr($brackets_content, 6);
// Go one level deeper into the tree
$current = $current->add('block')->set('name', $block_name);
} else {
// Variable or something else
$current->add('variable')->set('content', $brackets_content);
}
}
$line_count += substr_count($after, "\n");
if( $current !== $root )
throw new ParseError($this, 'missing {end}', $line_count + 1);
// Add the last remaining content to the root node
$root->add('html')->set('content', $after);
$this->root_block = $root;
}
/**
* Replace blocks and variables in the template's content.
*
* @return string The template's content, with replaced blocks and variables.
*/
function render() {
// Use recursion to parse all blocks from the root level
return self::render_block($this->root_block, $this);
}
/**
* Render a single block, recursively parsing its sub-blocks with a given data scope.
*
* @param Node $block The block to render.
* @param Node $data The data block to search in for the variable values.
* @return string The rendered block.
* @uses replace_variable()
*/
private static function render_block(Node $block, Node $data) {
$html = '';
foreach( $block->get_children() as $child ) {
switch( $child->get_name() ) {
case 'html':
$html .= $child->get('content');
break;
case 'block':
$block_name = $child->get('name');
foreach( $data->find($block_name) as $child_data )
$html .= self::render_block($child, $child_data);
break;
case 'variable':
$html .= self::replace_variable($child->get('content'), $data);
}
}
return $html;
}
/**
* Replace a variable name if it exists within a given data scope.
*
* Applies any of the following macro's:
*
* --------
* Variable
* --------
* <code>{var_name[:func1:func2:...]}</code>
* *var_name* can be of the form *foo.bar*. In this case, *foo* is the
* name of an object or associative array variable. *bar* is a property
* name to get of the object, or the associative index to the array.
* *func1*, *func2*, etc. are helper functions that are executed in the
* same order as listed. The retuen value of each helper function replaces
* the previous variable value.
*
* ------------
* If-statement
* ------------
* <code>{if:condition:success_variable[:else:failure_variable]}</code>
* *condition* is evaluated to a boolean. If it evaluates to TRUE, the
* value of *success_variable* is used. Otherwise, the value of
* *failure_variable* is used (defaults to an empty string if no
* else-statement is specified).
*
* @param string $variable The variable to replace.
* @param Node $data The data block to search in for a value.
* @return string The variable's value if it exists, the original string
* with curly brackets otherwise.
* @throws \UnexpectedValueException If a helper function is not callable.
*/
private static function replace_variable($variable, Node $data) {
// If-(else-)statement
if( preg_match('/^if:([^:]*):(.*?)(?::else:(.*))?$/', $variable, $matches) ) {
$condition = $data->get($matches[1]);
$if = $data->get($matches[2]);
if( $condition )
return $if;
return count($matches) > 3 ? self::replace_variable($matches[3], $data) : '';
}
// Default: variable with optional helper functions
$parts = explode(':', $variable);
$name = $parts[0];
$helper_functions = array_slice($parts, 1);
if( strpos($name, '.') !== false ) {
// Variable of the form 'foo.bar'
list($variable_name, $property) = explode('.', $name, 2);
$object = $data->get($variable_name);
if( is_object($object) && property_exists($object, $property) ) {
// Object property
$value = $object->$property;
} elseif( is_array($object) && isset($object[$property]) ) {
// Associative array index
$value = $object[$property];
}
}
// Default: Simple variable name
if( !isset($value) )
$value = $data->get($name);
// Don't continue if the variable name is not found in the data block
if( $value !== null ) {
// Apply helper functions to the variable's value iteratively
foreach( $helper_functions as $func ) {
if( !is_callable($func) ) {
throw new \UnexpectedValueException(
sprintf('Helper function "%s" is not callable.', $func)
);
}
$value = $func($value);
}
return $value;
}
return '{'.$variable.'}';
}
/**
* Remove all current include paths.
*/
static function clear_include_path() {
self::$include_path = array();
}
/**
* Replace all include paths by a single new one.
*
* @param string $path The new path to set as root.
* @uses clear_include_path()
*/
static function set_root($path) {
self::clear_include_path();
self::add_root($path);
}
/**
* Add a new include path.
*
* @param string $path The path to add.
* @throws FileNotFoundError If the path does not exist.
*/
static function add_root($path) {
if( $path[strlen($path) - 1] != '/' )
$path .= '/';
if( !is_dir($path) )
throw new FileNotFoundError($path, true);
self::$include_path[] = $path;
}
}
/**
* Error, thrown when an error occurs during the parsing of a template file.
*
*/
class ParseError extends \RuntimeException {
/**
* Constructor.
*
* Sets an error message with the path to the template file and a line number.
*
* @param Template $tpl The template in which the error occurred.
* @param string $message A message describing the error.
* @param int $line The line number at which the error occurred.
*/
function __construct(Template $tpl, $message, $line) {
$this->message = sprintf('Parse error in file %s, line %d: %s',
$tpl->get_path(), $line, $message);
}
}
?>