Working around nested conditions in TypoScript


As you may know, nested conditions are not supported in TypoScript, nor is negating a condition. This can be really painful at times, so here is my way to workaround that problem. Unfortunately, it is not applicable to all situations, so if you’ve know of a better way to do this, please leave a comment :)

Negations can be expressed in TypoScript using the following workaround:

[PIDInRootLine = 42]
	# Does nothing
[else]
	# So this is ![PIDInRootLine = 42]
[end]

Not very elegant, I know. Also, this doesn’t work if you want to negate only a single condition of an expression, for example:

[PIDInRootLine = 42] && [usergroups = *]
	# Does nothing
[else]
	# This is !([PIDInRootLine = 42] && [usergroups = *])
[end]

But what if we want this:

[PIDInRootLine = 42] && ![usergroups = *]

I’ve worked around this limitation by writing two conditions based on the fact that, when I write a condition such as the former, I want to express something like “Do something if A and if not B”, which also can be expressed as “Do something if A. Then, if B, undo what you did.”. According to this translation, the former expression could be written like this:

page.10 = TEXT
page.10.value = 100

tempValue < page.10.value
[PIDInRootLine = 42]
	page.10.value = 42
[end]

[usergroups = *]
	page.10.value < tempValue
[end]

This changes page.10.value to 42, but only if [PIDInRootLine = 42] && ![usergroups = *]. Not very elegant either, but it did solve my problem and I hope it helps you solve yours too :)