Attention
TYPO3 v11 has reached end-of-life as of October 31th 2024 and is no longer being maintained. Use the version switcher on the top left of this page to select documentation for a supported version of TYPO3.
Need more time before upgrading? You can purchase Extended Long Term Support (ELTS) for TYPO3 v11 here: TYPO3 ELTS.
Switch ViewHelper <f:switch>
Switch ViewHelper which can be used to render content depending on a value or expression.
Implements what a basic PHP switch()
does.
An optional default case can be specified which is rendered if none of the
case
conditions matches.
Using this ViewHelper can be a sign of weak architecture. If you end up using it extensively
you might want to consider restructuring your controllers/actions and/or use partials and sections.
E.g. the above example could be achieved with <f:
and the partials "title.male.html", "title.female.html", ...
Depending on the scenario this can be easier to extend and possibly contains less duplication.
Examples
Simple Switch statement
<f:switch expression="{person.gender}">
<f:case value="male">Mr.</f:case>
<f:case value="female">Mrs.</f:case>
<f:defaultCase>Mr. / Mrs.</f:defaultCase>
</f:switch>
Output:
"Mr.", "Mrs." or "Mr. / Mrs." (depending on the value of {person.gender})
Source code
Go to the source code of this ViewHelper: SwitchViewHelper.php (GitHub).
Arguments
The following arguments are available for <f:
:
expression
-
- Type
- mixed
- Required
true
Expression to switch