In website content operation, we often need to extract a part of the long text such as articles, product descriptions, etc. as a summary, for list display or card preview.This not only effectively saves page space, but also attracts the attention of users, guiding them to click and view the full content.However, when these long texts contain HTML tags, simple character truncation often leads to unclosed tags, thereby destroying the page layout and affecting the user experience.
AnQiCMS (AnQiCMS) understands this pain point and has built it into the template enginetruncatechars_htmlA filter designed to provide content operators with an elegant and efficient solution.This filter's greatest highlight is that it can intelligently truncate HTML content while ensuring that all unclosed HTML tags are properly closed to avoid page disarray caused by truncation.
Get to knowtruncatechars_htmlFilter
truncatechars_htmlAs the name implies, it is a filter for 'character truncation of HTML'. Unlike the ordinarytruncatecharsDifferent in that it truncates characters only, without considering the HTML structuretruncatechars_htmlWhen performing truncation operations, the input HTML string will be parsed first.It identifies which tags are opened, and when the truncation position falls inside a tag or after truncation a tag is not closed, it intelligently adds the corresponding closing tag at the end of the truncated content to maintain the integrity of the HTML structure.
For example, if you have a segment containing<strong>or<p>tag text, it will be truncated as followstruncatechars_htmlEnsure that these tags are properly closed at the end of the extracted section, even though these tags should be open at the truncation point of the original text.This processing method greatly simplifies the generation process of content abstracts, allowing us to confidently display the truncated content on list pages, search result pages, and other scenarios without worrying about layout issues.
How to usetruncatechars_html
Using in Anqi CMS template,truncatechars_htmlThe filter is very intuitive, and its basic syntax is as follows:
{{ obj|truncatechars_html:number }}
Here:
obj: represents the HTML content variable you want to truncate, such as in the article detail pagearchive.Contentorarchive.Description.number: is an integer that specifies the number of characters to retain after truncation, that numberincludesThe ellipsis added finally (...)
For example, if you want to truncate the content of a document to a 100-character summary and ensure the correct closure of HTML tags, you can write the template code like this:
{# 假设archive.Content是包含HTML标签的文档内容 #}
<div class="article-summary">
{{ archive.Content|truncatechars_html:100|safe }}
<a href="{{ archive.Link }}">阅读更多</a>
</div>
In this example,archive.ContentThe content will be truncated to a maximum of 100 characters, and all HTML tags that are opened but not closed before the truncation point will be correctly closed. Finally, we added|safefilter.|safeThe filter here is used to tell the template engine,truncatechars_htmlThe processed HTML content is safe and does not require additional HTML entity escaping. This is very important becausetruncatechars_htmlThe integrity of the HTML structure has been ensured. If escaping is performed, it may display the original HTML tags as plain text, thereby losing the style and layout of HTML.
Considerations in practical applications.
- Choose an appropriate truncation length:
numberThe choice of value should be based on your page design and content type.The length being too short may not provide enough information, and too long may lose the meaning of the abstract.Generally, testing different values and finding the length that best balances information quantity and visual effect is the key. - Combine with other filters:
truncatechars_htmlCan be combined with other filters provided by the AnQi CMS template engine, such as cleaning up some unnecessary HTML tags before truncation (althoughtruncatechars_htmlIt will handle closing, but if you want to completely remove some tags, you can considerstriptagsorremovetags),or perform text formatting after truncation. - Make sure the source content is HTML:
truncatechars_htmlIt is designed to handle HTML content. If you pass in plain text, it will work liketruncatecharsthe same, but the advantage of HTML smart closing cannot be demonstrated. - User Experience:Truncated content is often accompanied by links such as 'Read More', 'View Details', etc., to guide users to the full content page. This is good user experience practice.
Bytruncatechars_htmlThe filter, Anqi CMS effectively solves the problem of HTML content truncation, making website content management smoother and more professional.It ensures that even when the content is simplified, the visual consistency and technical stability of the page remain consistent.
Frequently Asked Questions (FAQ)
1.truncatechars_htmlandtruncatewords_htmlWhat is the difference?
truncatechars_htmlIt is truncated based on the number of characters, it calculates all characters (including visible characters and characters in HTML tags), and then smartly closes the tags. Andtruncatewords_htmlIt is truncated based on the number of words, it will identify the words in the text and truncate by the number of words, and it will also intelligently close HTML tags.Which one to choose depends on the logic of content truncation you want: is it strictly by character length, or by semantic word count.
2. Why do you need to addtruncatechars_htmlthen it needs to be added|safeFilter?
truncatechars_htmlThe filter itself ensures that HTML tags are properly closed, but it still returns a string.The AnQiCMS (and most template engines) for security reasons, defaults to escaping all output strings to prevent XSS attacks.This means<p>It may be escaped into<p>which causes HTML tags not to render properly. Therefore, using|safea filter is to explicitly inform the template engine,truncatechars_htmlThis HTML content has been verified and is safe, it can be output in its original HTML form without escaping.
3.truncatechars_htmlCan it handle all complex HTML structures, such as deeply nested tables or script tags?
truncatechars_htmlIt is designed to handle common HTML structures and maintain their integrity as much as possible during truncation. For most standard text and simple layout tags such asp,div,span,strong,em,ul,li,a,imgIt can work well, making sure that the tags are correctly closed.However, for extremely complex or irregularly structured HTML (such as deeply nested tables, incomplete script tags, CSS style blocks, etc.), although it tries to avoid destroying the page, it may still produce results that do not completely meet expectations in some extreme cases.It is usually recommended to maintain the good structure of the original HTML content to achieve **truncation effects.