简体   繁体   中英

How to use a typo3 constant in an href?

To make things easier, I can define the main url of my page as a constant in a template: tx_dti.settings.url = https:/someserver/

Now I like to use this constant in my text element to link an internal page eg:
<a href="{tx_dti.settings.url}?id=13" class="internal-link">Contact Form</a>

Unfortunately my above mentioned approach does not work.
What is the correct syntax to replace the server with a constant?
The goal is that I only want to change the constant when I move from a dev to a productive server.

in general:
You can not use typoscript constants outside typoscript templates (constants and setup). If you want to use the value of a typoscript constant in other context you need to transfer the value.

If you want to use the constant in fluid you need to transfer the value to a fluid variable.

temp.content = FLUIDTEMPLATE
temp.content {
    template = ...
    variables {
        domain = TEXT
        domain.value = {$myTSconstant}
        :
    }
}

or

settings { 
    domain = TEXT
    domain.value = {$myTSconstant}
    :
}

If you want to use the constant in a data field of a content element you might use a marker and replace the marker with typoscript as last step of rendering.

"<p>This is my text and here I use a marker: __MYMARKER__</p>"

the corresponding typoscript:

page {
    :

    stdWrap.replacement {
         1.search = __MYMARKER__
         1.replace = {$myTSconstant}
    }
}

Note:
always use TS constants with a $ before the name inside the curly braces.
Don't confuse it with variable usage in fluid (without $ ).

regarding your problem:
as Riccardo mentioned: don't use constants for your links, even: don't build your links by hand . If you really need your domain in urls, automate it with a setting of config.absRefPrefix (preferrable to config.baseURL )

I guess that you could use config.absRefPrefix or config.baseURL (see https://docs.typo3.org/typo3cms/TyposcriptReference/Setup/Config/ )

you can set in TypoScript constants:

tx_dti.settings.url = http://myserver

then in TypoScript Setup:

config.baseURL = {$tx_dti.settings.url}

or

config.absRefPrefix = {$tx_dti.settings.url}

Is this sufficient or am I misunderstanding your request?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM