forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionViewHelper.php
More file actions
121 lines (114 loc) · 3.46 KB
/
SectionViewHelper.php
File metadata and controls
121 lines (114 loc) · 3.46 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
<?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\SyntaxTree\TextNode;
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
use TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\TemplateVariableContainer;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\ParserRuntimeOnly;
/**
* A ViewHelper to declare sections in templates for later use with e.g. the ``f:render`` ViewHelper.
*
* Examples
* ========
*
* 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})
*
* @api
*/
class SectionViewHelper extends AbstractViewHelper
{
use ParserRuntimeOnly;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* Initialize the arguments.
*
* @return void
* @api
*/
public function initializeArguments()
{
$this->registerArgument('name', 'string', 'Name of the section', true);
}
/**
* Save the associated ViewHelper node in a static public class variable.
* called directly after the ViewHelper was built.
*
* @param ViewHelperNode $node
* @param TextNode[] $arguments
* @param VariableProviderInterface $variableContainer
* @return void
*/
public static function postParseEvent(ViewHelperNode $node, array $arguments, VariableProviderInterface $variableContainer)
{
/** @var $nameArgument TextNode */
$nameArgument = $arguments['name'];
$sectionName = $nameArgument->getText();
$sections = $variableContainer['1457379500_sections'] ? $variableContainer['1457379500_sections'] : [];
$sections[$sectionName] = $node;
$variableContainer['1457379500_sections'] = $sections;
}
/**
* Rendering directly returns all child nodes.
*
* @return string HTML String of all child nodes.
* @api
*/
public function render()
{
$content = '';
if ($this->viewHelperVariableContainer->exists(SectionViewHelper::class, 'isCurrentlyRenderingSection')) {
$this->viewHelperVariableContainer->remove(SectionViewHelper::class, 'isCurrentlyRenderingSection');
$content = $this->renderChildren();
}
return $content;
}
}