Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » How to iterate through an Arraylist within an array list?

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

Page: 1
Go to End
Author Topic: 1061 Views
  • Friksel
    Avatar
    Community Member
    7 Posts

    How to iterate through an Arraylist within an array list? Link to this post

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

  • kinglozzer
    Avatar
    Community Member
    139 Posts

    Re: How to iterate through an Arraylist within an array list? Link to this post

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

  • Friksel
    Avatar
    Community Member
    7 Posts

    Re: How to iterate through an Arraylist within an array list? Link to this post

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

  • kaanuni
    Avatar
    Community Member
    22 Posts

    Re: How to iterate through an Arraylist within an array list? Link to this post

    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?

    1061 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.