forked from wanze/TemplateEngineTwig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplateEngineTwig.module
executable file
·188 lines (155 loc) · 5.15 KB
/
TemplateEngineTwig.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
<?php
require_once(dirname(dirname(__FILE__)) . '/TemplateEngineFactory/TemplateEngine.php');
if (!class_exists('Twig_Compiler')) {
require_once (/*NoCompile*/__DIR__ . '/vendor/autoload.php');
}
/**
* TemplateEngineTwig
*
* @author Stefan Wanzenried <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
* @version 1.1.0
*/
class TemplateEngineTwig extends TemplateEngine implements Module, ConfigurableModule
{
const COMPILE_DIR = 'TemplateEngineTwig_compile/';
/**
* @var Twig_Environment
*/
protected $twig;
/**
* Stores variables and values set with TemplateEngineTwig::set(). Passed to Twig when rendering the template.
*
* @var array
*/
protected $variables = array();
/**
* @inheritdoc
*/
public function initEngine()
{
$loader = new Twig_Loader_Filesystem($this->getTemplatesPath());
$this->twig = new Twig_Environment($loader, array(
'cache' => $this->wire('config')->paths->assets . 'cache/' . self::COMPILE_DIR,
'debug' => $this->wire('config')->debug,
'auto_reload' => $this->getConfig('auto_reload') == 1,
'autoescape' => $this->getConfig('auto_escape') == 1,
));
if ($this->wire('config')->debug) {
$this->twig->addExtension(new Twig_Extension_Debug());
}
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->initTwig($this->twig);
}
/**
* @inheritdoc
*/
public static function getDefaultConfig()
{
$config = parent::getDefaultConfig();
return array_merge($config, array(
'template_files_suffix' => 'html.twig',
'api_vars_available' => 1,
'auto_reload' => 1,
'auto_escape' => 0,
));
}
/**
* @inheritdoc
*/
public function set($key, $value)
{
$this->variables[$key] = $value;
}
/**
* @inheritdoc
*/
public function render()
{
try {
$template = $this->twig->loadTemplate($this->getFilename());
return $template->render($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 twig instance is created.
* Allows for customizing Twig, e.g. add extensions
*
* @param Twig_Environment $twig
*/
protected function ___initTwig(Twig_Environment $twig)
{
}
/**
* Methods per interfaces Module, ConfigurableModule
*
*/
/**
* @return array
*/
public static function getModuleInfo()
{
return array(
'title' => 'Template Engine Twig',
'summary' => 'Twig templates for the TemplateEngineFactory',
'version' => 110,
'author' => 'Stefan Wanzenried (Wanze)',
'href' => 'https://processwire.com/talk/topic/6835-module-twig-for-the-templateenginefactory/',
'singular' => false,
'autoload' => false,
'requires' => array('TemplateEngineFactory'),
);
}
/**
* @inheritdoc
*/
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 Twig templates');
$f->description = __('All API variables (page, input, config etc.) are accessible in Twig, e.g. {{ page }} for $page');
$f->name = 'api_vars_available';
if ($data['api_vars_available']) $f->checked = 1;
$wrapper->append($f);
$f = $modules->get('InputfieldCheckbox');
$f->label = __("Auto reload templates (recompile)");
$f->description = __('If enabled, templates are recompiled whenever the source code changes');
$f->name = 'auto_reload';
if ($data['auto_reload']) $f->checked = 1;
$wrapper->append($f);
$f = $modules->get('InputfieldCheckbox');
$f->label = __("Auto escape variables");
$f->description = __("If enabled, templates will auto-escape variables. If you use ProcessWire's textformatter to escape variables, do not enable this feature.");
$f->name = 'auto_escape';
if ($data['auto_escape']) $f->checked = 1;
$wrapper->append($f);
return $wrapper;
}
}