-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTemplate.php
More file actions
executable file
·350 lines (293 loc) · 10.7 KB
/
Template.php
File metadata and controls
executable file
·350 lines (293 loc) · 10.7 KB
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
<?php
/**
* Represents a PHP Template file
*
* @author Jorgen Evens <jorgen@wlab.be>
* @package WebLab
*
*/
class WebLab_Template
{
/**
*
* @var stdClass Contains config for templates.
*/
protected static $_config;
/**
* Contains the root template, if set.
* @var WebLab_Template
*/
protected static $_root;
/**
* Lazy load config.
*/
protected static function _getConfig() {
if( empty( self::$_config ) ) {
self::$_config = WebLab_Config::getApplicationConfig()->get( 'Application.Templates', WebLab_Config::OBJECT, false );
}
return self::$_config;
}
/**
* Set the root template for current request.
* @param WebLab_Template $template
*/
public static function setRootTemplate( WebLab_Template $template ) {
self::$_root = $template;
}
/**
* Get the root template for current request.
*/
public static function &getRootTemplate() {
return self::$_root;
}
/**
* @var int The maximum age in seconds of a cached template.
*/
public $max_cache_age = 0;
/**
*
* @var String Contains the path to the template file.
*/
protected $_template;
/**
*
* @var Array All variables assigned to this template.
*/
protected $_variables;
/**
*
* @var String The directory in which to search for the templates.
*/
protected $_dir;
/**
* @var String The directory of a specific theme. Defaults to 'source'.
*/
protected $_theme = null;
/**
* @var string Determines if logic should be called.
*/
protected $_setup = false;
/**
* Constructs a new Template
* @param String $template The path to the template, relative to the configured template directory.
* @param String $theme The name of the theme to be used. default theme is 'source'
* @param String $directory The directory in which to look for the template. Defaults to configuration setting.
*/
public function __construct( $template, $theme=null, $directory=null ) {
$config = self::_getConfig();
if( !empty( $directory ) && is_string( $directory ) ) {
$this->setTemplateDir( $directory );
} elseif( is_string( $config->directory ) ) {
$this->setTemplateDir( $config->directory );
}
if( $theme === null && !empty( $config->theme ) && is_string( $config->theme ) ) {
$this->setTheme( $config->theme );
}
if( is_string( $theme ) ) {
$this->setTheme( $theme );
}
//$variables['variables'] = array();
$this->_variables = array();
if( !is_string( $template ) ) {
throw new WebLab_Exception_Template( 'Template name shoud refer to the path within the theme.' );
}
$this->_template = $template;
$this->_init();
$this->setBasicVariables();
}
/**
* Set some default settings for a template.
*/
protected function setBasicVariables() {}
/**
* Attaches a template as a variable to this template.
* @param WebLab_Template $template Template you would like to attach to this template.
* @param String $moduleName Variable name for $template.
* @return WebLab_Template This template instance.
* @deprecated Use WebLab_Template::set now.
*/
public function attach( WebLab_Template &$template, $moduleName ) {
$this->_variables[ $moduleName ] = &$template;
return $this;
}
/**
* Set a template variable value.
*
* @param string $variable
* @param mixed $value
* @return WebLab_Template
*/
public function set( $variable, $value ) {
$this->_variables[ $variable ] = $value;
return $this;
}
/**
* Get a template variable.
*
* @param string $variable
* @return mixed:
*/
public function get( $variable ) {
if( isset( $this->_variables[ $variable ] ) ) {
return $this->_variables[ $variable ];
}
return null;
}
/**
* Set the theme folder within the template directory.
* @param String $dir Foldername of the theme.
* @return WebLab_Template This template instance.
*/
public function setTheme( $dir ) {
$this->_theme = $dir;
return $this;
}
/**
* Set the location of the template directory.
* This directory contains all of the themes.
* @param String $dir The directory to look for templates and themes.
*/
public function setTemplateDir( $dir ) {
$this->_dir = $dir;
return $this;
}
/**
* Get the entire stack of variables currently attached to this template.
* @return Array Return an array of attached variables. VariableNames as keys.
*/
public function getVars() {
return $this->_variables;
}
/**
* Catches all the property calls to set the appropriate variable.
* @param String $name The name of the property (variable) being set.
* @param Object &$value The value to be set.
* @return WebLab_Template This template instance.
*/
public function __set( $name, $value ) {
$this->_variables[ $name ] = &$value;
return $this;
}
/**
* Catches all the property calls to return the appropriate variable.
* @param String $name The name of the property (variable) te get.
* @return Object The value of this property.
*/
public function &__get( $name ) {
return $this->_variables[ $name ];
}
/**
* Renders the template to its compiled HTML form.
* @param bool $show Whether to send the output to the browser requesting it. Defaults to false.
* @return mixed The HTML code rendered or true if $show==true
*/
public function render( $flush=true ) {
if( !$this->_setup ) {
$this->_setup = true;
$this->_load( 'setup' );
}
if( $flush ) {
$this->_load();
flush();
return true;
}
ob_start();
$this->_load();
return ob_get_clean();
}
/**
* Include a component file for current template
* $component will be attached behind the filename, including a suffix .php
*
* @param string $component
* @param array $variables
*/
protected function _load( $component='', $extract=true ) {
if( !empty( $component ) ) {
$component = '.' . $component;
}
$namespace = explode( NAMESPACE_SEPARATOR, $this->_template );
$name = array_pop( $namespace );
$namespace = implode( DIRECTORY_SEPARATOR, $namespace );
$theme = $this->_theme;
if( !empty( $theme ) )
$theme .= '/';
$path = $this->_dir . '/' . $theme . $name . $component . '.php';
if( isset( self::_getConfig()->extract_vars ) && self::_getConfig()->extract_vars && $extract ) {
extract( $this->_variables );
}
// Load the template
$error_report = error_reporting();
error_reporting( $error_report & ( E_ALL ^ E_WARNING ) );
$exists = !!include( $path );
if( !$exists && !empty( $namespace ) ) {
$exists = !!include( $namespace . DIRECTORY_SEPARATOR . $path );
}
error_reporting( $error_report );
if( $exists === false && empty( $component ) ) {
throw new WebLab_Exception_Template( 'Template (' . $this->_template . ') not found!' );
}
}
/**
* Loads the init component of a template.
* Injecting headers, scripts or styles should be done here.
*/
protected function _init() {
$this->_load( 'init', false );
}
/**
* Retrieve cached template
* @return string|NULL
*/
protected function _getCache( $path ) {
if( file_exists( $path ) && ( time() - filemtime( $path ) ) < $this->max_cache_age ) {
return file_get_contents( $path, true );
}
return null;
}
/**
* Put cached code
* @param string $code
*/
protected function _putCache( $path, $code ) {
$folder = dirname( $path );
if( !is_dir( $folder ) ) {
mkdir( $folder, 0755, true );
}
if( !empty( $this->max_cache_age ) ) {
file_put_contents( $path, $code );
}
}
/**
* Returns the value of the function render.
* @return String Returns HTML that has been rendered by the render function. If a Exception occurs the message of this exception will be returned.
*/
final public function __toString() {
try {
/*
* Hot path for uncached templates.
*/
if( empty( $this->max_cache_age ) ) {
return $this->render( false );
} else {
/*
* Slower path, cache lookup
*/
$cache_dir = $path = WebLab_Config::getApplicationConfig()->get( 'Application.Templates.cache_dir', WebLab_Config::RAW );
$theme_dir = empty( $this->_theme ) ? '/' : '/' . $this->_theme . '/';
$param = md5( serialize( $this->_variables ) );
$cache_file = $theme_dir . $this->_template . $param . '.php';
$cache_file = $_SERVER['DOCUMENT_ROOT'] . BASE . $cache_dir . '/' . $theme_dir . md5( $cache_file );
$cache = $this->_getCache( $cache_file );
if( !empty( $cache ) ) {
return $cache;
}
$code = $this->render( false );
$this->_putCache( $cache_file, $code );
return $code;
}
} catch( Exception $ex ) {
return $ex->getMessage();
}
}
}