/
TemplateImplementation.php
158 lines (142 loc) · 4.97 KB
/
TemplateImplementation.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
155
156
157
158
<?php
namespace Neos\Fusion\FusionObjects;
/*
* This file is part of the Neos.Fusion package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Mvc\ActionRequest;
use Neos\FluidAdaptor\Core\Parser\Interceptor\ResourceInterceptor;
use TYPO3Fluid\Fluid\Core\Parser\InterceptorInterface;
/**
* Fusion object rendering a fluid template
*
* //fusionPath variables TODO The result of this Fusion object is made available inside the template as "variables"
* @api
*/
class TemplateImplementation extends AbstractArrayFusionObject
{
/**
* Path to the template which should be rendered
*
* @return string
*/
public function getTemplatePath()
{
return $this->fusionValue('templatePath');
}
/**
* Path to the partial root
*
* @return string
*/
public function getPartialRootPath()
{
return $this->fusionValue('partialRootPath');
}
/**
* Path to the layout root
*
* @return string
*/
public function getLayoutRootPath()
{
return $this->fusionValue('layoutRootPath');
}
/**
* Name of a specific section, if only this section should be rendered.
*
* @return string
*/
public function getSectionName()
{
return $this->fusionValue('sectionName');
}
/**
* @return string
* @internal
*/
public function getPath()
{
return $this->path;
}
/**
* {@inheritdoc}
*
* @return string
*/
public function evaluate()
{
$actionRequest = $this->runtime->getControllerContext()->getRequest();
if (!$actionRequest instanceof ActionRequest) {
$actionRequest = null;
}
$fluidTemplate = new Helpers\FluidView($this, $actionRequest);
$templatePath = $this->getTemplatePath();
if ($templatePath === null) {
throw new \Exception(sprintf("
No template path set.
Most likely you didn't configure `templatePath` in your Fusion object correctly.
For example you could add and adapt the following line to your Fusion:
`prototype(%s) < prototype(Neos.Fusion:Template) {
templatePath = 'resource://Vendor.Package/Private/Templates/MyObject.html'
}`
", $this->fusionObjectName));
}
$fluidTemplate->setTemplatePathAndFilename($templatePath);
$partialRootPath = $this->getPartialRootPath();
if ($partialRootPath !== null) {
$fluidTemplate->setPartialRootPath($partialRootPath);
}
$layoutRootPath = $this->getLayoutRootPath();
if ($layoutRootPath !== null) {
$fluidTemplate->setLayoutRootPath($layoutRootPath);
}
// Template resources need to be evaluated from the templates package not the requests package.
if (strpos($templatePath, 'resource://') === 0) {
$templateResourcePathParts = parse_url($templatePath);
foreach ($fluidTemplate->getRenderingContext()->buildParserConfiguration()->getInterceptors(InterceptorInterface::INTERCEPT_TEXT) as $interceptor) {
if ($interceptor instanceof ResourceInterceptor) {
$interceptor->setDefaultPackageKey($templateResourcePathParts['host']);
}
}
}
foreach ($this->properties as $key => $value) {
if (in_array($key, $this->ignoreProperties)) {
continue;
}
if (!is_array($value)) {
// if a value is a SIMPLE TYPE, e.g. neither an Eel expression nor a Fusion object,
// we can just evaluate it (to handle processors) and then assign it to the template.
$evaluatedValue = $this->fusionValue($key);
$fluidTemplate->assign($key, $evaluatedValue);
} else {
// It is an array; so we need to create a "proxy" for lazy evaluation, as it could be a
// nested Fusion object, Eel expression or simple value.
$fluidTemplate->assign($key, new Helpers\FusionPathProxy($this, $this->path . '/' . $key, $value));
}
}
$this->initializeView($fluidTemplate);
$sectionName = $this->getSectionName();
if ($sectionName !== null) {
return $fluidTemplate->renderSection($sectionName);
} else {
return $fluidTemplate->render();
}
}
/**
* This is a template method which can be overridden in subclasses to add new variables which should
* be available inside the Fluid template. It is needed e.g. for Expose.
*
* @param Helpers\FluidView $view
* @return void
*/
protected function initializeView(Helpers\FluidView $view)
{
// template method
}
}