Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to iterate through an Arraylist within an array list?


Go to End


4 Posts   3174 Views

Avatar
Friksel

Community Member, 7 Posts

19 December 2012 at 10:47pm

How can I loop through an ArrayList within another ArrayList?
I tried a lot of things and was looking all over the Silverstripe sites, but somehow I can't figure out how this works.
Anybody can give me some clue?

This is what I have now:

Function inside controller:

$pages = DataObject::get("SiteTree","MATCH (Title, Content) AGAINST ('$query' IN BOOLEAN MODE)");
$searchresults = new ArrayList(array(
     'pages' => $pages
));

$data['Results'] = $searchresults;

return $this->customise($data)->renderWith(array('Page_results')); 

Template:

<% if Results %>         
      <ul id="MainSearchResults">
        <% loop Results %>
                       <% if pages() %>
                         <% loop pages() %>
                          <li>
                            <a class="searchResultHeader" href="$Link">
                              <% if MenuTitle %>
                                $MenuTitle
                              <% else %>
                                $Title
                              <% end_if %>
                            </a>
                            <p>$Content.LimitWordCountXML</p>
                            <a class="readMoreLink" href="$Link" title="Lees meer over &quot;{$Title}&quot;">Lees meer over &quot; {$Title}&quot;...</a>
                          </li>
                       <% end_loop %>
                       <% else %>
                          <p>There are no results</p>
                       <% end_if %>

        <% end_loop %>
      </ul>
    <% else %>
      <p>Sorry, there are no results</p>
    <% end_if %>

Avatar
kinglozzer

Community Member, 187 Posts

20 December 2012 at 12:00am

In your example, $pages is a DataList, so you don't need to wrap it in an ArrayList.

Just do $data['Results'] = $pages and in your template:

<% if Results %>
        <% control Results %>
            etc
        <% end_control %>
<% else %>
        There were no results
<% end_if %>

Avatar
Friksel

Community Member, 7 Posts

20 December 2012 at 1:25am

@kinglozzer: thanks for your quick response. In my example I had only one list inside a list, but in fact I want to use several lists inside the Arraylist, so my question remains the same...

Avatar
kaanuni

Community Member, 22 Posts

20 December 2012 at 3:33am

Why the parentheses after pages? Have you tried removing them?

Also why not show the name of your controller function. Did you call it pages maybe?