Using TypoScript constants
Motivation
In a TypoScript template, constants are defined in the CONSTANTS section and can then be used in the SETUP section. Because of the principle of inheritance in TypoScript, this also works across multiple templates. Constants are very useful when it comes to keeping TypoScript code flexible.
Example
The syntax for the definition and the usage of constants is always the same: In the CONSTANTS section of the template you need to give the constant a name and and assign a value to it:
# 'baseUrl' stores the domain of the web site baseUrl = http://www.jochen-froehlich.de/ # 'id_homepage' stores the UID of the homepage id_homepage = 2
In the SETUP section of any template below the template in which the constants are defined, these values can be used.
Suppose our two constants 'baseUrl' and 'id_homepage' were defined in the TypoScript template of the top-level page. Then the values can be used in any template in the underlying page structure (tree of subpages).
The embedding of a constant in the SETUP section is as follows:
# Embedding constant 'baseUrl' in a meta tag (style sheet with absolute URL) page = PAGE page.headerData.10 = TEXT page.headerData.10.value = <link rel="stylesheet" href="{$baseUrl}/fileadmin/css/style.css" type="text/css" /> # Using constant 'id_homepage' in a condition [globalVar = TSFE:id={$id_homepage}] # process this code only when on the homepage [global]
Best Practice
How can these two constants be used in real life?
Suppose there are two TYPO3 systems: First, a live system, which is maintained by editors and grows steadily. Second, a test system that runs on a different domain. The page UIDs are differing over time because new pages are created only on the live system.
By using our two constants, it is now possible to store the respective values in separate CONSTANTS sections (one on the test system and the other on the live system). Both systems can use the same SETUP section though.
The constant 'baseUrl' makes it very easy to move a system to another domain: the domain name needs to be changed only in one single place.
Of course, not only the homepage UID can be made a constant, but the UIDs of other pages as well. This can be very handy for TypoScript conditions, the 'special' property of menus or target pages for links.
Constants are also very useful when using translations in TypoScript.
Using a constant as a variable
Instead of the term "constant" the term "variable" would fit better, since the value of a constant can be changed in templates on each page. This, for example, can be used to show or hide a content element on certain pages by using the constant's (resp. variable's) value in a TypoScript condition:
# Do not show an element by default # This definition is in the root template on the topmost page show_element = 0
# Show the element, if 'show_element' has a value greater than 0 (zero) # This condition is part of the root template on the topmost page [globalVar = LIT:0<{$show_element}] 10 = TEXT 10.value = ...the content element... [global]
The information in the root template's SETUP section is inherited to all pages on lower levels. This enables you to do the following: Create a template on any subpage and assign the value 1 to the 'show_element' constant/variable in its CONSTANTS section. Now the content element inside the condition block is shown on this page (and all its subpages). Once you set the value of 'show_element' back to 0 (zero), the content element will be hidden.
Read on
Be the first to comment this article!
Visitors found this page by searching for these keywords: