TYPO3: Well-formed fluid templates with dynamic tags

At work we let Jenkins run xmllint on our TYPO3 fluid template files to catch bugs before they get deployed.

This requires well-formed template files, and one of the challenges is to keep them well-formed even when tags and attributes need to be created dynamically depending on conditions.

Dynamic attributes

The following template is not well-formed and xmllint will error out:

<div {f:if(condition:data.isbig, then:'class="big"')}>..</div>

The solution to this problem is to set a variable if the condition matches and use that in the attribute:

<f:if condition="{data.isbig}">
    <v:variable.set name="divclass" value="big"/>
</f:if>
<div class="{divclass}">..</div>

Dynamic tags

The classic situation for dynamically generated tag names is when the editor shall be able to choose if a headline is a h1 or h2 in the content element settings:

<{settings.type}>..</{settings.type}>

This is of course pretty invalid XML, and VHS also provides a solution: v:tag:

<v:tag name="{settings.type}">..</v:tag>

Written by Christian Weiske.

Comments? Please send an e-mail.