|
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
|
|
|
* @see \TYPO3Fluid\Fluid\ViewHelpers\IfViewHelper |
|
34
|
|
|
* @api |
|
35
|
|
|
*/ |
|
36
|
|
|
class VariableViewHelper extends AbstractViewHelper |
|
37
|
|
|
{ |
|
38
|
|
|
use CompileWithContentArgumentAndRenderStatic; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function initializeArguments() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->registerArgument('value', 'mixed', 'Value to assign. If not in arguments then taken from tag content'); |
|
46
|
|
|
$this->registerArgument('name', 'string', 'Name of variable to create', true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $arguments |
|
51
|
|
|
* @param \Closure $renderChildrenClosure |
|
52
|
|
|
* @param RenderingContextInterface $renderingContext |
|
53
|
|
|
* @return null |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function renderStatic( |
|
56
|
|
|
array $arguments, |
|
57
|
|
|
\Closure $renderChildrenClosure, |
|
58
|
|
|
RenderingContextInterface $renderingContext |
|
59
|
|
|
) { |
|
60
|
|
|
$value = $renderChildrenClosure(); |
|
61
|
|
|
$renderingContext->getVariableProvider()->add($arguments['name'], $value); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|