Page 1 of 1

truncate content to certain character count

Posted: Mon Aug 22, 2011 8:21 pm
by stevegos
Hi, I wish to truncate some content to a certain character count. I found that I can do this like this...

{content|truncate:250:"... Read more":true}

However I want the Read more to be a link, for example...

{content|truncate:250:"... <a href="http://link.com">Read more</a>":true}

But... the set of "double quotes" in the link html causes problems. Is there a way to escape these chararcter so they are correctly displayed in the browser?

Also, is there a way to truncate to a word count rather than a chararcter count so the truncate does not cut words off part way through?

Thanks, Steven

Re: truncate content to certain character count

Posted: Mon Aug 22, 2011 10:36 pm
by Jos
how about

Code: Select all

{content|truncate:250:'... <a href="http://link.com">Read more</a>'}
You might also want to check the smarty documentation:
http://www.smarty.net/docsv2/en/languag ... uncate.tpl
This determines whether or not to truncate at a word boundary with FALSE, or at the exact character with TRUE.

Re: truncate content to certain character count

Posted: Tue Aug 23, 2011 7:24 am
by stevegos
Many thanks, thats worked fine.

I also need to know how to insert a smarty tag within a smarty tag. Is that called Nesting?

for example:

{content|truncate:250:'... <a href="http://link.com">{title}</a>'}

The above with a second set of curly braces {title} does not work. I did look on the smarty help pages but could not find anything.

Steven

Re: truncate content to certain character count

Posted: Tue Aug 23, 2011 7:36 am
by Jos
The exact same example is given here http://wiki.cmsmadesimple.org/index.php ... riables.3F

Re: truncate content to certain character count

Posted: Wed Aug 24, 2011 2:35 am
by calguy1000
Smarty tags can't be nested in the way you'd think Smarty is smart, just not that smart. (at least not smarty 2).

You need to build the string that you want to use for your 'read more' stuff, and assign that to a smarty variable, then use the smarty variable inside the next call... Smarty gives you at least two tags to do this: {capture} and {assign} ... capture is the easiest to deal with.

{capture assign='foo'}<a href="http://foo.com">{title}</a>{/capture}
{content|truncate:256:$foo}