Truncating text

Sometimes you will want to show only the first # of characters of a text in your template, for example for a 'preview'. This can be achieved easily with a CoTemplate callback:

{PAGE_TEXT|cot_string_truncate('$this', 200)}

The 2nd parameter is the maximum length of the final text, in this case 200 characters. cot_string_truncate will, by default, not break html tags or words.

An interesting way to dynamically show a 'Read more' link if the text was truncated is here:

{PAGE_TEXT|cot_string_truncate('$this', 200)}
<!-- IF {PAGE_TEXT} != {PAGE_TEXT|cot_string_truncate('$this', 200)} -->...
<a href="{PAGE_URL}">{PHP.L.ReadMore|mb_strtolower}</a><!-- ENDIF -->

Thanks to esclkm for this suggestion.


1. lukgoh  2011-10-22 18:14

Thank you for this!

I was wondering though, because I am using the html parser for the news pages, it adds the closing paragraph tag to the end of the PAGE_ROW_TEXT and therfore pushing the "..." and the read more link under the text. Is there anyway of stopping this without removing the < p >< /p > tags from the news article itself? 

Only registered users can post new comments