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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

Our old forums are still available as a read-only archive.

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

LatestNews vs. Article Holder


Go to End


3 Posts   2578 Views

Avatar
sd0wn

Community Member, 2 Posts

22 July 2008 at 5:12am

Is there anyway to limit the amount of characters returned for the $Content?

I like how the News displays on the article holder page, but for sidebar (latest news) on the homepage, I dont want to display so much. Maybe 120 characters max.

Is this possible?

Page.php:

function LatestNewsItems($num = 3) 
{
	$news = DataObject::get_one("ArticleHolder");
	return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $num) : false;
}

LatestNews.ss

<% control LatestNewsItems %>
<p>$Content</p>
<% if FirstLast != last %><div class="hr">&nbsp;</div><% end_if %>
<% end_control %>

Avatar
Anatol

126 Posts

22 July 2008 at 11:48am

Edited: 22/07/2008 11:49am

Hi,

try this page: http://doc.silverstripe.com/doku.php?id=text&s=limitcharacters

That's probably what you are looking for. You can limit the number of characters, or the number of words, display the first paragraph only, etc...

Just add e.g.

$Content.LimitCharacters(120)

You can also change the default characters that are displayed at the end if the text exceeds the maximum length (default is '...'). See here: http://api.silverstripe.com/sapphire/model/Text.html#methodLimitCharacters

Cheers!
Anatol

Avatar
BLU42 Media

Community Member, 71 Posts

22 July 2008 at 11:50am

You sure can... easily too! There are some cool functions built right into the framework that you can use in your ss template. Just switch out $Content with one of these:

$Content.FirstParagraph
$Content.FirstSentence

Check out http://doc.silverstripe.com/doku.php?id=htmltext for more details.

Hope that helps!