typo3typoscripttypo3-8.7.x

Make multiple if conditions in TypoScript


I want to check two field on a value to wrap each content element.

To check one value, you can do something like this:

20 = TEXT
20.value = <div class="div1">|</div>
20.value.override = <div class="div2">|</div>
20.value.override.if.value = 10
20.value.override.if.equals.field = colPos

What do I have to add to check a second value like this?

20 = TEXT
20.value = <div class="div1">|</div>
20.value.override = <div class="div2">|</div>
20.value.override.if.value = 10
20.value.override.if.equals.field = colPos
20.value.override.andIf.value = textmedia
20.value.override.andIf.equals.field = CType

So the content element should only been wrapped with div2 if the CType is textmedia AND the colPos is 10. Otherwise div1 should be used.


Solution

  • You can combine values using dataWrap and compare them within a single if:

    20 = TEXT
    20.value = <div class="div1">|</div>
    20.value.override = <div class="div2">|</div>
    20.value.override.if.value = 10textmedia
    20.value.override.if.equals.dataWrap = {field:colPos}{field:CType}
    

    When you want to go for more values having different results depending on the combination, using a CASE object might be the better approach:

    20 = CASE
    20.key.dataWrap = {field:myfield1}{field:myfield2}{field:myfield3}
    20.default = TEXT
    20.default.value = <div class="div1">|</div>
    20.001 = TEXT
    20.001.value = <div class="div001">|</div>
    20.101 = TEXT
    20.101.value = <div class="div101">|</div>
    

    To separate your values when they can contain more than just 0 or 1, you should add a separator like x within the dataWrap.

    20.key.dataWrap = {field:myfield1}x{field:myfield2}x{field:myfield3}