In website content operation, we often need to extract a part of long texts such as articles and product descriptions as summaries for list display or card preview.This not only effectively saves page space, but also attracts users' attention and guides them to click to view the full content.However, when these long texts contain HTML tags, simple character truncation often leads to unclosed tags, which can破坏页面布局, affect user experience.

Security CMS (AnQiCMS) understands this pain point and has built it into the template engine.truncatechars_htmlFilter, aiming 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, thus avoiding page layout issues caused by truncation.

Knowtruncatechars_htmlFilter

truncatechars_htmlAs the name implies, it is a filter for "truncating HTML by character".truncatecharsEnglishtruncatechars_htmlWhen performing the truncation operation, the input HTML string will be parsed first.It will identify which tags are open, and when the truncation position falls within a tag or a tag is not closed after truncation, it will intelligently add 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 piece of text that contains<strong>or<p>tags, after truncationtruncatechars_htmlEnsure these tags are correctly closed at the end of the extracted portion, even though these tags should have been open at the truncation point in the original text.This processing method greatly simplifies the generation process of content abstracts, allowing us to confidently display the extracted content on list pages, search result pages, and other scenarios without worrying about layout issues.

How to usetruncatechars_html

In the templates of Anqi CMS, usetruncatechars_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 the detail page of the article.archive.Contentorarchive.Description.
  • number:is an integer that specifies the number of characters to retain after truncation, this character count.includesthe final ellipsis added...).

For example, if you want to extract a summary of 100 characters from the content of a document and ensure that the HTML tags are properly closed, 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 open but unclosed HTML tags before the truncation point will be properly closed. Finally, we added|safeFilter.|safeThe role of the filter here is to inform the template engine,truncatechars_htmlProcessed 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 application

  • Choose an appropriate truncation length: numberThe selection of the value needs to be determined based on your page design and content type.The length may be too short to provide sufficient information, or too long to lose the meaning of the summary.usually, testing different values, finding the length that can balance the information volume 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 by itself, but if you want to remove some tags completely, you can considerstriptagsorremovetags),or perform text formatting after truncation.
  • Ensure that the source content is HTML: truncatechars_htmlIt is designed to handle HTML content. If you pass in plain text, it will work just liketruncatechars, but the advantages of its HTML intelligent closing cannot be demonstrated.
  • User Experience:This content is usually accompanied by links such as 'Read More', 'View Details', etc., to guide users to access the full content page. This is a good user experience practice.

Passtruncatechars_htmlFilter, the safe CMS effectively solves the problem of HTML content truncation, making website content management smoother and more professional.It ensures that the visual consistency and technical stability of the page are always consistent even when the content is simplified.


Common 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.truncatewords_htmlIt is truncated based on the number of words, it recognizes words in the text and truncates based on the word count, and also intelligently closes HTML tags.The choice depends on the logic you want to use for content truncation: is it strictly by character length, or by the number of words semantically?

2. Why do you need to addtruncatechars_htmlAdditional operations need to be added after that.|safeFilter?

truncatechars_htmlThe filter itself ensures that HTML tags are correctly closed, but it still returns a string.The CMS (and most template engines) for security reasons, defaults to escaping all output strings to prevent XSS attacks.<p>May be escaped&lt;p&gt;causing HTML tags to not render properly. Therefore, using|safea filter is to explicitly inform the template engine,truncatechars_htmlThe processed HTML content is validated and secure, 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 as much integrity as possible during truncation. For most regular text and simple layout tags (such asp,div,span,strong,em,ul,li,a,imgit, it can work well, ensuring the correct closing of the tags.However, for extremely complex or irregularly structured HTML (such as deeply nested tables, incomplete script tags, CSS style blocks, etc.), although it will try to avoid damaging the page, it may still produce results that do not fully meet expectations in some extreme cases.It is generally recommended to maintain the good structure of the source HTML content to achieve **truncation effects**.