Fork me on GitHub


GitHub Core

Show your support for Zikula! Sign up at Github account and watch the Core project!

GitHub Modules

Forum Activity

Forum feed

» Visit forum | » View latest posts

Oldest known version of this page was edited on 2008-10-06 08:17:35 by espaan [ added extra howto in php ]

Truncating (clipping) a large sentence

Within smarty you can easily truncate a large string to a smaller string for displaying.
There are several ways of doing this described below and some modifiers that might come in handy.

See also this Forum topic where a lot of string truncating is described.
  • $string|truncate:80 will clip a string after a specified length and add ... to the truncated string. Use $string|truncate:100:'more':true to add the not the default ..., but more after the truncated string and truncate at the exact character instead of at a word boundary (the default). Truncating after a whole sentence is also possible. Beware however that HTML tags can get truncated as well. The rendering of your string can become messed up. Smarty truncate description.
    In PHP you can do this with substr and wordwrap, but this is not as easy as the smarty modifier. Here are some examples of how to accomplish the same truncating in PHP.
  • $string|truncatehtml:80 a new Zikula modifier that will clip a string on whole words after a specified length, but will keep the HTML tags intact. This means you can truncate a string with HTML markup in there and it will still render ok. It has an optional parameter for the suffix after truncating. The source can be found here in the ValueAddon News project.
  • $string|strip_tags this will remove all the HTML tags and replace them with a space. You can use this to clean out HTML tags and do a regular truncate after that. $string|strip_tags|truncate:80. See also Smarty strip_tags description.
  • There are several jQuery plugins that will truncate a string and add a clickable (more) and (less) to the string. Very nice effect. Disadvantage is of course that you have to have jQuery installed and that it adds extra JavaScript? to your page. Check out the jQuery plugins: