Documentation · ViewHelper reference

SwitchViewHelper


Switch view helper which can be used to render content depending on a value or expression.
Implements what a basic switch()-PHP-method does.

Examples

Simple Switch statement

<f:switch expression="{person.gender}">
  <f:case value="male">Mr.</f:case>
  <f:case value="female">Mrs.</f:case>
  <f:case default="TRUE">Mrs. or Mr.</f:case>
</f:switch>

Output

Mr. / Mrs. (depending on the value of {person.gender}) or if no value evaluates to TRUE, default case


Note: Using this view helper 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:render partial="title.{person.gender}" /> and the partials "title.male.html", "title.female.html", ...
Depending on the scenario this can be easier to extend and possibly contains less duplication.
  • Tag usage example

    <f:switch expression="[mixed]">
    	<!-- tag content - may be ignored! -->
    </f:switch>
  • Inline usage example

    {f:switch(expression: [mixed])}

Arguments

Argument Description Type
expression mixed
Required attribute

Revision history

commit fe22a97f6b371a3b491f65294703840cdf1a8ecf
Author: Wouter Wolters <*****@wouterwolters.nl>
Date:   Wed May 29 19:06:33 2013 +0200

    [FEATURE] Switch View Helper
    
    Simple view helper that allows you to render content depending
    on a given value or expression.
    It bahaves similar to a basic switch statement in PHP.
    
    Usage:
    
    <f:switch expression="{person.gender}">
      <f:case value="male">Mr.</f:case>
      <f:case value="female">Mrs.</f:case>
    </f:switch>
    
    Change-Id: I44a7066dfa86785f795069e0f06391cb45fa373c
    Resolves: #48653
    Releases: 6.2
    Reviewed-on: https://review.typo3.org/21155
    Reviewed-by: Alexander Schnitzler
    Tested-by: Alexander Schnitzler
    Reviewed-by: Jigal van Hemert
    Tested-by: Jigal van Hemert
    Reviewed-by: Felix Kopp
    Tested-by: Felix Kopp

commit 77f29a3c69a7cbb27b34072754aa503bace6c9f1
Author: Christian Kuhn <*****@schwarzbu.ch>
Date:   Tue Oct 1 14:40:56 2013 +0200

    [TASK] Remove closing PHP tags
    
    Change-Id: Iaa92566c53301e49396fc9fb26b0b339c48d567b
    Resolves: #52360
    Releases: 6.2
    Reviewed-on: https://review.typo3.org/24212
    Reviewed-by: Christian Kuhn
    Tested-by: Christian Kuhn
    Reviewed-by: Ernesto Baschny
    Tested-by: Ernesto Baschny
    Reviewed-by: Anja Leichsenring
    Tested-by: Anja Leichsenring

commit 8e022bcbea4f7775ce10fee30b4682a6047c461c
Author: Jan Kiesewetter <*****@t3easy.de>
Date:   Sun Oct 27 11:22:44 2013 +0100

    [FEATURE] Fallback/default case for SwitchViewhelper
    
    In order to provide the full functionality known from the
    switch/case PHP function, a default case possibility is
    introduced. Use it like:
    <f:switch expression="{person.gender}">
      <f:case value="female">Mrs.</f:case>
      <f:case value="male">Mr.</f:case>
      <f:case default="TRUE">Mrs. or Mr.</f:case>
    </f:switch>
    
    Resolves: #49371
    Documentation: #54283
    Change-Id: I6b71ec39173ab957aa392bd595a65ceddadc81c9
    Releases: 6.2
    Reviewed-on: https://review.typo3.org/23739
    Reviewed-by: Cedric Ziel
    Tested-by: Cedric Ziel
    Reviewed-by: Anja Leichsenring
    Tested-by: Anja Leichsenring

commit 75fb62def665bbc96fd9e9873a19022c2e3dd8b5
Author: Anja Leichsenring <*****@ab-softlab.de>
Date:   Sun Dec 8 16:29:31 2013 +0100

    Revert "[FEATURE] Fallback/default case for SwitchViewhelper"
    
    This reverts commit 8e022bcbea4f7775ce10fee30b4682a6047c461c
    
    Merged a Feature after Feature Freeze is not acceptable. Sorry. Patch is pushed new but will not be available in 6.2.
    
    Change-Id: I39baa58c70b0e942d01c1c37bbf793b873db385d
    Reviewed-on: https://review.typo3.org/26055
    Reviewed-by: Anja Leichsenring
    Tested-by: Anja Leichsenring

commit 3f6b1cd06425dbfb4c686d382ec1e77eb05bc842
Author: Jan Kiesewetter <*****@t3easy.de>
Date:   Sun Oct 27 11:22:44 2013 +0100

    [FEATURE] Fallback/default case for SwitchViewhelper
    
    In order to provide the full functionality known from the
    switch/case PHP function, a default case possibility is
    introduced. Use it like:
    <f:switch expression="{person.gender}">
      <f:case value="female">Mrs.</f:case>
      <f:case value="male">Mr.</f:case>
      <f:case default="TRUE">Mrs. or Mr.</f:case>
    </f:switch>
    
    Change-Id: Ib9468cdabd66fb8b484cb4d07520d5b529548dd1
    Resolves: #49371
    Documentation: #54283
    Releases: 6.2
    Reviewed-on: https://review.typo3.org/26058
    Reviewed-by: Benjamin Kott
    Tested-by: Benjamin Kott
    Reviewed-by: Anja Leichsenring
    Tested-by: Anja Leichsenring