Jump to:

1851 Posts in 1606 Topics by 561 members

Blog Module

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

Discuss the Blog Module.

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

Page: 1
Go to End
Author Topic: 1545 Views
  • house98
    Avatar
    Community Member
    31 Posts

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

    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?

  • house98
    Avatar
    Community Member
    31 Posts

    Re: Use Blog Entry as Content on Another Section, Limited to Just Latest Blog Entry (1) Link to this post

    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.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Use Blog Entry as Content on Another Section, Limited to Just Latest Blog Entry (1) Link to this post

    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.

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