1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace AcMailer\View; |
5
|
|
|
|
6
|
|
|
use Zend\Expressive\Template\TemplatePath; |
7
|
|
|
use Zend\Expressive\Template\TemplateRendererInterface; |
8
|
|
|
use Zend\View\Model\ViewModel; |
9
|
|
|
use Zend\View\Renderer\RendererInterface; |
10
|
|
|
|
11
|
|
|
class SimpleZendViewRenderer implements TemplateRendererInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var RendererInterface |
15
|
|
|
*/ |
16
|
|
|
private $renderer; |
17
|
|
|
|
18
|
|
|
public function __construct(RendererInterface $renderer) |
19
|
|
|
{ |
20
|
|
|
$this->renderer = $renderer; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Render a template, optionally with parameters. |
25
|
|
|
* |
26
|
|
|
* Implementations MUST support the `namespace::template` naming convention, |
27
|
|
|
* and allow omitting the filename extension. |
28
|
|
|
* |
29
|
|
|
* @param string $name |
30
|
|
|
* @param array|\Traversable $params |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function render($name, $params = []): string |
34
|
|
|
{ |
35
|
|
|
$layout = $params['layout'] ?? null; |
36
|
|
|
unset($params['layout']); |
37
|
|
|
|
38
|
|
|
$viewModel = new ViewModel($params); |
39
|
|
|
$viewModel->setTemplate($name); |
40
|
|
|
|
41
|
|
|
// If a layout was provided, add the original view model as a child |
42
|
|
|
if ($layout !== null) { |
43
|
|
|
$layoutModel = new ViewModel([ |
44
|
|
|
'content' => $this->renderer->render($viewModel), |
45
|
|
|
]); |
46
|
|
|
$layoutModel->setTemplate($layout); |
47
|
|
|
$viewModel = $layoutModel; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->renderer->render($viewModel); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Add a template path to the engine. |
55
|
|
|
* |
56
|
|
|
* Adds a template path, with optional namespace the templates in that path |
57
|
|
|
* provide. |
58
|
|
|
* |
59
|
|
|
* @param string $path |
60
|
|
|
* @param string $namespace |
61
|
|
|
*/ |
62
|
|
|
public function addPath($path, $namespace = null) |
63
|
|
|
{ |
64
|
|
|
// Do nothing |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Retrieve configured paths from the engine. |
69
|
|
|
* |
70
|
|
|
* @return TemplatePath[] |
71
|
|
|
*/ |
72
|
|
|
public function getPaths(): array |
73
|
|
|
{ |
74
|
|
|
return []; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add a default parameter to use with a template. |
79
|
|
|
* |
80
|
|
|
* Use this method to provide a default parameter to use when a template is |
81
|
|
|
* rendered. The parameter may be overridden by providing it when calling |
82
|
|
|
* `render()`, or by calling this method again with a null value. |
83
|
|
|
* |
84
|
|
|
* The parameter will be specific to the template name provided. To make |
85
|
|
|
* the parameter available to any template, pass the TEMPLATE_ALL constant |
86
|
|
|
* for the template name. |
87
|
|
|
* |
88
|
|
|
* If the default parameter existed previously, subsequent invocations with |
89
|
|
|
* the same template name and parameter name will overwrite. |
90
|
|
|
* |
91
|
|
|
* @param string $templateName Name of template to which the param applies; |
92
|
|
|
* use TEMPLATE_ALL to apply to all templates. |
93
|
|
|
* @param string $param Param name. |
94
|
|
|
* @param mixed $value |
95
|
|
|
*/ |
96
|
|
|
public function addDefaultParam($templateName, $param, $value) |
97
|
|
|
{ |
98
|
|
|
// Do nothing |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|