-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTemplateEngineLatte.module.php
executable file
·154 lines (135 loc) · 5.8 KB
/
TemplateEngineLatte.module.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
<?php
namespace ProcessWire;
use TemplateEngineLatte\TemplateEngineLatte as LatteEngine;
/**
* Adds Latte templates to the TemplateEngineFactory module.
*/
class TemplateEngineLatte extends WireData implements Module, ConfigurableModule
{
/**
* @var array
*/
private static $defaultConfig = [
'template_files_suffix' => 'latte',
'default_layout' => '',
'api_vars_available' => 1,
'api_vars_overwrites' => 0,
'simple_path_resolution' => 0,
'auto_refresh' => 1,
];
public function __construct()
{
parent::__construct();
$this->wire('classLoader')->addNamespace('TemplateEngineLatte', __DIR__ . '/src');
$this->setDefaultConfig();
}
/**
* @return array
*/
public static function getModuleInfo()
{
return [
'title' => 'Template Engine Latte',
'summary' => 'Latte templates for the TemplateEngineFactory',
'version' => '2.1.0',
'author' => 'Philipp Daun',
'href' => 'https://github.com/daun/TemplateEngineLatte',
'singular' => true,
'autoload' => true,
'requires' => [
'TemplateEngineFactory>=2.0.0',
'PHP>=8.0',
'ProcessWire>=3.0',
],
];
}
public function init()
{
/** @var \ProcessWire\TemplateEngineFactory $factory */
$factory = $this->wire('modules')->get('TemplateEngineFactory');
$factory->registerEngine('Latte', new LatteEngine($factory->getArray(), $this->getArray()));
}
private function setDefaultConfig()
{
foreach (self::$defaultConfig as $key => $value) {
$this->set($key, $value);
}
}
/**
* @param array $data
*
* @throws \ProcessWire\WireException
* @throws \ProcessWire\WirePermissionException
*
* @return \ProcessWire\InputfieldWrapper
*/
public static function getModuleConfigInputfields(array $data)
{
/** Clear Latte temp dirctory? */
$post = wire('input')->post;
if ($post->submit_save_module && $post->clear_temp) {
if (LatteEngine::clearLatteTempDir()) {
wire()->message(__CLASS__ . ': ' . __(' Latte cache cleared.', __FILE__));
}
}
/** @var Modules $modules */
$data = array_merge(self::$defaultConfig, $data);
$wrapper = new InputfieldWrapper();
$modules = wire('modules');
/** @var \ProcessWire\InputfieldText $field */
$field = $modules->get('InputfieldText');
$field->label = __('Template files suffix');
$field->name = 'template_files_suffix';
$field->value = $data['template_files_suffix'];
$field->required = 1;
$wrapper->append($field);
/** @var \ProcessWire\InputfieldText $field */
$field = $modules->get('InputfieldText');
$field->label = __('Default layout');
$field->description = __('The base layout file that views will use, relative to the views directory.');
$field->note = __('Example: `layouts/@default.latte` will set `site/templates/views/layouts/@default.latte`');
$field->name = 'default_layout';
$field->value = $data['default_layout'];
$wrapper->append($field);
$field = $modules->get('InputfieldCheckbox');
$field->label = __('Provide ProcessWire API variables in Latte templates');
$field->description = __('API variables (`$pages`, `$input`, `$config`...) are accessible in Latte, e.g. `{$config}` for the config API variable.');
$field->name = 'api_vars_available';
$field->checked = (bool) $data['api_vars_available'];
$wrapper->append($field);
/** @var InputfieldCheckbox */
$field = $modules->get('InputfieldCheckbox');
$field->label = __('Allow overwriting ProcessWire API variables in controllers');
$field->description = __('If enabled, API variables can be overwritten in controllers, e.g. `$view->page = $someotherpage;`');
$field->name = 'api_vars_overwrites';
$field->checked = (bool) $data['api_vars_overwrites'];
$field->showIf = 'api_vars_available=1';
$wrapper->append($field);
/** @var \ProcessWire\InputfieldCheckbox $field */
$field = $modules->get('InputfieldCheckbox');
$field->label = __('Auto-refresh templates (recompile)');
$field->description = __('If enabled, templates are recompiled whenever the source code changes.');
$field->name = 'auto_refresh';
$field->checked = (bool) $data['auto_refresh'];
$wrapper->append($field);
/** @var \ProcessWire\InputfieldCheckbox $field */
$field = $modules->get('InputfieldCheckbox');
$field->label = __('Simplified path resolution');
$field->description = __('Enable Blade-style dot syntax for directory traversal.') . ' ' .
__('This will also prepend the root view folder to all paths.'). PHP_EOL . PHP_EOL .
__("Both of these will render `site/templates/views/partials/navigation.latte`:") . PHP_EOL . ' ' .
__("Before: `{include '../partials/navigation.latte'}`") . PHP_EOL . ' ' .
__("After: `{include 'partials.navigation'}`");
$field->name = 'simple_path_resolution';
$field->checked = (bool) $data['simple_path_resolution'];
$wrapper->append($field);
/** @var \ProcessWire\InputfieldCheckbox $field */
$field = $modules->get('InputfieldCheckbox');
$field->label = __('Clear Latte cache');
$field->description = __('Forces all templates to be recompiled by emptying the temp directory used by Latte.');
$field->name = 'clear_temp';
$field->checked = false;
$wrapper->append($field);
return $wrapper;
}
}