forked from TYPO3/Fluid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAliasViewHelper.php
More file actions
94 lines (87 loc) · 2.37 KB
/
AliasViewHelper.php
File metadata and controls
94 lines (87 loc) · 2.37 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
<?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\CompileWithRenderStatic;
/**
* Declares new variables which are aliases of other variables.
* Takes a "map"-Parameter which is an associative array which defines the shorthand mapping.
*
* The variables are only declared inside the ``<f:alias>...</f:alias>`` tag. After the
* closing tag, all declared variables are removed again.
*
* Using this ViewHelper can be a sign of weak architecture. If you end up
* using it extensively you might want to fine-tune your "view model" (the
* data you assign to the view).
*
* Examples
* ========
*
* Single alias
* ------------
*
* ::
*
* <f:alias map="{x: 'foo'}">{x}</f:alias>
*
* Output::
*
* foo
*
* Multiple mappings
* -----------------
*
* ::
*
* <f:alias map="{x: foo.bar.baz, y: foo.bar.baz.name}">
* {x.name} or {y}
* </f:alias>
*
* Output::
*
* [name] or [name]
*
* Depending on ``{foo.bar.baz}``.
*
*
* @api
*/
class AliasViewHelper extends AbstractViewHelper
{
use CompileWithRenderStatic;
/**
* @var boolean
*/
protected $escapeOutput = false;
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();
$this->registerArgument('map', 'array', 'Array that specifies which variables should be mapped to which alias', true);
}
/**
* @param array $arguments
* @param \Closure $renderChildrenClosure
* @param RenderingContextInterface $renderingContext
* @return mixed
*/
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$templateVariableContainer = $renderingContext->getVariableProvider();
$map = $arguments['map'];
foreach ($map as $aliasName => $value) {
$templateVariableContainer->add($aliasName, $value);
}
$output = $renderChildrenClosure();
foreach ($map as $aliasName => $value) {
$templateVariableContainer->remove($aliasName);
}
return $output;
}
}