Page 1 of 1

Limiting the length of the news summaries

Posted: Fri Oct 17, 2008 5:31 pm
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?

Re: Limiting the length of the news summaries

Posted: Fri Oct 17, 2008 5:35 pm
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.

Re: Limiting the length of the news summaries

Posted: Fri Oct 17, 2008 5:56 pm
by Nullig
Just change:

{eval var=$entry->summary}

to:

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

Nullig

Re: Limiting the length of the news summaries

Posted: Fri Oct 17, 2008 6:53 pm
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}