Conditions and the if statement can be used to control TypoScript depending on variables. There are so many variables that you can use them to solve almost any task.
We have compiled examples of various conditions on this page.
The conditions are each enclosed in square brackets [...]; when constructed with if, the respective object only becomes valid if the condition is fulfilled.
For conditions to work, the following must be observed:
- Each condition must end with the line [global]. If this is forgotten, the following TypoScript code will be interpreted differently than intended
- Conditions must be outside of {...} brackets. Example:
## Incorrect example!
page.15 = TEXT
page.15 {
value = Hello User
[backend.user.isAdmin]
value = Hello Admin
[global]
}
This condition does not work because it is inside {...}.
Instead you have to write it like this:
page.15 = TEXT page.15.value = Hello User [backend.user.isAdmin] page.15.value = Hello Admin [global]
Weekdays
A different template should be used on Thursdays than on the other days of the week:
config.cache_clearAtMidnight = 1
page.10.template.file = fileadmin/normal_template.html
[dayofweek = 4] # 0 = Sunday, 1 = Monday, ...
page.10.template.file = fileadmin/thursday_template.html
[global]
With config.cache_clearAtMidnight you ensure that the page is regenerated the first time it is called up after midnight (normally pages are regenerated every 24 hours and stored in the cache).
Changes for a single page only
If you make changes with TypoScript in the template (or extension template), these apply to the current page and all subpages.
However, if you only want to make changes for a single page (without the subpages), this is possible with a condition. In this case, the global variable TSFE:id (this is the page ID) is used:
[globalVar = TSFE:id=1234]
// here is the code that is only valid for page 1234
[global]
Alternatively, the variable TSFE:page|pid can be used instead of TSFE:id. If the condition is to apply to several pages (OR link), several conditions are separated by commas:
[globalVar = TSFE:id=10, TSFE:id=20, TSFE:page|pid=30]
colPos: Different configuration depending on column
If you want different formatting depending on the column, the easiest way to achieve this is via CSS by placing a div tag around each column.
It is a little more difficult if you want to make changes in the TypoScript depending on the column (colPos).
An example: Each link in the text should be preceded or followed by a small graphic. However, the graphic should be different in each content column.
Inserting a graphic before a text link is achieved with this TypoScript code:
tt_content.text.20.parseFunc.tags.link.prepend = IMAGE
tt_content.text.20.parseFunc.tags.link.prepend.file = fileadmin/images/image.gif
To use a different graphic for each column, a little trick is used: a temporary COA (Content Object Array) is created, i.e. a content object that in turn contains other objects. The if function is then used to delete the objects that should not appear in the current column:
temp.linkimage = COA
temp.linkimage.10 = IMAGE
temp.linkimage.10.file = fileadmin/images/image1.gif
temp.linkimage.10.if {
value = 0
equals.field = colPos
}
temp.linkimage.20 = IMAGE
temp.linkimage.20.file = fileadmin/images/image2.gif
temp.linkimage.20.if {
value = 1
equals.field = colPos
}
temp.linkimage.30 = IMAGE
temp.linkimage.30.file = fileadmin/images/image3.gif
temp.linkimage.30.if {
value = 2
equals.field = colPos
}
tt_content.text.20.parseFunc.tags.link.prepend < temp.linkimage
In the last line, the temporary COA is then copied into the text link object. Although three image elements have been defined, the COA ultimately only contains one graphic, as only one of the three if conditions is fulfilled.
If you want the additional graphic after instead of before a link, replace "prepend" with "append" in the code.
This page contains automatically translated content.