Jonas
Jonas

Reputation: 379

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.

Upvotes: 2

Views: 5356

Answers (2)

Paul Beck
Paul Beck

Reputation: 2685

Nesting of Typoscript "if"-conditions is possible by using the stdWrap-properties of the conjunctions. In your case the docs says, conditions are connected with an AND conjunction. So you just need to use the stdWrap-properties of another condition (lets take isTrue) and make it an TEXT object by using the cObject property of stdWrap. Then you can place another "equals" condition inside the cObject which is returning "1" to isTrue if the condition is met.

Try it like this, not tested

20 = TEXT
20 {
    value = <div class="div1">|</div>
    value {
        override = <div class="div2">|</div>
        override.if {
            # Condition 1
            value = 10
            equals.field = colPos
            # Another "equals" condition nested in isTrue using cObject
            isTrue.cObject = TEXT
            isTrue.cObject {
                value = 1
                if.value = textmedia
                equals.field = CType
            }
        }
    }
}

Creating an OR-condition in Typocript is also possible. You can convert the isTrue property to an COA holding multiple TEXT objects which are returning something like 1 if their conditions are met.

Example for creating an OR:

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

Some helpful docs about this:

https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/If.html

https://docs.typo3.org/typo3cms/TyposcriptReference/Functions/Stdwrap.html

Upvotes: 3

Jo Hasenau
Jo Hasenau

Reputation: 2684

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}

Upvotes: 4

Related Questions