Completed
Pull Request — master (#248)
by Claus
02:53 queued 13s
created

VariableViewHelper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 5 1
A renderStatic() 0 8 1
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\ViewHelperNode;
11
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
12
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
13
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
14
15
/**
16
 * Variable assigning ViewHelper
17
 *
18
 * Assigns one template variable which will exist also
19
 * after the ViewHelper is done rendering, i.e. adds
20
 * template variables.
21
 *
22
 * If you require a variable assignment which does not
23
 * exist in the template after a piece of Fluid code
24
 * is rendered, consider using `f:alias` instead.
25
 *
26
 * Usages:
27
 *
28
 *     {f:variable(name: 'myvariable', value: 'some value')}
29
 *     <f:variable name="myvariable">some value</f:variable>
30
 *     {oldvariable -> f:format.htmlspecialchars() -> f:variable(name: 'newvariable')}
31
 *     <f:variable name="myvariable"><f:format.htmlspecialchars>{oldvariable}</f:format.htmlspecialchars></f:variable>
32
 *
33
 * Please note:
34
 *
35
 * If you assign a variable with a dotted name, e.g.
36
 * "myarray.property" then this may or may not be
37
 * supported by the VariableProvider that's being
38
 * used. The default
39
 *
40
 * @see \TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper
41
 * @api
42
 */
43
class VariableViewHelper extends AbstractViewHelper
44
{
45
    use CompileWithContentArgumentAndRenderStatic;
46
47
    /**
48
     * @return void
49
     */
50
    public function initializeArguments()
51
    {
52
        $this->registerArgument('value', 'mixed', 'Value to assign. If not in arguments then taken from tag content');
53
        $this->registerArgument('name', 'string', 'Name of variable to create', true);
54
    }
55
56
    /**
57
     * @param array $arguments
58
     * @param \Closure $renderChildrenClosure
59
     * @param RenderingContextInterface $renderingContext
60
     * @return null
61
     */
62
    public static function renderStatic(
63
        array $arguments,
64
        \Closure $renderChildrenClosure,
65
        RenderingContextInterface $renderingContext
66
    ) {
67
        $value = $renderChildrenClosure();
68
        $renderingContext->getVariableProvider()->add($arguments['name'], $value);
69
    }
70
71
}
72