-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWireframeRendererTwig.module.php
More file actions
142 lines (126 loc) · 4.1 KB
/
WireframeRendererTwig.module.php
File metadata and controls
142 lines (126 loc) · 4.1 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
<?php
namespace ProcessWire;
/**
* Wireframe Renderer Twig
*
* @version 0.2.0
* @author Teppo Koivula <teppo@wireframe-framework.com>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
class WireframeRendererTwig extends Wire implements Module {
/**
* Twig loader
*
* @var \Twig\Loader\FilesystemLoader
*/
protected $loader;
/**
* Twig environment
*
* @var \Twig\Environment
*/
protected $twig;
/**
* View file extension
*
* @var string
*/
protected $ext = 'twig';
/**
* Init method
*
* If you want to override any of the parameters passed to Twig Environment, you can do this by
* providing 'environment' array via the settings parameter:
*
* ```
* $wireframe->setRenderer($modules->get('WireframeRendererTwig')->init([
* 'environment' => [
* 'autoescape' => false,
* 'cache' => '/your/custom/cache/path',
* ],
* ]))
* ```
*
* @param array $settings Additional settings (optional).
* @return WireframeRendererTwig Self-reference.
*/
public function ___init(array $settings = []): WireframeRendererTwig {
// optionally override the default file extension
if (!empty($settings['ext'])) {
$this->ext = $settings['ext'];
}
// autoload Twig classes
if (!class_exists('\Twig\FilesystemLoader')) {
require_once(__DIR__ . '/vendor/autoload.php' /*NoCompile*/);
}
// init Twig Loader and Environment
$this->loader = $this->initLoader($settings['loader'] ?? []);
$this->twig = $this->initTwig($settings['environment'] ?? []);
return $this;
}
/**
* Init Twig FilesystemLoader
*
* @param array $settings Twig Loader settings (optional).
* @return \Twig\Loader\FilesystemLoader
*/
public function ___initLoader(array $settings = []): \Twig\Loader\FilesystemLoader {
$wireframe = $this->wire('modules')->get('Wireframe');
$loader = new \Twig\Loader\FilesystemLoader($wireframe->paths->partials);
foreach ($wireframe->getViewPaths() as $type => $path) {
$loader->addPath($path, $type);
}
return $loader;
}
/**
* Init Twig Environment
*
* @param array $settings Twig Environment settings (optional).
* @return \Twig\Environment
*/
public function ___initTwig(array $settings = []): \Twig\Environment {
$twig = (new \Twig\Environment($this->loader, array_merge([
'autoescape' => 'name',
'auto_reload' => true,
'cache' => $this->wire('config')->paths->cache . '/WireframeRendererTwig',
'debug' => $this->wire('config')->debug,
], $settings['environment'] ?? [])));
if ($this->wire('config')->debug) {
$twig->addExtension(new \Twig\Extension\DebugExtension());
}
return $twig;
}
/**
* Render method
*
* @param string $type Type of file to render (view, layout, partial, or component).
* @param string $view Name of the view file to render.
* @param array $context Variables used for rendering.
* @return string Rendered markup.
* @throws WireException if param $type has an unexpected value.
*/
public function render(string $type, string $view, array $context = []): string {
if (!in_array($type, array_keys($this->wire('modules')->get('Wireframe')->getViewPaths()))) {
throw new WireException(sprintf('Unexpected type (%s).', $type));
}
return $this->twig->render('@' . $type . '/' . $view, $context);
}
/**
* Set view file extension
*
* @param string $ext View file extension.
* @return WireframeRendererTwig Self-reference.
*/
public function setExt(string $ext): WireframeRendererTwig {
$this->ext = $ext;
return $this;
}
/**
* Get view file extension
*
* @return string View file extension.
*/
public function getExt(): string {
return $this->ext;
}
}