-
Notifications
You must be signed in to change notification settings - Fork 0
/
EngineView.hh
148 lines (120 loc) · 3.71 KB
/
EngineView.hh
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
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\View;
use Titon\View\Engine;
use Titon\View\Engine\TemplateEngine;
use Titon\View\Event\RenderedEvent;
use Titon\View\Event\RenderedTemplateEvent;
use Titon\View\Event\RenderingEvent;
use Titon\View\Event\RenderingTemplateEvent;
/**
* Adds support for rendering engines which handle the basics of rendering a template.
*
* @package Titon\View\View
*/
class EngineView extends AbstractView {
/**
* Template rendering engine.
*
* @var \Titon\View\Engine
*/
protected Engine $engine;
/**
* Set the default rendering engine.
*
* @param \Titon\View\Locator $locator
* @param \Titon\View\Engine $engine
*/
public function __construct(Locator $locator, ?Engine $engine = null) {
parent::__construct($locator);
if ($engine === null) {
$engine = new TemplateEngine();
}
$this->engine = $engine;
$this->engine->setView($this);
}
/**
* Return the rendering engine.
*
* @return \Titon\View\Engine
*/
public function getEngine(): Engine {
return $this->engine;
}
/**
* {@inheritdoc}
*/
public function render(string $template, bool $private = false): string {
$engine = $this->getEngine();
// Emit before event
$event = new RenderingEvent($this, $template);
$this->emit($event);
$template = $event->getTemplate();
// Render template
$this->renderLoop($template, $private ? Template::CLOSED : Template::OPEN);
// Apply wrappers
foreach ($engine->getWrappers() as $wrapper) {
$this->renderLoop($wrapper, Template::WRAPPER);
}
// Apply layout
if ($layout = $engine->getLayout()) {
$this->renderLoop($layout, Template::LAYOUT);
}
// Emit after event
$event = new RenderedEvent($this, $engine->getContent());
$this->emit($event);
return $event->getContent();
}
/**
* Rendering of individual template parts for each type: layout, wrapper, etc.
*
* @param string $template
* @param \Titon\View\Template $type
* @return $this
*/
public function renderLoop(string $template, Template $type): this {
$engine = $this->getEngine();
// Emit before event
$event = new RenderingTemplateEvent($this, $template, $type);
$this->emit($event);
$template = $event->getTemplate();
// Render content
$content = $this->renderTemplate(
$this->getLocator()->locate($template, $type),
$this->getVariables()
);
// Emit after event
$event = new RenderedTemplateEvent($this, $content, $type);
$this->emit($event);
$content = $event->getContent();
// Set content
$engine->setContent($content);
return $this;
}
/**
* {@inheritdoc}
*/
public function renderTemplate(string $path, DataMap $variables = Map {}): string {
$expires = $variables->get('cache') ?: '+1 day';
$callback = () ==> $this->getEngine()->render($path, $variables);
if ($storage = $this->getStorage()) {
return (string) $storage->store(md5($path), $callback, $expires);
}
return (string) $callback();
}
/**
* Set the rendering engine.
*
* @param \Titon\View\Engine $engine
* @return $this
*/
public function setEngine(Engine $engine): this {
$engine->setView($this);
$this->engine = $engine;
return $this;
}
}