truncate content to certain character count

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
stevegos

truncate content to certain character count

Post 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
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: truncate content to certain character count

Post 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.
stevegos

Re: truncate content to certain character count

Post 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
Jos
Support Guru
Support Guru
Posts: 4020
Joined: Wed Sep 05, 2007 8:03 pm

Re: truncate content to certain character count

Post by Jos »

The exact same example is given here http://wiki.cmsmadesimple.org/index.php ... riables.3F
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: truncate content to certain character count

Post 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}
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Locked

Return to “CMSMS Core”