Cotonti / Open Source PHP Content Management FrameworkContent Management Framework

Truncating text

Документация / Инструкции и типовые решения / 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  22.10.2011 18:14

Thanks: 0

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? 

Всего: 1, на странице: 1

Добавление комментариев доступно только зарегистрированным пользователям