2

I have to arrays like:

$business = array(0 => 'Car', 1 => 'IT');
$counts   = array(0 => 15, 1 => 33);

I assign both array in my fluid template and iterate over the array business.

<f:for each="{business}" as="b" key="key">
    <li>
        <f:link.action action="business" arguments="{current_business: b.uid}">
            <f:if condition="{counts.key} > 0">
                <f:then>
                    {b.title} {counts.key}
                </f:then>
                <f:else>
                    {b.title}
                </f:else>
             </f:if>
        </f:link.action>
    </li>
</f:for>

I don't get an ouput for {counts.key}, should i access this an other way?

2 Answers 2

4

This can be done using the v:variable.get-ViewHelper from the extension vhs. Instead of {counts.key} use

{v:variable.get(name: 'counts.{key}')}

or

<v:variable.get name="counts.{key}"/>.
Sign up to request clarification or add additional context in comments.

Comments

3

Of course you can do it as Jost showed by manipulating data within the view , it's perfectly valid, on the other hand probably combining arrays within your controllers into associative array(s) will be just more comfortable, like:

$business = array(
    0 => array('title' => 'Car', 'count' => '15'),
    1 => array('title' => 'IT', 'count' => '33'),
);

view

<f:for each="{business}" as="b">
    <li>
        <f:link.action action="business" arguments="{current_business: b.uid}">
            <f:if condition="{b.count} > 0">
                <f:then>
                    {b.title} ({b.count})
                </f:then>
                <f:else>
                    {b.title} (no items)
                </f:else>
             </f:if>
        </f:link.action>
    </li>
</f:for>

Note: If in real case that's a collection of model objects, you can just add transient - count field into your model - without TCA and SQL declarations - in this case you will be able to set values "on the fly" on this field within the controller and use them as common model field in the views but they won't be saved to DB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.