Completed
Pull Request — master (#415)
by Claus
01:55
created

InlineViewHelper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 9 1
A renderStatic() 0 7 1
1
<?php
2
namespace TYPO3Fluid\Fluid\ViewHelpers;
3
4
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
5
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
6
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic;
7
8
/**
9
 * Inline Fluid rendering ViewHelper
10
 *
11
 * Renders Fluid code stored in a variable, which you normally would
12
 * have to render before assigning it to the view. Instead you can
13
 * do the following (note, extremely simplified use case):
14
 *
15
 *      $view->assign('variable', 'value of my variable');
16
 *      $view->assign('code', 'My variable: {variable}');
17
 *
18
 * And in the template:
19
 *
20
 *      {code -> f:inline()}
21
 *
22
 * Which outputs:
23
 *
24
 *      My variable: value of my variable
25
 *
26
 * You can use this to pass smaller and dynamic pieces of Fluid code
27
 * to templates, as an alternative to creating new partial templates.
28
 */
29
class InlineViewHelper extends AbstractViewHelper
30
{
31
    use CompileWithContentArgumentAndRenderStatic;
32
33
    protected $escapeChildren = false;
34
35
    protected $escapeOutput = false;
36
37
    /**
38
     * @return void
39
     */
40
    public function initializeArguments()
41
    {
42
        $this->registerArgument(
43
            'code',
44
            'string',
45
            'Fluid code to be rendered as if it were part of the template rendering it. Can be passed as inline '.
46
            'argument or tag content'
47
        );
48
    }
49
50
51
    /**
52
     * @param array $arguments
53
     * @param \Closure $renderChildrenClosure
54
     * @param RenderingContextInterface $renderingContext
55
     * @return mixed|string
56
     */
57
    public static function renderStatic(
58
        array $arguments,
59
        \Closure $renderChildrenClosure,
60
        RenderingContextInterface $renderingContext
61
    ) {
62
        return $renderingContext->getTemplateParser()->parse($renderChildrenClosure())->render($renderingContext);
63
    }
64
}
65