/
Twiggy.php
580 lines (482 loc) · 11.8 KB
/
Twiggy.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Twiggy - Twig template engine implementation for CodeIgniter
*
* Twiggy is not just a simple implementation of Twig template engine
* for CodeIgniter. It supports themes, layouts, templates for regular
* apps and also for apps that use HMVC (module support).
*
* @package CodeIgniter
* @subpackage Twiggy
* @category Libraries
* @author Edmundas Kondrašovas <as@edmundask.lt>
* @license http://www.opensource.org/licenses/MIT
* @version 0.8.4
* @copyright Copyright (c) 2012 Edmundas Kondrašovas <as@edmundask.lt>
*/
if(!defined('TWIGGY_ROOT')) define('TWIGGY_ROOT', dirname(__DIR__));
require_once(TWIGGY_ROOT . '/vendor/Twig/lib/Twig/Autoloader.php');
Twig_Autoloader::register();
class Twiggy
{
private $CI;
private $_config = array();
private $_template_locations = array();
private $_data = array();
private $_globals = array();
private $_themes_base_dir;
private $_theme;
private $_layout;
private $_template;
private $_twig;
private $_twig_loader;
private $_module;
private $_meta = array();
private $_rendered = FALSE;
/**
* Constructor
*/
public function __construct()
{
log_message('debug', 'Twiggy: library initialized');
$this->CI =& get_instance();
$this->_config = $this->CI->config->item('twiggy');
$this->_themes_base_dir = ($this->_config['include_apppath']) ? APPPATH . $this->_config['themes_base_dir'] : $this->_config['themes_base_dir'];
$this->_set_template_locations($this->_config['default_theme']);
try
{
$this->_twig_loader = new Twig_Loader_Filesystem($this->_template_locations);
}
catch(Twig_Error_Loader $e)
{
log_message('error', 'Twiggy: failed to load the default theme');
show_error($e->getRawMessage());
}
// Decide whether to enable Twig cache. If it is set to be enabled, then set the path where cached files will be stored.
$this->_config['environment']['cache'] = ($this->_config['environment']['cache']) ? $this->_config['twig_cache_dir'] : FALSE;
$this->_twig = new Twig_Environment($this->_twig_loader, $this->_config['environment']);
$this->_twig->setLexer(new Twig_Lexer($this->_twig, $this->_config['delimiters']));
// Initialize defaults
$this->theme($this->_config['default_theme'])
->layout($this->_config['default_layout'])
->template($this->_config['default_template']);
// Auto-register functions and filters.
if(count($this->_config['register_functions']) > 0)
{
foreach($this->_config['register_functions'] as $function) $this->register_function($function);
}
if(count($this->_config['register_filters']) > 0)
{
foreach($this->_config['register_filters'] as $filter) $this->register_filter($filter);
}
$this->_globals['title'] = NULL;
$this->_globals['meta'] = NULL;
}
/**
* Set data
*
* @access public
* @param mixed key (variable name) or an array of variable names with values
* @param mixed data
* @param boolean (optional) is this a global variable?
* @return object instance of this class
*/
public function set($key, $value, $global = FALSE)
{
if(is_array($key))
{
foreach($key as $k => $v) $this->set($k, $v, $global);
}
else
{
if($global)
{
$this->_twig->addGlobal($key, $value);
$this->_globals[$key] = $value;
}
else
{
$this->_data[$key] = $value;
}
}
return $this;
}
/**
* Unset a particular variable
*
* @access public
* @param mixed key (variable name)
* @return object instance of this class
*/
public function unset_data($key)
{
if(array_key_exists($key, $this->_data)) unset($this->_data[$key]);
return $this;
}
/**
* Set title
*
* @access public
* @param string
* @return object instance of this class
*/
public function title()
{
if(func_num_args() > 0)
{
$args = func_get_args();
// If at least one parameter is passed in to this method,
// call append() to either set the title or append additional
// string data to it.
call_user_func_array(array($this, 'append'), $args);
}
return $this;
}
/**
* Append string to the title
*
* @access public
* @param string
* @return object instance of this class
*/
public function append()
{
$args = func_get_args();
$title = implode($this->_config['title_separator'], $args);
if(empty($this->_globals['title']))
{
$this->set('title', $title, TRUE);
}
else
{
$this->set('title', $this->_globals['title'] . $this->_config['title_separator'] . $title, TRUE);
}
return $this;
}
/**
* Prepend string to the title
*
* @access public
* @param string
* @return object instance of this class
*/
public function prepend()
{
$args = func_get_args();
$title = implode($this->_config['title_separator'], $args);
if(empty($this->_globals['title']))
{
$this->set('title', $title, TRUE);
}
else
{
$this->set('title', $title . $this->_config['title_separator'] . $this->_globals['title'], TRUE);
}
return $this;
}
/**
* Set title separator
*
* @access public
* @param string separator
* @return object instance of this class
*/
public function set_title_separator($separator = ' | ')
{
$this->_config['title_separator'] = $separator;
return $this;
}
/**
* Set meta data
*
* @access public
* @param string name
* @param string value
* @param string (optional) name of the meta tag attribute
* @return object instance of this class
*/
public function meta($name, $value, $attribute = 'name')
{
$this->_meta[$name] = array('name' => $name, 'value' => $value, 'attribute' => $attribute);
return $this;
}
/**
* Unset meta data
*
* @access public
* @param string (optional) name of the meta tag
* @return object instance of this class
*/
public function unset_meta()
{
if(func_num_args() > 0)
{
$args = func_get_args();
foreach($args as $arg)
{
if(array_key_exists($arg, $this->_meta)) unset($this->_meta[$arg]);
}
}
else
{
$this->_meta = array();
}
return $this;
}
/**
* Register a function in Twig environment
*
* @access public
* @param string the name of an existing function
* @return object instance of this class
*/
public function register_function($name)
{
$this->_twig->addFunction($name, new Twig_Function_Function($name));
return $this;
}
/**
* Register a filter in Twig environment
*
* @access public
* @param string the name of an existing function
* @return object instance of this class
*/
public function register_filter($name)
{
$this->_twig->addFilter($name, new Twig_Filter_Function($name));
return $this;
}
/**
* Load theme
*
* @access public
* @param string name of theme to load
* @return object instance of this class
*/
public function theme($theme)
{
if(!is_dir(realpath($this->_themes_base_dir. $theme)))
{
log_message('error', 'Twiggy: requested theme '. $theme .' has not been loaded because it does not exist.');
show_error("Theme does not exist in {$this->_themes_base_dir}{$theme}.");
}
$this->_theme = $theme;
$this->_set_template_locations($theme);
return $this;
}
/**
* Set layout
*
* @access public
* @param string name of the layout
* @return object instance of this class
*/
public function layout($name)
{
$this->_layout = $name;
$this->_twig->addGlobal('_layout', '_layouts/'. $this->_layout . $this->_config['template_file_ext']);
return $this;
}
/**
* Set template
*
* @access public
* @param string name of the template file
* @return object instance of this class
*/
public function template($name)
{
$this->_template = $name;
return $this;
}
/**
* Compile meta data into pure HTML
*
* @access private
* @return string HTML
*/
private function _compile_metadata()
{
$html = '';
foreach($this->_meta as $meta)
{
$html .= $this->_meta_to_html($meta);
}
return $html;
}
/**
* Convert meta tag array to HTML code
*
* @access private
* @param array meta tag
* @return string HTML code
*/
private function _meta_to_html($meta)
{
return "<meta " . $meta['attribute'] . "=\"" . $meta['name'] . "\" content=\"" . $meta['value'] . "\">\n";
}
/**
* Load template and return output object
*
* @access private
* @return object output
*/
private function _load()
{
$this->_globals['meta'] = $this->_compile_metadata();
$this->_rendered = TRUE;
return $this->_twig->loadTemplate($this->_template . $this->_config['template_file_ext']);
}
/**
* Render and return compiled HTML
*
* @access public
* @param string (optional) template file
* @return string compiled HTML
*/
public function render($template = '')
{
if(!empty($template)) $this->template($template);
try
{
return $this->_load()->render($this->_data);
}
catch(Twig_Error_Loader $e)
{
show_error($e->getRawMessage());
}
}
/**
* Display the compiled HTML content
*
* @access public
* @param string (optional) template file
* @return void
*/
public function display($template = '')
{
if(!empty($template)) $this->template($template);
try
{
$this->_load()->display($this->_data);
}
catch(Twig_Error_Loader $e)
{
show_error($e->getRawMessage());
}
}
/**
* Set template locations
*
* @access private
* @param string name of theme to load
* @return void
*/
private function _set_template_locations($theme)
{
// Reset template locations array since we loaded a different theme
//$this->_template_locations = array();
// Check if HMVC is installed.
// NOTE: there may be a simplier way to check it but this seems good enough.
if(method_exists($this->CI->router, 'fetch_module'))
{
$this->_module = $this->CI->router->fetch_module();
// Only if the current page is served from a module do we need to add extra template locations.
if(!empty($this->_module))
{
$module_locations = Modules::$locations;
foreach($module_locations as $loc => $offset)
{
$this->_template_locations[] = $loc . $this->_module . '/' . $this->_config['themes_base_dir'] . $theme;
}
}
}
$this->_template_locations[] = $this->_themes_base_dir . $theme;
// Reset the paths if needed.
if(is_object($this->_twig_loader))
{
$this->_twig_loader->setPaths($this->_template_locations);
}
}
/**
* Get current theme
*
* @access public
* @return string name of the currently loaded theme
*/
public function get_theme()
{
return $this->_theme;
}
/**
* Get current layout
*
* @access public
* @return string name of the currently used layout
*/
public function get_layout()
{
return $this->_layout;
}
/**
* Get template
*
* @access public
* @return string name of the loaded template file (without the extension)
*/
public function get_template()
{
return $this->_template;
}
/**
* Get metadata
*
* @access public
* @param string (optional) name of the meta tag
* @param boolean whether to compile to html
* @return mixed array of tag(s), string (HTML) or FALSE
*/
public function get_meta($name = '', $compile = FALSE)
{
if(empty($name))
{
return ($compile) ? $this->_compile_metadata() : $this->_meta;
}
else
{
if(array_key_exists($name, $this->_meta))
{
return ($compile) ? $this->_meta_to_html($this->_meta[$name]) : $this->_meta[$name];
}
return FALSE;
}
}
/**
* Check if template is already rendered
*
* @access public
* @return boolean
*/
public function rendered()
{
return $this->_rendered;
}
/**
* Magic method __get()
*/
public function __get($variable)
{
if($variable == 'twig') return $this->_twig;
if(array_key_exists($variable, $this->_globals))
{
return $this->_globals[$variable];
}
elseif(array_key_exists($variable, $this->_data))
{
return $this->_data[$variable];
}
return FALSE;
}
}
// End Class