Jump to:

3434 Posts in 1026 Topics by 866 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: 6446 Views
  • Devel0per
    Avatar
    Community Member
    5 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    u can add given below function to ur "sapphire/core/model/fieldtypes/Text.php

       function CustomWordCount($numWords = 30, $add = '...') {
          $this->value = strip_tags($this->value);
          $ret = explode(' ', $this->value, $numWords + 1);

          if(count($ret) <= $numWords - 1) {
             $ret = $this->value;
          } else {
             array_pop($ret);
             $ret = implode(' ', $ret) . $add;
          }
          return $ret;
       }

    and then in .ss file call,
    $Content.CustomWordCount

    i hope it will work for u.

  • Johan
    Avatar
    Community Member
    49 Posts

    Re: NoHTML in LimitWordCount Summary Link to this post

    This is new I think.

    I see this in "sapphire/core/model/fieldtypes/Text.php"

       /**
        * Limit this field's content by a number of words.
        * CAUTION: This is not XML safe. Please use
        * {@link LimitWordCountXML()} instead.
        *
        * @param int $numWords Number of words to limit by
        * @param string $add Ellipsis to add to the end of truncated string
        * @return string
        */
       function LimitWordCount($numWords = 26, $add = '...') {
          $this->value = trim(Convert::xml2raw($this->value));
          $ret = explode(' ', $this->value, $numWords + 1);
          
          if(count($ret) <= $numWords - 1) {
             $ret = $this->value;
          } else {
             array_pop($ret);
             $ret = implode(' ', $ret) . $add;
          }
          
          return $ret;
       }

    So add $Content.LimitWordCount to the .ss file for 26 words or add $Content.LimitWordCount(10) to change the amount of words.

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