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.

Template Questions /

How to iterate through an Arraylist within an array list?


4 Posts   1210 Views


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'));


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

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


Community Member, 175 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 %>
<% end_control %>
<% else %>
There were no results
<% end_if %>


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...


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?