Urs
Urs

Reputation: 5132

In TYPO3 Fluid, how do I access a variable from inside a foreach loop?

I am building a navigation with the VHS Menu ViewHelper. Below is the entire code. My question is probably very basic fluid though.

Inside a scope, how do I access a variable that exists outside that scope - or how do I pass it into that scope?

EDIT: I've updated my code to not use dashes in variable names as well as assigned different names to v:page.menu with as="menuLevel0".


The full code:

{namespace v=Tx_Vhs_ViewHelpers}
<ul id="nav" class="grid-full classic">
    <v:page.menu as="menuLevel0" expandAll="1" useShortcutData="1" levels="4" classHasSubpages="parent" classFirst="first" classLast="last">
        <f:for each="{menuLevel0}" as="itemLevel0" iteration="iterLevel0">
            <li class="{itemLevel0.class} level0 nav-{iterLevel0.cycle} level-top">
                <a href='{itemLevel0.link}' class='level-top'><span>
                    <f:if condition="{itemLevel0.nav_title}"><f:then>{itemLevel0.nav_title}</f:then><f:else>{itemLevel0.title}</f:else></f:if>
                nav-{iterLevel0.cycle}</span>
                </a>
                <f:if condition="{itemLevel0.hasSubPages}">
                    <f:then>
                        <v:page.menu as="menuLevel1" expandAll="1" classHasSubpages="parent" classFirst="first" classLast="last">
                            <ul class="level0">
                                <f:for each="{menuLevel1}" as="itemLevel1" iteration="iterLevel1">
                                    <li class="{itemLevel1.class} level1 nav-{iterLevel0.cycle}-{iterLevel1.cycle}">
                                        <a href='{itemLevel1.link}' class='level-top'><span>
                                            <f:if condition="{itemLevel1.nav_title}"><f:then>{itemLevel1.nav_title}</f:then><f:else>{itemLevel1.title}</f:else></f:if>
                                            nav-{menuLevel0.iterLevel0.cycle}-{iterLevel1.cycle}
                                        </span></a>
                                    </li>
                                </f:for>
                            </ul>
                        </v:page.menu>
                    </f:then>
                </f:if>
            </li>
        </f:for>
    </v:page.menu>
</ul>

Upvotes: 4

Views: 3288

Answers (4)

Ashish Patel
Ashish Patel

Reputation: 1041

In Controller

public function listAction() {
    $events = $this->eventsRepository->getEventsList();
    $this->view->assign('eventsList', $events);
}

In View File you can access like this.

<f:for each="{eventsList}" as="events">
    <tr>
        <td> {events.event_id} </td>        
        <td> <f:link.external uri="{events.event_link}" target="_blank">{events.event_link}</f:link.external></td>
        <td><f:format.date format="d-m-Y">{events.event_date}</f:format.date></td>
    </tr>
</f:for>

Upvotes: 1

Cedric Ziel
Cedric Ziel

Reputation: 1052

In fluid, variables are and should be scoped to the blocks you are using.

While you are using our v:page.menu ViewHelpers, you need to understand the scoping a bit further:

  1. Use different names for variables in nested scopes: {levelOneIterator} and {levelTwoIterator} are good examples for such behaviour
  2. As a rule of thumb: Use lowerCamelCased variable names. It prevents fluid from parsing your var-names wrong.

The reason why your code is not working:

You assign a {iter} variable. Therefore fluid doesn't parse the dash-ified variable name as it detects the undashified identifier first. Dashes have special meaning in fluid and you can run into kind-of race conditions there.

To take this a bit further:

``` {foo-bar} // echos "baz" {foo-bar} // echos "bing", as the bar vaiable is read first, and "foo-" isnt a variable in the current scope

```

Update:

Accessing variables from a parent scope doesnt require prefixing your var-call. In a child for-loop, you can access the parent iteration by simply calling the var.

<f:for each="{iterable}" as="iterableItem iteration="iteratorRoot"> {iteratorRoot.index} <!-- Echoes the current, zero-indexed iteration --> <f:for each="{iterableItem}" as="subItem" iteration="iterationChild"> <!-- STILL echoes the current, zero-indexed iteration --> {iteratorRoot.index} <!-- Echoes the current, zero-indexed sub iteration --> {iterationChild.index} </f:for> </f:for>

As long as you give different names to the iterators, you can access them in any scope by their originating name. Same goes for the aliased (as="name") named variables.

Upvotes: 2

thismaechler
thismaechler

Reputation: 211

Try not using "-" in the variable-name! Use CamelCase instead:

<f:render section="layer2" arguments="{uid: curPage.uid, parentIter: iter.cycle}" />

Upvotes: 1

Daniel
Daniel

Reputation: 7036

Have you tried not to use the same varibale name for the iterators in both of your foreach loops?

Upvotes: 0

Related Questions