BlogPost Content Summary with char/word limit and preserved and rendered HTML

Silverstripe Version:4*

I need to customize the BlogPost summaries:

  • Limit to 350 characters or 30 words
  • Preserve HTML (links, colors,…) after limit
  • Render HTML in template
    I don’t suppose anyone has had to do that yet? :slight_smile:

BlogPostExtension.php

class BlogPostExtension extends DataExtension {

private $CustomExcerpt;

public function CustomExcerpt() {
     // TODO: Limit content and preserve HTML  
     return $this->owner->Content;
    }
}

BlogPostSummaries.ss

<% loop $AllBlogPosts %>
   ...
     <p class="l-card_excerpt">$CustomExcerpt</p> // How to render the HTML??
   ...
<% end_loop %>

The problem now is that the HTML is not rendererd.

Result is not rendered:

<b>Some text</b><br>..

What do I need to do to render the HTML?

Sounds like you need to read up on formatting and casting in the docs. You just need to set the cast type for your CustomExcerpt() method.

Thanks for the tip.

    private static $casting = [
        'CustomExcerpt' => 'HTMLText'
    ];
1 Like