forked from wanze/TemplateEngineFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateEngine.php
More file actions
240 lines (198 loc) · 5.64 KB
/
TemplateEngine.php
File metadata and controls
240 lines (198 loc) · 5.64 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
<?php
/**
* TemplateEngine
*
* Base class for all implemented template engines.
*
* @author Stefan Wanzenried <stefan.wanzenried@gmail.com>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
abstract class TemplateEngine extends Wire
{
/**
* Stores module configuration per implemented TemplateEngine
*
* @var array
*/
protected static $loaded_config = array();
/**
* Filename of template file
*
* @var string
*/
protected $filename = '';
/**
* Instance to the TemplateEngineFactory module
*
* @var TemplateEngineFactory
*/
protected $factory;
/**
* @param string $filename Filename of template file for this instance
*/
public function __construct($filename = '')
{
$this->initConfig(); // Want to have config available as early as possible
$this->setFilename($filename);
$this->factory = $this->wire('modules')->get('TemplateEngineFactory'); // Module is singular === singleton
}
/**
* Register the template engine when installing
*
*/
public function install()
{
$this->factory->registerEngine($this);
}
/**
* Unregister template engine when uninstalling
*
*/
public function uninstall()
{
$this->factory->unregisterEngine($this);
}
public function init() {}
/**
* Init engine, derived classes must use this method to setup the engine
*
*/
abstract public function initEngine();
/**
* @param $key
* @param $value
*/
public function __set($key, $value)
{
$this->set($key, $value);
}
/**
* Set a key/value pair to the template engine
*
* @param $key
* @param $value
*/
abstract public function set($key, $value);
/**
* Render markup from template engine
*
* @return mixed
*/
abstract public function render();
/**
* @return array
*/
public static function getDefaultConfig()
{
return array(
'templates_path' => 'templates/views/', // Relative to /site/ directory
'global_template' => '',
'template_files_suffix' => 'tpl',
);
}
/**
* ProcessWire does call this method and set config values from database
* In our context, the config is loaded and available already in the constructor so just leave empty
*
*/
public function setConfigData(array $data = array()) {}
/**
* Return config all implemented engines share in common
*
* @param array $data
* @return InputfieldWrapper
*/
public static function getModuleConfigInputfields(array $data)
{
$wrapper = new InputfieldWrapper();
$modules = wire('modules');
$f = $modules->get('InputfieldText');
$f->name = 'templates_path';
$f->label = __('Path to templates');
$f->description = __('Relative path from the site directory where template files are stored. E.g. "templates/views/" resolves to "/site/templates/views/"');
$f->value = $data['templates_path'];
$f->required = 1;
$wrapper->append($f);
$f = $modules->get('InputfieldText');
$f->label = __('Template files suffix');
$f->description = __('File extension of template files');
$f->name = 'template_files_suffix';
$f->value = $data['template_files_suffix'];
$f->required = 1;
$wrapper->append($f);
$f = $modules->get('InputfieldText');
$f->name = 'global_template';
$f->label = __('Global template file');
$f->description = __('Filename of a template file that is used as main template behind the API variable');
$f->value = $data['global_template'];
$wrapper->append($f);
return $wrapper;
}
/**
* Get a config value
*
* @param $key
* @return string|null
*/
public function getConfig($key)
{
return (isset(self::$loaded_config[$this->className][$key])) ? self::$loaded_config[$this->className][$key] : null;
}
/**
* Set a config value (runtime only and for all instances of the derived TemplateEngine class)
*
* @param $key
* @param $value
*/
public function setConfig($key, $value)
{
self::$loaded_config[$this->className][$key] = $value;
}
/**
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* @param string $filename
*/
public function setFilename($filename)
{
$suffix = $this->getConfig('template_files_suffix');
if (preg_match("/\.{$suffix}$/", $filename)) {
$this->filename = $filename;
} else {
$this->filename = $filename . '.' . $suffix;
}
}
/**
* Get the path where templates are stored
*
* @return string
*/
public function getTemplatesPath()
{
$path = ltrim($this->getConfig('templates_path'), '/');
return $this->config->paths->root . 'site/' . rtrim($path, '/') . '/';
}
/**
* Load configuration once for all instances of TemplateEngine
*
*/
protected function initConfig()
{
if (!isset(self::$loaded_config[$this->className])) {
$configs = $this->wire('modules')->getModuleConfigData($this);
self::$loaded_config[$this->className] = array_merge($this->getDefaultConfig(), $configs);
}
}
}