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.
Table of contents
Basic usage of TypoScript conditions
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]
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
&&
# "or" is an alias for "||": one of the criteria is enough
[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]
# "and" is an alias for "&&": both criteria must apply
[frontend.user.isLoggedIn && siteLanguage("languageId") == 1]
page.30 = TEXT
page.30.value = A logged in frontend user browses the second site language
[GLOBAL]
Single criteria can be negated using
not
or
!
page = PAGE
page.10 = TEXT
page.10.value = You are logged in
# This is hard to read
[frontend.user.isLoggedIn]
[ELSE]
page.10.value = You are *not* logged in
[END]
# This is faster to read
[!frontend.user.isLoggedIn]
page.10.value = You are *not* logged in
[END]
As in most programming languages,
not
binds more strongly than
and
, which in turn binds more strongly than
or
. Use
parentheses whenever the intended grouping is not the default one:
# Reads as: isLoggedIn or (languageId == 1 and not ip('127.0.0.1'))
[frontend.user.isLoggedIn || siteLanguage("languageId") == 1 && !ip('127.0.0.1')]
page.40 = TEXT
page.40.value = Default grouping
[GLOBAL]
# Parentheses enforce a different grouping
[(frontend.user.isLoggedIn || siteLanguage("languageId") == 1) && !ip('127.0.0.1')]
page.50 = TEXT
page.50.value = Grouping enforced by parentheses
[GLOBAL]
Warning
All criteria of a condition belong into one pair of square brackets.
Chaining several bracketed conditions with
&&
or
| was the syntax used before the Symfony expression
language was introduced in TYPO3 v9. It has been removed since and now
fails silently: everything after the first closing bracket is treated
as a comment, so all criteria but the first one are ignored without any
error message.
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
my is set:
[traverse(page, "uid") == {$myPageUid}]
page.10.value = Page uid is 42
[end]
Note
Using the Null coalescing operator ?? with constants used within conditions is not possible.
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]
# }
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:
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.