TYPO3: How do I set conditional values in Site Handling?

Viewed 48

I have a system with two different domains in it: On for production and one for development. In Site Handling I am using a condition to differentiate the two.

How can I access the name of the current domain in my TypoScript setup? I want to put the domain name in the middle of a text string, which is later output in the website.

I tried defining the domain in the settings: section of config.yaml. But how do I change it depending on the system I am on? I did not find a way to use a condition there. Is there a recommended way to achieve what I am looking for?

2 Answers

A simple approach would be this:

[request.getNormalizedParams().getHttpHost() == "domain-1.com"]
    lib.myText = TEXT
    lib.myText.value = This site is running on domain-1.com
[request.getNormalizedParams().getHttpHost() == "domain-2.com"]
    lib.myText = TEXT
    lib.myText.value = This site is running on domain-2.com
[global]

The conditions are listed and explained here:
https://docs.typo3.org/m/typo3/reference-typoscript/11.5/en-us/Conditions/Index.html

Another option is to just use the old options of TypoScript to get the current host:

page = PAGE
page.10 = TEXT
page.10.stdWrap.data = getIndpEnv : HTTP_HOST
page.10.stdWrap.htmlSpecialChars = 1
page.10.stdWrap.noTrimWrap = |This site is running on ||

The available $_SERVER variables are listed and explained here:
https://docs.typo3.org/m/typo3/reference-typoscript/11.5/en-us/Functions/Data.html#getindpenv

Another option is to use environment variables. So you have per instance (production and development) an .env file in which you define a variable like

.env for production

CUSTOM_DOMAIN_VALUE="my-production-domain.com"

.env for development

CUSTOM_DOMAIN_VALUE="my-development-domain.com"

This environment variable could you use in the config.yaml with '%env(CUSTOM_DOMAIN_VALUE)%'. The TypoScript condition to switch between the value is not necessary.

To work with environment you could use the package Symfony dotenv or the package from Helmut Hummel Dotenv-Connector

Related