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.

Blog Module /

Discuss the Blog Module.

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

Use Blog Entry as Content on Another Section, Limited to Just Latest Blog Entry (1)


Reply


3 Posts   1622 Views

Avatar
house98

Community Member, 31 Posts

5 January 2009 at 7:39am

Edited: 05/01/2009 7:40am

Ok, so this sounds odd, but bare with me. :) I'm wanting to use the Blog module for articles. In the case of the site I'm working on we have 3 sections that will all have 3 separate blogs. For this case, I will focus on the 'family-blog'.

The idea is similar to one mentioned in the archives (which I can't reply to, else I wouldn't have started a new topic) found here:
http://www.silverstripe.org/archive/show/160555

Here is a snippit of what I have now on the family-blog BlogHolder in Page.ss:

<% if URLSegment == family %>
      <% control ChildrenOf(family-blog) %>
            <h2>$Title</h2>
            $ParagraphSummary
            <a href=$Link> more </a>
      <% end_control %>
<% end_if %>

This works perfectly fine for showing the first paragraph of a blog entry, however if there are multiple entries (which there are) they are all shown.

So the suggestion from the URL above was to add a Range(0,X) such as this.

<% if URLSegment == family %>
   <% control ChildrenOf(family-blog) %>
      <% control Range(0,1) %>
         <h2>$Title</h2>
         $ParagraphSummary
         <a href=$Link> more </a>
         <% end_control %>
      <% end_control %>
<% end_if %>

This, however produces no errors, but also no content.

I can't seem to find any reference for how to use Range(X,X) or even if it's supported.

Any guidance?

Avatar
house98

Community Member, 31 Posts

5 January 2009 at 7:56am

Ok.

Thanks to simon_w on the forums ( I love you man )... I was told that Range() could not be used on a control. Also, he suggested I use:

<% if First %> -- which is great, but for me was showing the first entered, so I ch anged it to <% if Last %> and PRESTO! My one, latest article showing where it should be showing. Perfect.

Thanks simon_w -- hope this helps others.

Avatar
Hamish

Community Member, 712 Posts

5 January 2009 at 3:49pm

The only downside of this method is it will be quite inefficient. Sapphire has to find and iterate through all the blog entries to print just one.

It might not be an issue, but as time goes by and more entries are added it could get slow.

In your page class, you could add a new method like:

function LatestBlogPost($parentID) {
return DataObject::get("BlogEntry", "parentID = $parentID", "Date DESC", null, 1);
}

This will return the single latest blog entry for the blog holder with id $parentID.

You might want to refine that a little so you can use the url segment instead of a numeric id.

Then, in your template you can do:
control

<% control LatestBlogPost(123) %>
$Title
<% end_control %>

..where 123 is the page id of the blog to show.