Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

NoHTML in LimitWordCount Summary

Go to End

10 Posts   6628 Views

Andrew Houle

23 April 2009 at 3:29am Community Member, 132 Posts

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


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

Carbon Crayon

23 April 2009 at 9:03am (Last edited: 23 April 2009 9:03am), Community Member, 598 Posts

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

24 April 2009 at 1:23am Community Member, 132 Posts

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) {
      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 %>
<% 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!

Carbon Crayon

24 April 2009 at 4:15am Community Member, 598 Posts

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

24 April 2009 at 6:13am Community Member, 132 Posts

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

25 April 2009 at 7:26am Community Member, 132 Posts

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.


26 August 2010 at 12:22am Community Member, 49 Posts

Not ideal but this may do as a solution:



2 December 2010 at 9:14pm (Last edited: 2 December 2010 9:16pm), Community Member, 56 Posts

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. :)

Go to Top