088294b32145e0f81e5c906ff33eb57047f5ebfd
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / RenderChildrenViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Render the inner parts of a Widget.
19 * This ViewHelper can only be used in a template which belongs to a Widget Controller.
20 *
21 * It renders everything inside the Widget ViewHelper, and you can pass additional
22 * arguments.
23 *
24 * @api
25 */
26 class RenderChildrenViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
27 {
28 /**
29 * @param array $arguments
30 * @return string
31 */
32 public function render(array $arguments = array())
33 {
34 $renderingContext = $this->getWidgetRenderingContext();
35 $widgetChildNodes = $this->getWidgetChildNodes();
36 $this->addArgumentsToTemplateVariableContainer($arguments);
37 $output = $widgetChildNodes->evaluate($renderingContext);
38 $this->removeArgumentsFromTemplateVariableContainer($arguments);
39 return $output;
40 }
41
42 /**
43 * Get the widget rendering context, or throw an exception if it cannot be found.
44 *
45 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException
46 * @return \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface
47 */
48 protected function getWidgetRenderingContext()
49 {
50 $renderingContext = $this->getWidgetContext()->getViewHelperChildNodeRenderingContext();
51 if (!$renderingContext instanceof \TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface) {
52 throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\RenderingContextNotFoundException('Rendering Context not found inside Widget. <f:renderChildren> has been used in an AJAX Request, but is only usable in non-ajax mode.', 1284986604);
53 }
54 return $renderingContext;
55 }
56
57 /**
58 * @return \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\RootNode
59 */
60 protected function getWidgetChildNodes()
61 {
62 return $this->getWidgetContext()->getViewHelperChildNodes();
63 }
64
65 /**
66 * @throws \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException
67 * @return \TYPO3\CMS\Fluid\Core\Widget\WidgetContext
68 */
69 protected function getWidgetContext()
70 {
71 $request = $this->controllerContext->getRequest();
72 if (!$request instanceof \TYPO3\CMS\Fluid\Core\Widget\WidgetRequest) {
73 throw new \TYPO3\CMS\Fluid\Core\Widget\Exception\WidgetRequestNotFoundException('The Request is not a WidgetRequest! <f:renderChildren> must be called inside a Widget Template.', 1284986120);
74 }
75 return $request->getWidgetContext();
76 }
77
78 /**
79 * Add the given arguments to the TemplateVariableContainer of the widget.
80 *
81 * @param array $arguments
82 * @return void
83 */
84 protected function addArgumentsToTemplateVariableContainer(array $arguments)
85 {
86 $templateVariableContainer = $this->getWidgetRenderingContext()->getTemplateVariableContainer();
87 foreach ($arguments as $identifier => $value) {
88 $templateVariableContainer->add($identifier, $value);
89 }
90 }
91
92 /**
93 * Remove the given arguments from the TemplateVariableContainer of the widget.
94 *
95 * @param array $arguments
96 * @return void
97 */
98 protected function removeArgumentsFromTemplateVariableContainer(array $arguments)
99 {
100 $templateVariableContainer = $this->getWidgetRenderingContext()->getTemplateVariableContainer();
101 foreach ($arguments as $identifier => $value) {
102 $templateVariableContainer->remove($identifier);
103 }
104 }
105 }