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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

NoHTML in LimitWordCount Summary


Go to End


10 Posts   11583 Views

Avatar
Andrew Houle

Community Member, 140 Posts

23 April 2009 at 3:29am

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.

Avatar
Carbon Crayon

Community Member, 598 Posts

23 April 2009 at 9:03am

Edited: 23/04/2009 9:03am

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.

Avatar
Andrew Houle

Community Member, 140 Posts

24 April 2009 at 1:23am

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!

Avatar
Carbon Crayon

Community Member, 598 Posts

24 April 2009 at 4:15am

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

Avatar
Andrew Houle

Community Member, 140 Posts

24 April 2009 at 6:13am

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.

Avatar
Andrew Houle

Community Member, 140 Posts

25 April 2009 at 7:26am

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.

Avatar
Johan

Community Member, 49 Posts

26 August 2010 at 12:22am

Not ideal but this may do as a solution:

$Content.FirstSentence

Avatar
cuSSter

Community Member, 56 Posts

2 December 2010 at 9:14pm

Edited: 02/12/2010 9:16pm

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