Jump to:

3432 Posts in 1025 Topics by 865 members

Template Questions

SilverStripe Forums » Template Questions » NoHTML in LimitWordCount Summary

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

Page: 1 2
Go to End
Author Topic: 6437 Views
  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    NoHTML in LimitWordCount Summary Link to this post

    I think this should be rather easy, but I can't find a simple solution to this. Basically I just want to print a summary of about 50 words as plain text. However, asterisks are being substituted where bold text is. This thread seems to be headed in the right direction: http://silverstripe.org/customising-the-cms/show/255639#post255639 but the ticket explaining the fix is not working.

    Has anyone gotten this to work correctly? What should go in my controller, and what should go in my template?

    I have something like this in my controller...

    function ArticleSummary() {
          $ac = $this->obj('ArticleContent')->NoHTML();
          return $ac->LimitWordCount(50);
       }

    And this in my template...

    <p>$ArticleSummary</p>

    I know that logic has plenty of flaws, but I wanted to give a starting point. Thanks in advance for you help.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    Hi Andrew

    I ended up doing something this with regular php as I wanted a charachter limit, it looked something like this:

    function LimitWords($CharLimit = 500){

    $NoHTML = strip_tags($this->Content);

    return substr($NoHTML, $CharLimit);

    }


    Shouldnt be too much trouble to adapt it for woud count instead of chars.

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    HI Aram,

    Thanks for your help. I'm still quite a noob at most things programming so forgive me if this is a dumb question. The Article summaries that I want to pull minus all the HTML issues are coming from two url params of IssueID and CategoryID. So I have a function like this...

    function show() {
          $articles = DataObject::get("Article", "ParentID = " . $this->urlParams['ID'] . " AND CategoryID = " . $this->urlParams['OtherID']);
          if ($articles->Count() == 1) {
             Director::redirect($articles->First()->Link());
          }
          else {
             return $this->customise(array('Articles' => $articles))->renderWith(array('IssueHolder_show','Page'));
          }
       }

    Then in my template I pull the info using something like...

    <% control Articles %>
    $ArticleContent.LimitWordCountPlainText(60)
    <% end_control %>

    I would like to sub out $ArticleContent.LimitWordCountPlainText(60) with $ArticleBlurb which is a function like how you described. My question is how to interact with my show() function? Hopefully that makes sense, thanks in advance for your help!

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    Hi Andrew

    You should be able to put the ArticleBlurb() function in your ArticlePage model and then call it within your <% control Articles %> without affecting the show() stuff.

    As long as you are in the context of an article, $ArticleBlurb should return the correct stuff in the same way as $Content would.

    Hope that helps

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    I got it to work but It's not limiting the characters, but it is stripping the html... weird. Oh well, I've got a different workaround that will do the trick. Thanks for you help.

  • Andrew Houle
    Avatar
    Community Member
    132 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    I figured out why the code wasn't returning the first x amount of characters. So for completion sake, maybe this will help someone else; here is an example of the code:

    function ArticleSummary($Char=240) {
       $NoHTML = strip_tags($this->ArticleContent);
       return substr($NoHTML,null,$Char);
    }

    I don't think the substr function will work properly without taking in that second parameter, which btw could be use to start at a different place.

  • Johan
    Avatar
    Community Member
    49 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    Not ideal but this may do as a solution:

    $Content.FirstSentence

  • cuSSter
    Avatar
    Community Member
    56 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    The code was great but I think it's more neat if you limit the content with number of words, not with the number of characters. I just tweaked the code that Andrew provided. This function now returns the content limited to a number of words. Hope this could help someone.

    public function getArticleSummary( $word_limit = 50 ) {
       $NoHTML = strip_tags( $this->ArticleContent );
       $words = explode( ' ', $NoHTML );
       return implode( ' ', array_slice( $words, 0, $word_limit ) );
    }

    Thanks to Andrew for initiating this topic.

    6437 Views
Page: 1 2
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.