String comparison in Typoscript through user functions in TYPO3

Generally, string comparison using the on-board tools provided by TypoScript can be quite cumbersome in TYPO3. Although, for instance for globalStrings there exists the possibility to use regular expressions and the * character as wildcard, oftentimes this is not flexible enough to handle more complex conditions, such as combined conditions.

User functions in TypoScript

Luckily, it is quite easy to add more complex string comparison functionality to TypoScript through user functions. Simply add your user defined function in localconf.php, e.g.

/**
 * Return TRUE on success, FALSE otherwise.
 * @return boolean
 */
function user_match($var1, $var2, $var3) {
  if($var1 == $var2) {
    return TRUE;
  }

  if($var1 == $var3) {
    return preg_match('#...#', $var2);
  }

  ...
     
  return false;
}

You can then call this function in TypoScript as conditional statement:

[userFunc = user_match(value1, value2, value3)]
  # do something here
[end]

Have a look at the Typoscript Conditions reference for more information.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.