-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtemplater.php
executable file
·308 lines (246 loc) · 6.84 KB
/
templater.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
<?php
class TPLTemplateNotExistsException extends Exception {}
class TPLLayoutNotExistsException extends Exception {}
/*
Class:
<Templater>
A class to help separate logic from presentation.
*/
class Templater
{
// Property: <Templater::$vars>
// The variabels that will be available to templates.
protected $vars = array();
// Property: <Templater::$dir>
// The directory in which templates reside.
protected $dir = 'templates/';
// Property: <Templater::$template>
// The name of the template that this instance will use.
protected $template;
// Property: <Templater::$type>
// The response type of the template. Used for <Templater::$layout>.
protected $type;
// Property: <Templater::$layout>
// The layout template in which the template will be included by. Used for different response types, HTML, JSON, XML, etc.
protected $layout;
// Property: <Templater::$cache>
// Holds an instance of <Cache>, if the output is cached.
protected $cache;
// Property: <Templater::$cache_path>
// Holds the values that will be passed to <Cache> when cacheing is activated.
protected $cache_path;
// Property: <Templater::$cache_active>
// True if output should use cached version
protected $cache_active = false;
// Property: <Templater::$base_dir>
// Base directory for all templates. See <Templater::$layout_dir> for the layouts.
protected static $base_dir = 'templates/';
// Property: <Templater::$layout_dir>
// Path to where all layout files are.
protected static $layout_dir = 'templates/layouts/';
/*
Method:
<Templater::add>
Add information that will be available to the presentational layer.
Parameters:
mixed $key - If $key is an array it will be merged into the <Templater::$vars>-array, else $key is the name of the variable assosciated with the $value inside the template.
mixed $value - The value.
Returns:
Returns the $this-instance for chaining commands.
*/
public function add($key, $value = null)
{
if ( is_array($key) )
{
$this->vars = array_merge($this->vars, $key);
}
else
{
$this->vars[$key] = $value;
}
return $this;
}
/*
Method:
<Templater::render>
Render a file from the <Templater::$dir>, and if cacheing is enabled and the cache is not outdated output the contents of the cache.
The passed $filename will be rendered inside the defined layout. (<Templater::$layout>).
Parameters:
string $filename - The file, contained in <Templater::$dir>, to render.
*/
public function render($filename)
{
if ( ! $this->exists($filename) )
{
throw new TPLTemplateNotExistsException($this->dir . $filename);
}
$this->template = $this->dir . $filename;
// If cacheing is enabled and not outdated use that copy
// Else start output buffering so we can catch outputed contents.
if ( $this->cache_active )
{
if ( ! $this->isOutDated() )
{
echo $this->cache->get();
return;
}
else
{
ob_start();
}
}
// Magic
extract($this->vars);
include($this->layout);
// Update cache with outputed contents
if ( $this->cache_active )
{
$contents = ob_get_contents();
$this->cache->update($contents);
}
}
/*
Method:
<Templater::renderError>
Render an error page.
*/
public function renderError($error_code)
{
$error_name = sprintf("error.%s.php", $error_code);
$generic_name = "error.500.php";
if ( file_exists(self::$base_dir . $error_name) )
$this->template = self::$base_dir . $error_name;
elseif ( file_exists(self::$base_dir . $generic_name) )
$this->template = self::$base_dir . $generic_name;
if ( $this->template )
{
extract($this->vars);
include($this->layout);
}
else
{
printf('<!DOCTYPE><title>%s - Error</title><h1>%s - error</h1><p>Something went wrong</p>', $error_code, $error_code);
}
}
/*
Method:
<Templater::isOutDated>
If the cache needs updating.
Returns:
True if the cache is outdated.
*/
public function isOutDated()
{
return ( ! $this->cache_active ) ? true : $this->cache->isOutDated();
}
/*
Method:
<Templater::exists>
Checks wether a template exists inside <Templater::$dir>
Parameters:
string $filename - The file to be checked.
*/
public function exists($filename)
{
return file_exists($this->dir . $filename);
}
/*
Method:
<Templater::setType>
Set the type of layout to be used, HTML, JSON, etc. Will throw a TPLLayoutNotExistsException if the layout did not exist.
Parameters:
string $type - The type of layout. A corresponding layout.type.php file must exist in <Templater::$dir>
*/
public function setType($type)
{
$name = self::$layout_dir . 'layout.' . $type . '.php';
if ( ! file_exists($name) )
{
throw new TPLLayoutNotExistsException($name);
}
$this->layout = $name;
$this->type = $type;
}
/*
Method:
<Templater::getType>
Returns:
The current response type, <Templater::$type>
*/
public function getType()
{
return $this->type;
}
/*
Method:
<Templater::setCache>
Set values to <Templater::$cache> that will be used as paramaters if cacheing is enabled.
Parameters:
string $path - The path for <Cache::__construct>.
int $time - (optiona) The amount of time, in seconds, to cache a template.
*/
public function setCachePath($path, $time = 600)
{
$this->cache_path = array($path, $time);
}
/*
Method:
<Templater::activeCache>
Activate cacheing of output. Create the instance of <Cache> that will be used for output, using the <Templater::$cache_path> as array path.
*/
public function activateCache()
{
$this->cache_active = true;
$this->cache = new Cache($this->cache_path[0], $this->cache_path[1]);
}
/*
Method:
<Templater::toJSON>
Method for converting members to JSON-data. Because json_encode does not properly encode <DomainObject>s.
Parameters:
$data - The array to convert, will recursively convert data.
Returns:
The JSON-data
*/
public function toJSON($data)
{
foreach ( $data as $key => $value )
{
if ( $value instanceof DomainCollection )
{
$data[$key] = $value->getPublicData();
}
else if ( $value instanceof DomainObject )
{
$data[$key] = $value->getPublicData();
}
else if ( is_array($value) )
{
$data[$key] = $this->toJSON($value);
}
else if ( is_object($value) && method_exists($value, 'toJSON') )
{
$data[$key] = $value->toJSON();
}
}
return $data;
}
// Method: <Templater::setDir>
// Set the directory in which templates are contained.
public function setDir($dir)
{
$this->dir = $dir;
}
// Method: <Templater::setBaseDir>
// Set the <Templater::$base_dir>.
public static function setBaseDir($dir)
{
self::$base_dir = $dir;
}
// Method: <Templater::setLayoutDir>
// Set the <Templater::$layout_dir>.
public static function setLayoutDir($dir)
{
self::$layout_dir = $dir;
}
}