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