Completed
Pull Request — master (#293)
by Christian
02:05
created

SectionViewHelper::compile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace TYPO3Fluid\Fluid\ViewHelpers;
3
4
/*
5
 * This file belongs to the package "TYPO3 Fluid".
6
 * See LICENSE.txt that was shipped with this package.
7
 */
8
9
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
10
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\TextNode;
11
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
12
use TYPO3Fluid\Fluid\Core\Variables\VariableProviderInterface;
13
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
14
use TYPO3Fluid\Fluid\Core\ViewHelper\TemplateVariableContainer;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\ParserRuntimeOnly;
16
17
/**
18
 * A ViewHelper to declare sections in templates for later use with e.g. the RenderViewHelper.
19
 *
20
 * = Examples =
21
 *
22
 * <code title="Rendering sections">
23
 * <f:section name="someSection">This is a section. {foo}</f:section>
24
 * <f:render section="someSection" arguments="{foo: someVariable}" />
25
 * </code>
26
 * <output>
27
 * the content of the section "someSection". The content of the variable {someVariable} will be available in the partial as {foo}
28
 * </output>
29
 *
30
 * <code title="Rendering recursive sections">
31
 * <f:section name="mySection">
32
 *  <ul>
33
 *    <f:for each="{myMenu}" as="menuItem">
34
 *      <li>
35
 *        {menuItem.text}
36
 *        <f:if condition="{menuItem.subItems}">
37
 *          <f:render section="mySection" arguments="{myMenu: menuItem.subItems}" />
38
 *        </f:if>
39
 *      </li>
40
 *    </f:for>
41
 *  </ul>
42
 * </f:section>
43
 * <f:render section="mySection" arguments="{myMenu: menu}" />
44
 * </code>
45
 * <output>
46
 * <ul>
47
 *   <li>menu1
48
 *     <ul>
49
 *       <li>menu1a</li>
50
 *       <li>menu1b</li>
51
 *     </ul>
52
 *   </li>
53
 * [...]
54
 * (depending on the value of {menu})
55
 * </output>
56
 *
57
 * @api
58
 */
59
class SectionViewHelper extends AbstractViewHelper
60
{
61
    use ParserRuntimeOnly;
62
63
    /**
64
     * @var boolean
65
     */
66
    protected $escapeOutput = false;
67
68
    /**
69
     * Initialize the arguments.
70
     *
71
     * @return void
72
     * @api
73
     */
74
    public function initializeArguments()
75
    {
76
        $this->registerArgument('name', 'string', 'Name of the section', true);
77
    }
78
79
    /**
80
     * Save the associated ViewHelper node in a static public class variable.
81
     * called directly after the ViewHelper was built.
82
     *
83
     * @param ViewHelperNode $node
84
     * @param TextNode[] $arguments
85
     * @param VariableProviderInterface $variableContainer
86
     * @return void
87
     */
88
    public static function postParseEvent(ViewHelperNode $node, array $arguments, VariableProviderInterface $variableContainer)
89
    {
90
        /** @var $nameArgument TextNode */
91
        $nameArgument = $arguments['name'];
92
        $sectionName = $nameArgument->getText();
93
        $sections = $variableContainer['1457379500_sections'] ? $variableContainer['1457379500_sections'] : [];
94
        $sections[$sectionName] = $node;
95
        $variableContainer['1457379500_sections'] = $sections;
96
    }
97
98
    /**
99
     * Rendering directly returns all child nodes.
100
     *
101
     * @return string HTML String of all child nodes.
102
     * @api
103
     */
104
    public function render()
105
    {
106
        $content = '';
107
        if ($this->viewHelperVariableContainer->exists(SectionViewHelper::class, 'isCurrentlyRenderingSection')) {
108
            $this->viewHelperVariableContainer->remove(SectionViewHelper::class, 'isCurrentlyRenderingSection');
109
            $content = $this->renderChildren();
110
        }
111
        return $content;
112
    }
113
}
114