Search and replace text in TYPO3

By defining placeholders at one centralized location, you can replace content on an entire website automatically.

Motivation

On a website, there are often text blocks or single words that occur in several places. In this case it is useful to store these texts at a central location along with an arbitrary short name. These short names can then be used as a kind of placeholder in content elements throughout the site and will be replaced automatically.

Negative Example

On this website you can see, how those shortnames can be applied: The headings Motivation, Example, and Best Practice are actually the shortnames "MOTIVATION", "EXAMPLE", and "BEST_PRACTICE".

Just basic words are replaced here. But you can also replace URLs, whole text blocks or HTML tags (e.g. img src tags for images).

Best Practice

By storing the texts and their corresponding shortnames at a central location, it gets easy to modify the texts quickly. You could, for example, change the value of the shortname "EXAMPLE" to "case study" and this text would be replaced in all content elements that contain this placeholder. This is kind of a "search and replace" feature in TYPO3.

This is done by adding the parseFunc.short function (TEXT cObject) to the PAGE object (here: page) in your TypoScript template:

Usage of parseFunc.short TypoScript, SETUP section
# language 0: German (default)
page.stdWrap.parseFunc.short {
# simple text
  EXAMPLE = Beispiel
# baseUrl of our website (Constant from CONSTANTS section)
  BASE_URL = {$baseUrl}
# <a href> tag
  URL_PARTNER = <a href="http://www.example.com">Partneragentur</a>
}
 
# language 1: English
[globalVar = GP:L=1]
page.stdWrap.parseFunc.short {
  EXAMPLE = example
  BASE_URL = {$baseUrl}
  URL_PARTNER = <a href="http://www.example.com">partner agency</a>
}
[global]

As you can see, various assignments are possible (simple text, TypoScript constants, HTML tags, etc.). Using TypoScript conditions, translations for several languages can be defined for the same shortnames.

Now let's see how this works in a text that contains the placeholders defined in the example above:

Original text with placeholders:

This is an EXAMPLE for automatic replacement of placeholders using the parseFunc.short function: The baseUrl of our website is BASE_URL and this is a hyperlink to our URL_PARTNER.

Using the shortnames which were defined in our example above, this text will be changed in the following way:

Text parsed by parseFunc.short:

This is an example for automatic replacement of placeholders using the parseFunc.short function: The baseUrl of our website is http://www.jochenfroehlich.com/ and this is a hyperlink to our <a href="http://www.example.com">partner agency</a>.

Tip

To prevent accidental replacement of e.g. abbreviations, we recommend to assign a unique prefix to the placeholders. For example, we prepend the string S_ (for "short") to each of our shortnames.