Completed
Pull Request — master (#393)
by Claus
06:03
created

IfViewHelper::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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\Rendering\RenderingContextInterface;
10
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
11
12
/**
13
 * This view helper implements an if/else condition.
14
 *
15
 * **Conditions:**
16
 *
17
 * As a condition is a boolean value, you can just use a boolean argument.
18
 * Alternatively, you can write a boolean expression there.
19
 * Boolean expressions have the following form:
20
 * XX Comparator YY
21
 * Comparator is one of: ==, !=, <, <=, >, >= and %
22
 * The % operator converts the result of the % operation to boolean.
23
 *
24
 * XX and YY can be one of:
25
 * - number
26
 * - Object Accessor
27
 * - Array
28
 * - a ViewHelper
29
 * Note: Strings at XX/YY are NOT allowed, however, for the time being,
30
 * a string comparison can be achieved with comparing arrays (see example
31
 * below).
32
 * ::
33
 *
34
 *   <f:if condition="{rank} > 100">
35
 *     Will be shown if rank is > 100
36
 *   </f:if>
37
 *   <f:if condition="{rank} % 2">
38
 *     Will be shown if rank % 2 != 0.
39
 *   </f:if>
40
 *   <f:if condition="{rank} == {k:bar()}">
41
 *     Checks if rank is equal to the result of the ViewHelper "k:bar"
42
 *   </f:if>
43
 *   <f:if condition="{0: foo.bar} == {0: 'stringToCompare'}">
44
 *     Will result true if {foo.bar}'s represented value equals 'stringToCompare'.
45
 *   </f:if>
46
 *
47
 * = Examples =
48
 *
49
 * <code title="Basic usage">
50
 * <f:if condition="somecondition">
51
 *   This is being shown in case the condition matches
52
 * </f:if>
53
 * </code>
54
 * <output>
55
 * Everything inside the <f:if> tag is being displayed if the condition evaluates to TRUE.
56
 * </output>
57
 *
58
 * <code title="If / then / else">
59
 * <f:if condition="somecondition">
60
 *   <f:then>
61
 *     This is being shown in case the condition matches.
62
 *   </f:then>
63
 *   <f:else>
64
 *     This is being displayed in case the condition evaluates to FALSE.
65
 *   </f:else>
66
 * </f:if>
67
 * </code>
68
 * <output>
69
 * Everything inside the "then" tag is displayed if the condition evaluates to TRUE.
70
 * Otherwise, everything inside the "else"-tag is displayed.
71
 * </output>
72
 *
73
 * <code title="inline notation">
74
 * {f:if(condition: someCondition, then: 'condition is met', else: 'condition is not met')}
75
 * </code>
76
 * <output>
77
 * The value of the "then" attribute is displayed if the condition evaluates to TRUE.
78
 * Otherwise, everything the value of the "else"-attribute is displayed.
79
 * </output>
80
 *
81
 * @api
82
 */
83
class IfViewHelper extends AbstractConditionViewHelper
84
{
85
    /**
86
     * @param array $arguments
87
     * @param RenderingContextInterface $renderingContext
88
     * @return bool
89
     */
90
    public static function verdict(array $arguments, RenderingContextInterface $renderingContext)
91
    {
92
        return (bool)$arguments['condition'];
93
    }
94
}
95