Jump to:

3447 Posts in 1032 Topics by 872 members

Template Questions

SilverStripe Forums » Template Questions » Problems with links when using $Content.LimitWordCountXML(xx)

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

Page: 1
Go to End
Author Topic: 671 Views
  • iraira88
    Community Member
    17 Posts

    Problems with links when using $Content.LimitWordCountXML(xx) Link to this post


    is there a possibility to parse out links when using "$Content.LimitWordCountXML(xx)" in my template? I use the blog module ( Silverstripe 3 ) on my website and "$Content.LimitWordCountXML(18)" in BlogSummary.ss to show some short teasers on the BlogHolder page. The problem is: when I have a link in my BlogEntry content like

    " <a href="http://www.screenplane.com/products/steadyflex/">Screenplane</a> "

    it will be displayed like this on my website:

    " Screenplane[http://www.screenplane.com/products/steadyflex/] "

    Can somebody help me with this issue?

    Many thanks,

  • IOTI
    Community Member
    189 Posts

    Re: Problems with links when using $Content.LimitWordCountXML(xx) Link to this post

    Hi iraira88

    You'll have to create a custom function for your BlogEntry class to get the functionality you want. Start by creating a DataExtension of the BlogEntry class.

    I've included some code I used in an old 2.4 project. it is untested in 3.0 so may require some modification.

    class BlogEntryExtension extends DataExtension {

        * Returns a Summary of the content but retains links (including the pesky site_tree_id links)
        * also removes images.
        * @return (string)
       public function getCustomSummary($maxWords = 35){
          $content = $this->obj('Content');
          $data = (string)$content->forTemplate();
          $data = strip_tags($data, '<a>');
          // grab the first paragraph, or, failing that, the whole content
          if( strpos( $data, "\n\n" ) )
             $data = substr( $data, 0, strpos( $data, "\n\n" ) );
          $sentences = explode( '.', $data );   
          $count = count( explode( ' ', $sentences[0] ) );
          // if the first sentence is too long, show only the first $maxWords words
          if( $count > $maxWords ) {
             return implode( ' ', array_slice( explode( ' ', $sentences[0] ), 0, $maxWords ) ).'...';
          // add each sentence while there are enough words to do so
          $result = '';
          do {
             $result .= trim(array_shift( $sentences )).'.';
             if(count($sentences) > 0) {
                $count += count( explode( ' ', $sentences[0] ) );
             // Ensure that we don't trim half way through a tag or a link
             $brokenLink = (substr_count($result,'<') != substr_count($result,'>')) ||
                (substr_count($result,'<a') != substr_count($result,'</a'));
          } while( ($count < $maxWords || $brokenLink) && $sentences && trim( $sentences[0] ) );
          if( preg_match( '/<a[^>]*>/', $result ) && !preg_match( '/<\/a>/', $result ) )
             $result .= '</a>';
          return $result;


    and then in your _config.php add


    and in BlogSummary.ss use $CustomSummary instead of $Content.LimitWordCountXML. Hope that helps.

  • iraira88
    Community Member
    17 Posts

    Re: Problems with links when using $Content.LimitWordCountXML(xx) Link to this post

    Thank you very much IOTI!

    I've included your "getCustomSummary" function to my BlogEntry class just to see if it works for me (and $getCustomSummary in BlogSummary.ss). Everything works great now!

    Do I still need to do it with a BlogEntryExtension? Is there any difference between both solutions?

  • IOTI
    Community Member
    189 Posts

    Re: Problems with links when using $Content.LimitWordCountXML(xx) Link to this post

    That's fantastic it worked !

    You don't have to use a DataExtension class but it is highly recommended. The reason being, when a new blog module update comes out and you go to update your blog module, you'll have to account for that change in the code every time. You'll have to manually update the BlogEntry.php class with the getCustomSummary() function. Using a DataExtension, you won't have to do this!

    You'll want to make a new php file in your mysite/code folder called BlogEntryExtension.php and use the code i provided in my previous post. After you make the new BlogEntryExtension.php class and add the extra line to your _config.php do a ?flush=all on your site. it won't take long and will save you a ton of time in the future.

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.