Using a condition to check values

This TypoScript snippet shows a condition that checks the value of a variable. The further processing is based on the value of the variable.

In some cases you need to display or hide content based on some condition. This can easily be done by putting the content into a condition block. Then you check the value of a so-called flag, i.e. variables with a value of either 0 (off/no/false) or 1 (on/yes/true) in the condition.

Suppose you want to display a text only if the variable show_content is true (has a value greater than zero). In the CONSTANTS field of your TypoScript template you just define a variable and set it to a default value (here: 1).

Define a variable TypoScript, CONSTANTS section
# Set variable 'show-content' to a default value of 1
show_content = 1
Check the variable in a condition TypoScript, SETUP section
temp.some_text = TEXT
[globalVar = LIT:0<{$show_content}]
# Assign a string to the text object, if condition is true
temp.some_text.value = The variable is greater than zero
# Empty text object, if condition is false
temp.some_text >

The condition checks, if our variable show_content is greater than the (literal, LIT in TypoScript) value 0 (zero). If this condition is true — which is the default in our example — the text object is filled with content. Otherwise (the else case), the text object is emptied.

Remember that such conditions generally have to be written outside of any statement blocks (i.e. outside of curly brackets).

Please do also read our Best Practice article about constants in TypoScript.