Limiting the length of the news summaries

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
pdedecker
Forum Members
Forum Members
Posts: 28
Joined: Sun Apr 16, 2006 8:25 am

Limiting the length of the news summaries

Post by pdedecker »

This piece of code is part of my news template:

Code: Select all

{if $entry->summary}
{eval var=$entry->summary}
<p>[{$entry->morelink}]</p>
{else}
[{$entry->morelink}]
{/if}
Sometimes summaries turn out to be too long to fit in the sidebar. Let's say I want to limit the length of the news summaries to 50 characters (also trim out spaces at the end of the string, if necessary) and add "..." at the end of the resulting string. How can I do this?
User avatar
duclet
Forum Members
Forum Members
Posts: 187
Joined: Fri Jun 23, 2006 12:55 pm

Re: Limiting the length of the news summaries

Post by duclet »

The easiest solution is to just limit them when inputting the news so you don't have to worry about it here. Otherwise, just store the result of the eval to a variable and use of the Smarty modifiers to shorten the length.
User avatar
Nullig
Power Poster
Power Poster
Posts: 2380
Joined: Fri Feb 02, 2007 4:31 pm
Location: Comox Valley, BC

Re: Limiting the length of the news summaries

Post by Nullig »

Just change:

{eval var=$entry->summary}

to:

{eval var=$entry->summary|truncate:50:'...':false:false}

Nullig
Ziggywigged
Power Poster
Power Poster
Posts: 424
Joined: Sat Feb 02, 2008 12:42 am
Location: USA

Re: Limiting the length of the news summaries

Post by Ziggywigged »

Nice tip, Nullig.

Is there a way to auto-create a summary of the first (let's say) 50 characters from the content?
Often the summary is just the first sentence or paragraph from the content anyway.

==edit==

Nevermind, just found that it works with content too: {eval var=$entry->content|truncate:50:'...':false:false}
This is very useful, thanks again.

==edit2==

Note to self: to strip html tags use this:

Code: Select all

{eval var=$entry->content|truncate:50:'...':false:false|strip_tags:true}
Last edited by Anonymous on Mon Oct 20, 2008 12:00 pm, edited 1 time in total.
Take a penny, leave a penny.
Post Reply

Return to “Developers Discussion”