Conditions in the TypoScript syntax

TypoScript can contain if and if / else control structures. They are called conditions, their "body" is only considered if a condition criteria evaluates to true. Examples of condition criteria are:

  • Is a user logged in?
  • Is it Monday?
  • Is the page called in a certain language?

Conditions are a TypoScript syntax construct. They are thus available in both frontend TypoScript and backend TSconfig. However, condition criteria are based on prepared variables and functions, and those are different in frontend TypoScript and backend TSconfig. For example, the frontend variable does not exist in TSconfig, it is (obviously) impossible to have a backend TSconfig condition that checks for a logged in frontend user.

Basic usage of TypoScript conditions

page = PAGE
page.10 = TEXT
page.10.value = HELLO WORLD!

[frontend.user.isLoggedIn]
  page.20 = TEXT
  page.20 {
    value = A frontend user is logged in.
  }
[GLOBAL]
Copied!

Syntax and rules

These general rules apply:

General condition syntax

Conditions are encapsulated in [ and ]

[GLOBAL], [ELSE] and [END]

[ELSE] negates a previous condition criteria and can contain a new body until [END] or [GLOBAL]. [ELSE] is considered if the condition criteria did not evaluate to true.

[END] and [GLOBAL] stop a given condition scope. This is similar to a closing curly brace } in programming languages like PHP.

[date("j") == 9]
  page.10.value = It is the 9th day of the month!
[ELSE]
  page.10.value = It is NOT the 9th day of the month!
[END]
Copied!

Conditions automatically stop at the end of a text snippet (file or record), even without [END] or [GLOBAL]. Another snippet on the same level is in "global" scope automatically. The backend TypoScript and TSconfig modules may mumble about a not properly closed condition, though.

Changed in version 12.0

[END] and [GLOBAL] behave exactly the same. Both are kept for historical reasons (for now).

Conditions automatically stop at the end of a text snippet (file or record).

Combining multiple TypoScript conditions with and or or

Multiple condition criteria can be combined using or or ||, as well as and or &&

[frontend.user.isLoggedIn || ip('127.0.0.1')]
  page.20 = TEXT
  page.20 {
    value = A frontend user is logged in, or the browser IP is 127.0.0.1
    stdWrap.case = upper
  }
[GLOBAL]
Copied!

Single criteria can be negated using !

TypoScript constant usage in Conditions

Conditions can use constants. They are available in frontend TypoScript "setup" and in TSconfig from "site settings". A simple example if this constant myPageUid = 42 is set:

[traverse(page, "uid") == {$myPageUid}]
    page.10.value = Page uid is 42
[end]
Copied!

Nesting conditions via TypoScript imports

Conditions can not be nested within code blocks.

# Invalid: Conditions must not be used within code blocks
# someIdentifier {
#    someProperty = foo
#    [frontend.user.isloggedIn]
#       someProperty = bar
#    [GLOBAL]
# }
Copied!

Changed in version 12.0

Conditions can be nested into each other, if they are located in different snippets (files or records), see example below. They can not be nested within the same code snippet.

A second condition that is not [ELSE], [END] or [GLOBAL] stops a previous condition and starts a new one. This is the main reason conditions can not be nested within one text snippet.

@import can be nested inside conditions. This allows conditional includes and is a new feature of the TYPO3 v12 parser.

Using the null-safe operator in conditions

New in version 12.1

Using the null-safe operator is possible when accessing properties on objects which might not be available in some context, for example TSFE in the backend:

# Previously
# [getTSFE() && getTSFE().id == 123]

# Now
[getTSFE()?.id == 123]
Copied!

Allowed criteria in TypoScript conditions

For a reference of allowed condition criteria, please refer to the according chapter in the frontend TypoScript Reference and the backend TSconfig Reference. These references come with examples for single condition criteria as well.

The TSconfig and TypoScript backend modules show lists of existing conditions and allow simulating criteria verdicts to analyze their impact on the resulting TypoScript tree.

TypoScript conditions and the Symfony expression language

Condition criteria are based on the Symfony expression language. The Core allows extending the Symfony expression language with own variables and functions, see symfony expression language API for more details.