-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWireframeRendererLatte.module.php
123 lines (109 loc) · 3.49 KB
/
WireframeRendererLatte.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
<?php
namespace ProcessWire;
/**
* Wireframe Renderer Latte
*
* @version 0.0.1
* @author Teppo Koivula <[email protected]>
* @license Mozilla Public License v2.0 https://mozilla.org/MPL/2.0/
*/
class WireframeRendererLatte extends Wire implements Module {
/**
* Latte Engine
*
* @var \Latte\Engine
*/
protected $latte;
/**
* View file extension
*
* @var string
*/
protected $ext = 'latte';
/**
* Init method
*
* If you want to override any of the parameters passed to Latte Engine, you can do this by
* providing 'latte' array via the settings parameter:
*
* ```
* $wireframe->setRenderer($modules->get('WireframeRendererLatte')->init([
* 'latte' => [
* 'tempDirectory' => '/your/custom/cache/path',
* ],
* ]))
* ```
*
* @param array $settings Additional settings (optional).
* @return WireframeRendererLatte Self-reference.
*/
public function ___init(array $settings = []): WireframeRendererLatte {
// optionally override the default file extension
if (!empty($settings['ext'])) {
$this->ext = $settings['ext'];
}
// autoload Latte classes
if (!class_exists('\Latte\Engine')) {
require_once(__DIR__ . '/vendor/autoload.php' /*NoCompile*/);
}
// init Latte Engine
$this->latte = $this->initLatte($settings['latte'] ?? []);
return $this;
}
/**
* Init Latte
*
* @param array $settings Latte settings
* @return \Latte\Engine
*/
public function ___initLatte(array $settings = []): \Latte\Engine {
$settings = array_merge([
'tempDirectory' => $this->wire('config')->paths->cache . '/WireframeRendererLatte',
'baseDir' => rtrim($this->wire('config')->paths->templates . '', '/'),
], $settings);
require_once __DIR__ . '/bin/FileLoaderWireframe.php';
$wireframe = $this->wire('modules')->get('Wireframe');
$loader = new \Latte\Loaders\FileLoaderWireframe($settings['baseDir']);
foreach ($wireframe->getViewPaths() as $type => $path) {
$loader->addTypeBaseDir($type, rtrim($path, '/'));
}
$latte = new \Latte\Engine;
$latte->setTempDirectory($settings['tempDirectory']);
$latte->setLoader($loader);
return $latte;
}
/**
* 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 $params Params used for rendering.
* @return string Rendered markup.
* @throws WireException if param $type has an unexpected value.
*/
public function render(string $type, string $view, array $params = []): string {
if (!in_array($type, array_keys($this->wire('modules')->get('Wireframe')->getViewPaths()))) {
throw new WireException(sprintf('Unexpected type (%s).', $type));
}
$out = $this->latte->renderToString('@' . $type . '/' . $view, $params);
return $out;
}
/**
* Set view file extension
*
* @param string $ext View file extension.
* @return WireframeRendererLatte Self-reference.
*/
public function setExt(string $ext): WireframeRendererLatte {
$this->ext = $ext;
return $this;
}
/**
* Get view file extension
*
* @return string View file extension.
*/
public function getExt(): string {
return $this->ext;
}
}