Jack O'Connor
Jack O'Connor

Reputation: 334

Calling the BlogEntries function from another function [SilverStripe]

I am working with the blog module in SilverStripe. Within the template it loops over the blog entries function as it should. However, I am also using API's which I am looping over in the blog holder too.

<div class="blog-section">
    <% loop $getSlackMessage %>
         <p id="author-tag">$UserName <span id="slackTimestamp">$Created.Format(h:i A)</span></p>
         <p id="postDescription">$Text</p>
    <% end_loop %>
</div>

<div class="blog-section">
    <% loop $getLatestTracks %>
         <p id="track-name">$Name </p>
    <% end_loop %>
</div>

<% loop BlogEntries %>
      <article class="col-md-3 item" section-type="article">
              <% include BlogSummary %>
      </article>   
<% end_loop %> 

So as you can see I am looping 3 times here, this is somewhat annoying as on every blog holder page the $getSlackMessage and $getLatestTracks will be visible. This means when I go page 2 I will see the same thing I seen on page 1 with a few different articles below them.

How can I consolidate all of these loops and have SilverStripe spit them out as one big loop which I can then order by created time?

If you need anything else let me know, thanks.

Upvotes: 1

Views: 206

Answers (1)

Varis
Varis

Reputation: 254

If you want a function that returns a merged result of getSlackMessage and getLatestTracks, then you can write this:

function consolidatedF() {
    $slackMessages = $this->getSlackMessage();
    $latestTracks = $this->getLatestTracks();

    $resultList = new ArrayList($slackMessages->toArray());
    $resultList->merge($latestTracks->toArray());

    return $resultList;
}

Then loop it in the template as <% loop $consolidatedF.Sort(Created, ASC) %>

Upvotes: 1

Related Questions