Sep 28, 2014

Outputting an array with a separator in TYPO3 Fluid

Sometimes you need to output multiple values with a separator and omit empty ones. How do you do it with Fluid?

The following piece of code also uses viewhelpers from an excellent VHS extension.


{namespace v = Tx_Vhs_ViewHelpers}

<f:section name="multivalue">
<v:var.set name="isFirstItem" value="1"/>

<f:if condition="{separator} == ''">
<v:var.set name="separator" value="<br />"/>
</f:if>

<f:for each="{values}" as="value">
<f:if condition="{value}">
<f:if condition="{isFirstItem}">
<f:then>
<v:var.set name="isFirstItem" value="0"/>
</f:then>
<f:else>
<f:format.raw>{separator}</f:format.raw>
</f:else>
</f:if>
<f:if condition="{raw}">
<f:then>
<f:format.raw>{value}</f:format.raw>
</f:then>
<f:else>
{value}
</f:else>
</f:if>
</f:if>
</f:for>
</f:section>

This section accepts values array and an optional separator argument. If values should be displayed as raw, set raw argument to TRUE.

No comments:

Post a Comment