TYPO3: How to use TypoScript Conditions
A TypoScript condition is a powerful and easy way of telling TYPO3 when to execute a piece of TypoScript. In this tutorial I’ll explain the basics of such a condition and how to use one. Conditions are very similar to a PHP if-statement and should be easy to use if you understand the principles of PHP if, elseif and else.
A condition always consists of 2 brackets with a statement in the middle:
[your-statement]
Multiple conditions can be combined by using so called operators: && (AND) or || (OR), if no operator is defined the default will be OR.
Example 1: Load ie.css only when the browser is IE
[browser = msie]
page.includeCSS {
1 = fileadmin/ie.css
}
[end]
Example 2: Load ie6.css only when the browser is IE and the version is lower than 7
[browser = msie] && [version = <7]
page.includeCSS {
1 = fileadmin/ie6.css
}
[end]
Example 3: Load ie stylesheet when the browser is IE, load normal stylesheet with all other browsers
[browser = msie] && [version = <7]
page.includeCSS {
1 = fileadmin/ie.css
}
[else]
page.includeCSS {
1 = fileadmin/style.css
}
[end]
Example 4: Use the time to accordingly greet your visitor
# Display 'Good Morning before 12 o'clock
[hour =< 12]
lib.greeting = TEXT
lib.greeting {
value = Good Morning!
}
[else]
# Display 'Good Day' after 12 o'clock
lib.greeting = TEXT
lib.greeting {
value = Good Day!
}
[end]
Let’s make things a little bit more interesting.
Example 5: We want to include some records (from page id 250) in our left_column div but only on page id 100 or a subpage of this page!
# First we define the content of the 'left_column' div:
left_column = COA
left_column {
10 < lib.regularContent
20 < lib.specialContent
}
# Display the lib.specialContent only on page id 100 or a subpage
[PIDinRootline = 100]
lib.specialContent = CONTENT
lib.specialContent {
table = tt_content
select {
pidInList = 250
orderBy = sorting
languageField = sys_language_uid
}
}
[end]
I could make a hundred examples but when you know to basics it isn’t that hard. If you want to know more about these conditions please read chapter 4 of the TSRef:
http://typo3.org/documentation/document-library/references/doc_core_tsref/4.1.0/view/4/1/
If you have any questions don’t hesitate to ask!
