-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemplateEngineJade.module
232 lines (197 loc) · 6.78 KB
/
TemplateEngineJade.module
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
<?php
/**
* TemplateEngineJade
*
* @author Diktus Dreibholz <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
* @version 1.0.4
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
require_once(dirname(dirname(__FILE__)) . '/TemplateEngineFactory/TemplateEngine.php');
require_once(dirname(__FILE__) . '/jade-php-custom.php');
class TemplateEngineJade extends TemplateEngine implements Module, ConfigurableModule
{
const COMPILE_DIR = 'TemplateEngineJade_compile/';
/**
* @var Jade_Environment
*/
protected $jade;
/**
* Stores variables and values set with TemplateEngineJade::set(). Passed to Jade when rendering the template.
*
* @var array
*/
protected $variables = array();
/**
* @param string $filename
*/
public function __construct($filename = '')
{
parent::__construct($filename);
}
/**
* Setup Jade
*/
public function initEngine()
{
$cachePath = $this->wire('config')->paths->assets . 'cache/' . self::COMPILE_DIR;
$options =
$this->jade = new Jade\CustomJade(array(
'cache' => ($this->getConfig('use_cache') ? $cachePath : null),
'extension' => '.' . $this->getConfig('template_files_suffix'),
'prettyprint' => $this->getConfig('prettyprint'),
'stream' => '.' . $this->getConfig('stream'),
'unescaped' => $this->getConfig('unescaped'),
'phpSingleLine' => true,
'keepBaseName' => true
));
if ($this->getConfig('api_vars_available')) {
foreach (Wire::getFuel() as $name => $object) {
if ($name == $this->factory->get('api_var')) continue;
$this->variables[$name] = $object;
}
}
$this->initJade($this->jade);
}
/**
* @return array
*/
public static function getDefaultConfig()
{
$config = parent::getDefaultConfig();
return array_merge($config, array(
'template_files_suffix' => 'jade',
'prettyprint' => 0,
'stream' => 'jade.stream',
'unescaped' => 0,
'api_vars_available' => 1
));
}
/**
* Set a key/value pair to the template
*
* @param $key
* @param $value
*/
public function set($key, $value)
{
$this->variables[$key] = $value;
}
/**
* Render markup from template file
*
* @throws WireException
* @return mixed
*/
public function render()
{
try {
$path = $this->getTemplatesPath() . $this->getFilename();
return $this->jade->render($path, $this->variables);
} catch (Exception $e) {
throw new WireException($e->getMessage());
}
}
/**
* Clear all variables passed
*/
public function clearVariables()
{
$this->variables = array();
}
/**
* Clear cache when uninstalling
*/
public function uninstall()
{
parent::uninstall();
wireRmdir($this->wire('config')->paths->assets . 'cache/' . self::COMPILE_DIR, true);
}
/**
* Hookable method called after jade instance is created.
* Allows for customizing Jade, e.g. add filters
*
* @param Jade_Environment $jade
*/
protected function ___initJade(Jade\Jade $jade) {}
/**
* Methods per interfaces Module, ConfigurableModule
*
*/
/**
* @return array
*/
public static function getModuleInfo()
{
return array(
'title' => 'Template Engine Jade',
'summary' => 'Jade templates for the TemplateEngineFactory',
'version' => 104,
'author' => 'Diktus Dreibholz (dreerr)',
'href' => 'https://processwire.com/talk/topic/11386-module-jade-for-the-templateenginefactory/',
'singular' => false,
'autoload' => false,
'requires' => array('TemplateEngineFactory'),
);
}
/**
* Return an InputfieldWrapper of Inputfields used to configure the class
*
* @param array $data Array of config values indexed by field name
* @return InputfieldWrapper
*
*/
public static function getModuleConfigInputfields(array $data)
{
/** @var Modules $modules */
$data = array_merge(self::getDefaultConfig(), $data);
$wrapper = parent::getModuleConfigInputfields($data);
$modules = wire('modules');
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->label = __('Import ProcessWire API variables in Jade templates');
$f->description = __('All API variables (page, input, config etc.) are accessible in Jade, e.g. page for $page');
$f->name = 'api_vars_available';
if ($data['api_vars_available']) $f->checked = 1;
$wrapper->append($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->label = __('Disable Escaping');
$f->description = __('Used variables will not be escaped');
$f->notes = __('By default *h1=$var* is escaped through *htmlspecialchars* and *h1!=$var* is not escaped. If you disable escaping *h1=$var* will behave like *h1!=$var*');
$f->name = 'unescaped';
if ($data['unescaped']) $f->checked = 1;
$wrapper->append($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->label = __('Use Cache');
$f->description = __('Templates are cached and will be recompiled only when the source code changes.');
$f->name = 'use_cache';
if ($data['use_cache']) $f->checked = 1;
$wrapper->append($f);
/** @var InputfieldCheckbox $f */
$f = $modules->get('InputfieldCheckbox');
$f->label = __('Output indented HTML');
$f->description = __('Output rendered templates as indented HTML');
$f->name = 'prettyprint';
if ($data['prettyprint']) $f->checked = 1;
$wrapper->append($f);
/** @var InputfieldText $f */
$f = $modules->get('InputfieldText');
$f->label = __('Stream Wrapper Protocol');
$f->description = __('Enter the name of the protocol under which `Jade\Stream\Template` class is [registered](http://php.net/manual/function.stream-wrapper-register.php)');
$f->notes = __('You might want to change this if [Suhosin](https://suhosin.org/) is enabled and *suhosin.executor.include.whitelist* is set');
$f->name = 'stream';
$f->value = $data['stream'];
$f->collapsed = 1;
$f->required = 1;
$wrapper->append($f);
return $wrapper;
}
}