forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderViewHelper.php
More file actions
185 lines (178 loc) · 7.39 KB
/
RenderViewHelper.php
File metadata and controls
185 lines (178 loc) · 7.39 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace TYPO3Fluid\Fluid\ViewHelpers;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
use TYPO3Fluid\Fluid\Core\Parser\ParsedTemplateInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderableInterface;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
/**
* A ViewHelper to render a section, a partial, a specified section in a partial
* or a delegate ParsedTemplateInterface implementation.
*
* Examples
* ========
*
* Rendering partials
* ------------------
*
* ::
*
* <f:render partial="SomePartial" arguments="{foo: someVariable}" />
*
* Output::
*
* the content of the partial "SomePartial". The content of the variable {someVariable} will be available in the partial as {foo}
*
* Rendering sections
* ------------------
*
* ::
*
* <f:section name="someSection">This is a section. {foo}</f:section>
* <f:render section="someSection" arguments="{foo: someVariable}" />
*
* Output::
*
* the content of the section "someSection". The content of the variable {someVariable} will be available in the partial as {foo}
*
* Rendering recursive sections
* ----------------------------
*
* ::
*
* <f:section name="mySection">
* <ul>
* <f:for each="{myMenu}" as="menuItem">
* <li>
* {menuItem.text}
* <f:if condition="{menuItem.subItems}">
* <f:render section="mySection" arguments="{myMenu: menuItem.subItems}" />
* </f:if>
* </li>
* </f:for>
* </ul>
* </f:section>
* <f:render section="mySection" arguments="{myMenu: menu}" />
*
* Output::
*
* <ul>
* <li>menu1
* <ul>
* <li>menu1a</li>
* <li>menu1b</li>
* </ul>
* </li>
* [...]
* (depending on the value of {menu})
*
*
* Passing all variables to a partial
* ----------------------------------
*
* ::
*
* <f:render partial="somePartial" arguments="{_all}" />
*
* Output::
*
* the content of the partial "somePartial".
* Using the reserved keyword "_all", all available variables will be passed along to the partial
*
*
* Rendering via a delegate ParsedTemplateInterface implementation w/ custom arguments
* -----------------------------------------------------------------------------------
*
* ::
*
* <f:render delegate="My\Special\ParsedTemplateImplementation" arguments="{_all}" />
*
* This will output whichever output was generated by calling ``My\Special\ParsedTemplateImplementation->render()``
* with cloned RenderingContextInterface $renderingContext as only argument and content of arguments
* assigned in VariableProvider of cloned context. Supports all other input arguments including
* recursive rendering, contentAs argument, default value etc.
*
* Note that while ParsedTemplateInterface supports returning a Layout name, this Layout will not
* be respected when rendering using this method. Only the ``render()`` method will be called!
*
* @api
*/
class RenderViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('section', 'string', 'Section to render - combine with partial to render section in partial');
$this->registerArgument('partial', 'string', 'Partial to render, with or without section');
$this->registerArgument('delegate', 'string', 'Optional PHP class name of a permanent, included-in-app ParsedTemplateInterface implementation to override partial/section');
$this->registerArgument('renderable', RenderableInterface::class, 'Instance of a RenderableInterface implementation to be rendered');
$this->registerArgument('arguments', 'array', 'Array of variables to be transferred. Use {_all} for all variables', false, []);
$this->registerArgument('optional', 'boolean', 'If TRUE, considers the *section* optional. Partial never is.', false, false);
$this->registerArgument('default', 'mixed', 'Value (usually string) to be displayed if the section or partial does not exist');
$this->registerArgument('contentAs', 'string', 'If used, renders the child content and adds it as a template variable with this name for use in the partial/section');
}
/**
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$section = $arguments['section'];
$partial = $arguments['partial'];
$variables = (array) $arguments['arguments'];
$optional = (boolean) $arguments['optional'];
$delegate = $arguments['delegate'];
/** @var RenderableInterface $renderable */
$renderable = $arguments['renderable'];
$tagContent = $renderChildrenClosure();
if ($arguments['contentAs']) {
$variables[$arguments['contentAs']] = $tagContent;
}
$view = $renderingContext->getViewHelperVariableContainer()->getView();
if (!$view) {
throw new Exception(
'The f:render ViewHelper was used in a context where the ViewHelperVariableContainer does not contain ' .
'a reference to the View. Normally this is taken care of by the TemplateView, so most likely this ' .
'error is because you overrode AbstractTemplateView->initializeRenderingContext() and did not call ' .
'$renderingContext->getViewHelperVariableContainer()->setView($this) or parent::initializeRenderingContext. ' .
'This is an issue you must fix in your code as f:render is fully unable to render anything without a View.'
);
}
$content = '';
if ($renderable) {
$content = $renderable->render($renderingContext);
} elseif ($delegate !== null) {
if (!is_a($delegate, ParsedTemplateInterface::class, true)) {
throw new \InvalidArgumentException(sprintf('Cannot render %s - must implement ParsedTemplateInterface!', $delegate));
}
$renderingContext = clone $renderingContext;
$renderingContext->getVariableProvider()->setSource($variables);
$content = (new $delegate())->render($renderingContext);
} elseif ($partial !== null) {
$content = $view->renderPartial($partial, $section, $variables, $optional);
} elseif ($section !== null) {
$content = $view->renderSection($section, $variables, $optional);
} elseif (!$optional) {
throw new \InvalidArgumentException('ViewHelper f:render called without either argument section, partial, renderable or delegate and optional flag is false');
}
// Replace empty content with default value. If default is
// not set, NULL is returned and cast to a new, empty string
// outside of this ViewHelper.
if ($content === '') {
$content = $arguments['default'] ?: $tagContent;
}
return $content;
}
}