-
Notifications
You must be signed in to change notification settings - Fork 91
/
VariableViewHelper.php
62 lines (56 loc) · 1.94 KB
/
VariableViewHelper.php
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
<?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\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
/**
* Variable assigning ViewHelper
*
* Assigns one template variable which will exist also
* after the ViewHelper is done rendering, i.e. adds
* template variables.
*
* If you require a variable assignment which does not
* exist in the template after a piece of Fluid code
* is rendered, consider using `f:alias` instead.
*
* Usages:
*
* {f:variable(name: 'myvariable', value: 'some value')}
* <f:variable name="myvariable">some value</f:variable>
* {oldvariable -> f:format.htmlspecialchars() -> f:variable(name: 'newvariable')}
* <f:variable name="myvariable"><f:format.htmlspecialchars>{oldvariable}</f:format.htmlspecialchars></f:variable>
*
* @see \TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper
* @api
*/
class VariableViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
/**
* @return void
*/
public function initializeArguments()
{
$this->registerArgument('value', 'mixed', 'Value to assign. If not in arguments then taken from tag content');
$this->registerArgument('name', 'string', 'Name of variable to create', true);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return null
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
$value = $renderChildrenClosure();
$renderingContext->getVariableProvider()->add($arguments['name'], $value);
}
}