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.

Archive /

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

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

LatestNews vs. Article Holder


Reply


3 Posts   2250 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: [url]http://doc.silverstripe.com/doku.php?id=text&s=limitcharacters[/url]

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: [url]http://api.silverstripe.com/sapphire/model/Text.html#methodLimitCharacters[/url]

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 [url]http://doc.silverstripe.com/doku.php?id=htmltext[/url] for more details.

Hope that helps!