5
votes

I define an array in my extensions setup.txt typoscript like so:

settings{
        halls {
            0 = Default
            1 = Mississauga   
            2 = Halifax
            3 = Niagara Falls 
            4 = Oakville 
            5 = Pickering 
        }}

how in my fluid template can I find the length of the halls array? I want an if condition to do one thing if length is greater than 1 and another if not.

I am using typo3 v4.5.32 with extbase 1.3.

Thank you. I tried this but it puts out two <th> tags instead of just one. The first if is meant to prove that there is more than one element, the next is meant to print out a <th> only if it is the first element.

<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
    <f:if condition="{itemIteration.isFirst !=  itemIteration.isLast}">
        <f:if condition="{itemIteration.index ==  0}">
            <th>
                <f:translate key="tx_bpscoupons_domain_model_coupon.hall" />
            </th>
        </f:if>
    </f:if>
</f:for>

Why?

UPDATE: here is my work around for a lack of "is array length > 1" ferature in fluid

<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
    <f:if condition="{itemIteration.isFirst}">
        <f:then>
            <f:if condition="{itemIteration.isLast}">
                // if both first and last there is only one so skip
            </f:if>
        </f:then>
        <f:else>
            <f:if condition="{itemIteration.isLast}">
                //if there is more than one element in halls then eventually you'll get here once so print out th tags
                <th>
                    <f:translate key="tx_bpscoupons_domain_model_coupon.hall" />
                </th>
            </f:if>
        </f:else>
    </f:if>
</f:for>

and to print out data:

<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
    <f:if condition="{itemIteration.isFirst}">
        <f:then>
            <f:if condition="{itemIteration.isLast}">
                <f:then>
                </f:then>
                <f:else>
                    <f:if condition="{number} == {coupon.hall}">
                        <td>{hall}</td>
                    </f:if>
                </f:else>
            </f:if>
        </f:then>
        <f:else>
            <f:if condition="{number} == {coupon.hall}">
                <td>{hall}</td>
            </f:if>
        </f:else>
    </f:if>
</f:for>

ugly but seems to work.

UPDATE 2 : DOH!!! I missed total, just what I needed but I was looking for length or count, so this code is what I want:

<f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
    <f:if condition="{itemIteration.total} > 1">
        <f:if condition="{itemIteration.isLast}">
          <th><f:translate key="tx_bpscoupons_domain_model_coupon.hall" /></th>
        </f:if>   
    </f:if>
</f:for>
4
Please close the question even if you answered it yourself.lorenz

4 Answers

8
votes

You can find a length of an array with the f:count ViewHelper. Just like other ViewHelper it can be used as an inline notation (You can find more here)

The correct code would be:

<f:if condition="{settings.halls -> f.count()} > 1">
  <f:then>
      //your code for then
  </f:then>
  <f:else>
     //your code for else 
  </f:else>
</f:if>
0
votes

see update#2 above for the answer. to reiterate:

 <f:for each="{settings.halls}" as="hall" key="number" iteration="itemIteration">
    <f:if condition="{itemIteration.total} > 1">
       <f:if condition="{itemIteration.isLast}">
          <th><f:translate key="tx_bpscoupons_domain_model_coupon.hall" /></th>
       </f:if>   
    </f:if>
 </f:for>
0
votes

For those of you who are looking for a custom viewhelper for this:


class ArrayCountViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
     * Get array count
     * 
     * @param mixed $array
     * @return string
     */
    public function render($array) {
        if (!is_array($array))
            return 0;
        return count($array);
    }
}
0
votes

<f:if condition="{f:count(subject: settings.halls)} > 1"> do something </f:if>