dora
dora

Reputation: 1344

TYPO3 FLUID conditions while looping array

I have an array which contains a list of names. In the front end, this has to be listed in a listed way. Using the <ul> and <li> tags.

Half of the list should be displayed under one <ul> tag and the other half under another <ul> tag.

like this:

<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>

<ul>
<li>Four</li>
<li>Five</li>
<li>Six</li>
</ul>

Now the problem is in FLUID I did not find a way to end the <ul> tags when the array loops half of the elements.

I want to do something like this

<f:for each="{my_array}" as="item">
<li>{item}</li>
<!-- if condition where count=(my_array%2) then use </ul><ul>-->
</f:for>

Upvotes: 3

Views: 10193

Answers (2)

Merec
Merec

Reputation: 2761

This should work. The only problem is when there is an odd amount of menu entries.

<f:for each="{my_array}" as="item" iteration="itemIterator">
    <ul>
        <li>{item}</li>
        <f:if condition="{itemIterator.iterator}%({itemIterator.total}/2) == 0">
            </ul><ul>
        </f:if>
    </ul>
</f:for>

Upvotes: 0

Andr&#225;s Ott&#243;
Andr&#225;s Ott&#243;

Reputation: 7695

I think you should get the id in PHP for the element in the middle and then you can use a simple if, like this:

It is a bit extended version of f:for. Here you can check the iteration property. When you use the iteration.index it is the current index of elements. However an elementLimit you have to set.

It is several properties for iteration : isFirst,isLast, isEven, isOdd, total, cycle, index.

Here is an article about it.

UPDATED example:

<f:for each="{my_array}" as="item" iteration="itemIterator">
    <li>{item}</li>
    <f:if condition="{itemIterator.index} = {elementLimit}">
        </ul><ul>
    </f:if>
</f:for>

Upvotes: 8

Related Questions