Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Why does <% with Content %>$LimitWordCountXML(10)<% end_with %> but $Content.LimitWordCountXML(10) doesn't

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

Page: 1
Go to End
Author Topic: 403 Views
  • vwd
    Avatar
    Community Member
    158 Posts

    Why does <% with Content %>$LimitWordCountXML(10)<% end_with %> but $Content.LimitWordCountXML(10) doesn't Link to this post

    Hi,

    I'm trying to create a number limited list of 'latest blog posts' in my side bar, that appears in all pages (Blog and non-blog) of a site. The listing is to have the blog title, a number limited synopsis of the content and a link.

    However, it appears that I can't call $Content.LimitWordCountXML(10) directly... Instead, I have use use <% with Content %>$LimitWordCountXML(10)<% end_with %>.

    Here is my code.

    In Page_Controller - Page.php:

       function LatestPosts($postLimit=3) {
        $latestPosts = null;
        $blogHolderAvailable = BlogHolder::get();
       
        if ($blogHolderAvailable) {
           $latestPosts = BlogEntry::get()->sort("Date", "ASC")->limit($postLimit);
        }   
        return $latestPosts;
       }   

    Template Snippet - doesn't work

          <ul>
          <% loop $LatestPosts %>
             <li class="latestPostSynopsis">
                <h4>$MenuTitle</h4>
                <p>$Content.LimitWordCountXML(10)...</p>            
                <a href="$Link" title="<% _t('VIEWFULL', 'View full post titled -') %> '$Title'">Read More</a>
             </li>
          <% end_loop %>
          </ul>   

    The above template outputs:

       <li>
          <h4>MyMenu1</h4>
          <p>This is the full blog content that has been output instead of being word limited to 10.LimitWordCountXML(10)...<p>
          <a href="/blog1" title="Full blog1 title">Read More</a>
       </li>

    When I replace $Content.LimitWordCountXML(10) with <% with Content %>$LimitWordCountXML(10)<% end_with %>, it works correctly.

    Is it not possible to pass in parameters to a sub-property the way I have done it? Do I need to enter into the scope of the property ($Content) using <% with %>to call methods and pass parameters?

    Thank you.
    VWD.

  • kinglozzer
    Avatar
    Community Member
    138 Posts

    Re: Why does <% with Content %>$LimitWordCountXML(10)<% end_with %> but $Content.LimitWordCountXML(10) doesn't Link to this post

    In your template, try:

    <p>{$Content.LimitWordCountXML(10)}...</p>

    I wonder if it's picking up those extra dots after and that's what's throwing it.

  • vwd
    Avatar
    Community Member
    158 Posts

    Re: Why does <% with Content %>$LimitWordCountXML(10)<% end_with %> but $Content.LimitWordCountXML(10) doesn't Link to this post

    You're right! Great suggestion! Surrounding the statement in curly braces got it working.

    Thanks Kinglozzer.

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