Fluid: if - then - elseif - else
Asked Answered
H

3

8

I'm a bit new to fluid and I want to make a the following php statement in Fluid.

if ($var == 'something') {
   // do something
} elseif ($other-var == 'something else') {
   // do something else
} else {
   // do then the other thin
}

How can I make this in Fluid? I don't see the elseif statement in the documentation.

Hardan answered 5/4, 2015 at 19:34 Comment(0)
S
26

Since TYPO3 8LTS

Since version 8 TYPO3 uses the standalone version of fluid, that was heavily developed and got tons of new features like elseif:

<f:if condition="{var} == 'something'">
    <f:then>do something</f:then>
    <f:else if="{other-var} == 'something else'">do something else</f:else>
    <f:else>do the other thing</f:else>
</f:if>

In addition there is support for syntax like this:

<f:if condition="{something} || {someOtherThing}">
    Something or someOtherThing
</f:if>

Until and including TYPO3 7LTS

With Plain Fluid you can nest two if ViewHelper:

<f:if condition="{var} == 'something'">
    <f:then>
       // do something
    </f:then>
    <f:else>
        <f:if condition="{other-var} == 'something else'">
            <f:then>
                // do something else
            </f:then>
           <f:else>
               // do then the other thing
           </f:else>
        </f:if>
    </f:else>
</f:if>

Or you could implement your own ViewHelper or use a ViewHelper Library like VHS that have a ViewHelper that does this more elegant.

Scheck answered 5/4, 2015 at 20:11 Comment(1)
In TYPO3 8 ensure, that {other-var} in f:else if="{other-var}" don't contains a string with calculations like '100% foo - 0% bar'. This will fx end in a Uncaught TYPO3 Exception Error 'Modulo by zero'Min
N
0

There's a possibility for AND and OR in Typo3 7 LTS:

http://typo3blogger.de/fluid-ifviewhelper-conditions-verknupfen/

AND:

<f:if condition="{0: value1, 1: value2} == {0: myValue1, 2: myValue2}"> 
    <!-- your code-->
</f:if>

OR (in the example for checking that at least one variable is not null):

<f:if condition="{0: value1, 1: value2} != {0: 0, 2: 0}"> 
    <!-- your code-->
</f:if>

Hope that helps someone.

Nude answered 7/12, 2017 at 11:17 Comment(0)
R
0

Is there an inline notation für 'else if'? This one is not working:

{f:if(condition: '{value} == 1', then: 'Value is 1', else if: '{value} == 2', then:'Value is 2')}
Redevelop answered 16/3, 2022 at 9:22 Comment(1)
{f:if(condition: '{condition}', then: 'true', else: 'false')}Acton

© 2022 - 2024 — McMap. All rights reserved.