Skip to content

Commit 369daf1

Browse files
[FEATURE] Restructure conditions docs (#1573)
Improve headlines and structure, move examples to typoscript files or inline Releases: main, 13.4, 12.4 Co-authored-by: lina.wolf <lwolf@w-commerce.de>
1 parent 886b979 commit 369daf1

File tree

12 files changed

+182
-185
lines changed

12 files changed

+182
-185
lines changed

Documentation/CodeSnippets/TypoScriptSyntax/Conditions1.rst.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

Documentation/CodeSnippets/TypoScriptSyntax/Conditions2.rst.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

Documentation/CodeSnippets/TypoScriptSyntax/Conditions3.rst.txt

Lines changed: 0 additions & 16 deletions
This file was deleted.

Documentation/CodeSnippets/TypoScriptSyntax/Conditions4.rst.txt

Lines changed: 0 additions & 19 deletions
This file was deleted.

Documentation/CodeSnippets/TypoScriptSyntax/Conditions5.rst.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

Documentation/Conditions/Index.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
:navigation-title: Conditions
2+
13
.. include:: /Includes.rst.txt
24
.. index::
35
Conditions
@@ -12,10 +14,9 @@
1214
.. _condition-functions-in-all-contexts:
1315
.. _condition-functions-in-frontend-context:
1416

15-
16-
==========
17-
Conditions
18-
==========
17+
=======================================
18+
Frontend TypoScript conditions criteria
19+
=======================================
1920

2021
Frontend TypoScript conditions offer a way to conditionally change TypoScript
2122
based on current context. Do not confuse conditions with the
Lines changed: 136 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1-
.. include:: /Includes.rst.txt
2-
.. index::
3-
TypoScript; Operator "["
4-
TypoScript; Conditions
5-
TypoScript; [GLOBAL] condition
6-
TypoScript; Symfony expression language
7-
.. _typoscript-syntax-conditions:
8-
.. _typoscript-syntax-syntax-square-brackets:
9-
.. _typoscript-syntax-syntax-conditions:
10-
.. _typoscript-syntax-syntax-value:
11-
.. _typoscript-syntax-else-condition:
12-
.. _typoscript-syntax-end-condition:
13-
.. _typoscript-syntax-global-condition:
14-
.. _typoscript-syntax-conditions-expression-language:
15-
.. _typoscript-syntax-implementing-custom-conditions:
16-
17-
18-
==========
19-
Conditions
20-
==========
1+
:navigation-title: Conditions
2+
3+
.. include:: /Includes.rst.txt
4+
.. index::
5+
TypoScript; Operator "["
6+
TypoScript; Conditions
7+
TypoScript; [GLOBAL] condition
8+
TypoScript; Symfony expression language
9+
.. _typoscript-syntax-conditions:
10+
.. _typoscript-syntax-implementing-custom-conditions:
11+
.. _typoscript-syntax-conditions-examples:
12+
13+
===================================
14+
Conditions in the TypoScript syntax
15+
===================================
2116

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

26-
- Is a user logged in?
27-
28-
- Is it Monday?
29-
30-
- Is the page called in a certain language?
21+
- Is a user logged in?
22+
- Is it Monday?
23+
- Is the page called in a certain language?
3124

3225
Conditions are a TypoScript syntax construct. They are thus available in both
3326
frontend TypoScript and backend TSconfig. However, condition criteria are based
@@ -36,129 +29,164 @@ TypoScript and backend TSconfig. For example, the :typoscript:`frontend` variabl
3629
not exist in TSconfig, it is (obviously) impossible to have a backend TSconfig
3730
condition that checks for a logged in frontend user.
3831

39-
For a reference of allowed condition criteria, please refer to the according
40-
chapter in the :ref:`frontend TypoScript Reference <conditions>` and
41-
the :ref:`backend TSconfig Reference <tsconfig-conditions>`. These references
42-
come with examples for single condition criteria as well.
32+
.. contents:: Table of contents
4333

44-
The TSconfig and TypoScript backend modules show lists of existing conditions
45-
and allow simulating criteria verdicts to analyze their impact on the
46-
resulting TypoScript tree.
34+
.. _typoscript-syntax-conditions-usage:
4735

48-
Condition criteria are based on
49-
the `Symfony expression language <https://symfony.com/doc/current/components/expression_language/syntax.html>`__.
50-
The Core allows extending the Symfony expression language with own variables and
51-
functions, see :ref:`symfony expression language API <t3coreapi:sel-within-typoscript-conditions>`
52-
for more details.
36+
Basic usage of TypoScript conditions
37+
====================================
5338

39+
.. literalinclude:: _codesnippets/_basic.typoscript
40+
:captions: packages/my_site_package/Configuration/TypoScript/setup.typoscript
5441

55-
Basic example
56-
=============
42+
.. _typoscript-syntax-conditions-syntax:
43+
44+
Syntax and rules
45+
================
5746

58-
.. include:: /CodeSnippets/TypoScriptSyntax/Conditions1.rst.txt
47+
These general rules apply:
5948

49+
.. _typoscript-syntax-conditions-confinements:
50+
.. _typoscript-syntax-conditions-braces:
51+
.. _typoscript-syntax-syntax-square-brackets:
52+
.. _typoscript-syntax-syntax-conditions:
6053

61-
.. _typoscript-syntax-conditions-syntax:
54+
General condition syntax
55+
------------------------
6256

63-
Syntax and rules
64-
================
57+
Conditions are encapsulated in :typoscript:`[` and :typoscript:`]`
6558

66-
The general syntax is like this:
59+
.. _typoscript-syntax-the-global-condition:
60+
.. _typoscript-syntax-global-condition:
61+
.. _typoscript-syntax-else-condition:
62+
.. _typoscript-syntax-end-condition:
6763

68-
.. include:: /CodeSnippets/TypoScriptSyntax/Conditions2.rst.txt
64+
`[GLOBAL]`, `[ELSE]` and `[END]`
65+
--------------------------------
6966

70-
These general rules apply:
67+
:typoscript:`[ELSE]` negates a previous condition criteria and can contain
68+
a new body until :typoscript:`[END]` or :typoscript:`[GLOBAL]`. :typoscript:`[ELSE]`
69+
is considered if the condition criteria did *not* evaluate to true.
7170

72-
* Conditions are encapsulated in :typoscript:`[` and :typoscript:`]`
71+
:typoscript:`[END]` and :typoscript:`[GLOBAL]` stop a given condition scope.
72+
This is similar to a closing curly brace :code:`}` in programming languages like PHP.
7373

74-
* :typoscript:`[ELSE]` negates a previous condition criteria and can contain
75-
a new body until :typoscript:`[END]` or :typoscript:`[GLOBAL]`. :typoscript:`[ELSE]`
76-
is considered if the condition criteria did *not* evaluate to true.
74+
.. literalinclude:: _codesnippets/_condition.typoscript
75+
:captions: packages/my_site_package/Configuration/TypoScript/setup.typoscript
7776

78-
* :typoscript:`[END]` and :typoscript:`[GLOBAL]` stop a given condition scope.
79-
This is similar to a closing curly brace :code:`}` in programming languages like PHP.
77+
Conditions automatically stop at the end of a text snippet (file or record), even
78+
without :typoscript:`[END]` or :typoscript:`[GLOBAL]`. Another snippet on the same
79+
level is in "global" scope automatically. The backend TypoScript and
80+
TSconfig modules may mumble about a not properly closed condition, though.
8081

81-
* Multiple condition criteria can be combined using :typoscript:`or` or :typoscript:`||`,
82-
as well as :typoscript:`and` or :typoscript:`&&`
82+
.. versionchanged:: 12.0
8383

84-
* Single criteria can be negated using :typoscript:`!`
84+
:typoscript:`[END]` and :typoscript:`[GLOBAL]` behave exactly the same. Both
85+
are kept for historical reasons (for now).
8586

86-
.. versionchanged:: 12.0
87+
Conditions automatically stop at the end of a text snippet (file or record).
8788

88-
:typoscript:`[END]` and :typoscript:`[GLOBAL]` behave exactly the same. Both
89-
are kept for historical reasons (for now).
89+
.. _typoscript-syntax-end-condition-and:
90+
.. _typoscript-syntax-end-condition-or:
9091

91-
* Conditions can use constants. They are available in frontend TypoScript "setup" and
92-
in TSconfig from "site settings". A simple example if this constant
93-
:typoscript:`myPageUid = 42` is set:
92+
Combining multiple TypoScript conditions with `and` or `or`
93+
-----------------------------------------------------------
9494

95-
.. code-block:: typoscript
95+
Multiple condition criteria can be combined using :typoscript:`or` or :typoscript:`||`,
96+
as well as :typoscript:`and` or :typoscript:`&&`
9697

97-
[traverse(page, "uid") == {$myPageUid}]
98-
page.10.value = Page uid is 42
99-
[end]
98+
.. literalinclude:: _codesnippets/_andor.typoscript
99+
:captions: packages/my_site_package/Configuration/TypoScript/setup.typoscript
100100

101-
* Conditions can *not* be nested within code blocks.
101+
Single criteria can be negated using :typoscript:`!`
102102

103-
.. versionchanged:: 12.0
103+
.. _typoscript-syntax-end-condition-constants:
104104

105-
Conditions *can* be nested into each other, if they are located in
106-
different snippets (files or records), see example below. They can *not* be nested
107-
within the same code snippet.
105+
TypoScript constant usage in Conditions
106+
---------------------------------------
108107

109-
* A second condition that is *not* :typoscript:`[ELSE]`, :typoscript:`[END]`
110-
or :typoscript:`[GLOBAL]` *stops* a previous condition and starts a new one.
111-
This is the main reason conditions can *not* be nested within one text snippet.
108+
Conditions can use constants. They are available in frontend TypoScript "setup" and
109+
in TSconfig from "site settings". A simple example if this constant
110+
:typoscript:`myPageUid = 42` is set:
112111

113-
* .. versionchanged:: 12.0
112+
.. code-block:: typoscript
114113
115-
:typoscript:`@import` *can* be nested
116-
inside conditions. This allows conditional includes and is a new feature of the
117-
TYPO3 v12 parser.
114+
[traverse(page, "uid") == {$myPageUid}]
115+
page.10.value = Page uid is 42
116+
[end]
118117
119-
* .. versionchanged:: 12.0
118+
.. _typoscript-syntax-end-condition-nesting:
120119

121-
Conditions automatically stop at the end of a text snippet (file or record), even
122-
without :typoscript:`[END]` or :typoscript:`[GLOBAL]`. Another snippet on the same
123-
level is in "global" scope automatically. The backend TypoScript and
124-
TSconfig modules may mumble about a not properly closed condition, though.
120+
Nesting conditions via TypoScript imports
121+
-----------------------------------------
125122

126-
* .. versionadded:: 12.1
123+
Conditions can *not* be nested within code blocks.
127124

128-
Using the null-safe operator is possible when accessing properties on objects
129-
which might not be available in some context, for example `TSFE` in the
130-
backend:
125+
.. code-block:: typoscript
131126
132-
.. code-block:: typoscript
127+
# Invalid: Conditions must not be used within code blocks
128+
# someIdentifier {
129+
# someProperty = foo
130+
# [frontend.user.isloggedIn]
131+
# someProperty = bar
132+
# [GLOBAL]
133+
# }
133134
134-
# Previously
135-
[getTSFE() && getTSFE().id == 123]
135+
.. versionchanged:: 12.0
136136

137-
# Now
138-
[getTSFE()?.id == 123]
137+
Conditions *can* be nested into each other, if they are located in
138+
different snippets (files or records), see example below. They can *not* be nested
139+
within the same code snippet.
139140

141+
A second condition that is *not* :typoscript:`[ELSE]`, :typoscript:`[END]`
142+
or :typoscript:`[GLOBAL]` *stops* a previous condition and starts a new one.
143+
This is the main reason conditions can *not* be nested within one text snippet.
140144

141-
.. _typoscript-syntax-conditions-usage:
142-
.. _typoscript-syntax-conditions-combine:
143-
.. _typoscript-syntax-conditions-confinements:
144-
.. _typoscript-syntax-conditions-braces:
145-
.. _typoscript-syntax-the-global-condition:
146-
.. _typoscript-syntax-conditions-examples:
145+
:typoscript:`@import` *can* be nested
146+
inside conditions. This allows conditional includes and is a new feature of the
147+
TYPO3 v12 parser.
147148

148-
Examples
149-
========
149+
.. _typoscript-syntax-end-condition-null-save:
150150

151-
* If a user is logged in, or if the client is local, text will be
152-
output in upper case:
151+
Using the null-safe operator in conditions
152+
------------------------------------------
153153

154-
.. include:: /CodeSnippets/TypoScriptSyntax/Conditions3.rst.txt
154+
.. versionadded:: 12.1
155155

156-
* In case :code:`if` is empty and only a :code:`else` body is needed for a
157-
single condition criteria, these two are identical:
156+
Using the null-safe operator is possible when accessing properties on objects
157+
which might not be available in some context, for example `TSFE` in the
158+
backend:
158159

159-
.. include:: /CodeSnippets/TypoScriptSyntax/Conditions4.rst.txt
160+
.. code-block:: typoscript
160161
161-
* Conditions can *not* be nested within curly braces. The example below
162-
is invalid syntax and the backend modules mumble with "missing braces":
162+
# Previously
163+
# [getTSFE() && getTSFE().id == 123]
163164
164-
.. include:: /CodeSnippets/TypoScriptSyntax/Conditions5.rst.txt
165+
# Now
166+
[getTSFE()?.id == 123]
167+
168+
169+
.. _typoscript-syntax-syntax-value:
170+
171+
Allowed criteria in TypoScript conditions
172+
=========================================
173+
174+
For a reference of allowed condition criteria, please refer to the according
175+
chapter in the :ref:`frontend TypoScript Reference <conditions>` and
176+
the :ref:`backend TSconfig Reference <tsconfig-conditions>`. These references
177+
come with examples for single condition criteria as well.
178+
179+
The TSconfig and TypoScript backend modules show lists of existing conditions
180+
and allow simulating criteria verdicts to analyze their impact on the
181+
resulting TypoScript tree.
182+
183+
.. _typoscript-syntax-conditions-expression-language:
184+
185+
TypoScript conditions and the Symfony expression language
186+
=========================================================
187+
188+
Condition criteria are based on
189+
the `Symfony expression language <https://symfony.com/doc/current/components/expression_language/syntax.html>`__.
190+
The Core allows extending the Symfony expression language with own variables and
191+
functions, see :ref:`symfony expression language API <t3coreapi:sel-within-typoscript-conditions>`
192+
for more details.

0 commit comments

Comments
 (0)