In website content operations, we often need to truncate long article content displayed on list pages, abstracts, or card views to maintain the cleanliness and consistency of the page. However, when these contents contain HTML tags (such as paragraphs<p>, links<a>, bold<strong>, image<img>When) simple string truncation can cause problems: it may truncate the tag in the middle, causing the page to display incorrectly, or even destroy the entire HTML structure of the page.

Fortunately, AnQiCMS (AnQiCMS) understands this pain point and provides it in its powerful template enginetruncatechars_htmlA filter specifically designed to intelligently truncate text containing HTML content, while ensuring that all tags are properly closed, thus avoiding common formatting and structural errors.

Whytruncatechars_htmlIs it the key to handle HTML content truncation?

Imagine you have a rich text content with images and links that needs to be displayed on the homepage with only the first 100 characters. If you simply use the string truncation feature, the result might be something like this:

<p>这是一段包含<b>重要信息<a href="...">的文...

This truncated HTML is invalid:<b>and<a>Tags are not closed. This can cause the browser to render incorrectly, affecting user experience, and may have a negative impact on search engine optimization (SEO) because search engines tend to prefer to capture well-structured, semantically clear pages.

truncatechars_htmlThe original intention of the filter is to solve this challenge. When truncating content, it is not simply cut by the number of characters, butIntelligently parse the HTML structureIt acts like a careful editor, not only truncating text when reaching the specified character limit but also tracing back to all open but unclosed HTML tags and adding the correct closing tags.This way, regardless of how the content is truncated, the output HTML fragment is always complete and valid.

truncatechars_htmlThe principle of working (simplify complexity)

Its core lies in an embedded HTML parser. When the content flows throughtruncatechars_htmlWhen filtering, this parser tracks all open HTML tags in real time.Once the character count reaches the preset truncation length, it will immediately stop outputting the text content.At this time, the parser checks all the unclosed tags it is tracking (for example, it may find one<div>tags, one<p>tags and one<strong>The label is still in the "open" state), then close them in the correct order.Finally, a truncated but structurally complete HTML fragment will appear before you.

For example, if you have such HTML content:<div><p>你好,<b>世界</b>!这是一段很长的文本。</p><a href="#">点击这里</a></div>If set to truncate to 15 characters,truncatechars_htmlit might output something like:<div><p>你好,<b>世界</b>!这是...</p></div>Note,<a>The tag is omitted directly because it is not within the truncation range and has no unclosed ends<div>/<p>and<b>They are all correctly closed.

In the actual application of the Anqi CMS template

In the template files of Anqi CMS, usetruncatechars_htmlThe filter is very intuitive. Usually, we would combinesafethe filters together becausetruncatechars_htmlThe output is still an HTML string, it needssafeA filter to indicate to the template engine to render it as safe HTML, rather than escaping.

Assuming you need to display the summary of the article and limit it to 100 characters:

{# 假设 item.Content 包含了 HTML 格式的文章内容 #}
<div class="article-summary">
    {{ item.Content|truncatechars_html:100|safe }}
    <a href="{{ item.Link }}" class="read-more">阅读更多</a>
</div>

In this example:

  • item.ContentIs the complete HTML content of your article.
  • truncatechars_html:100Truncate content to 100 characters (including an ellipsis).
  • |safeEnsure that the truncated HTML content is correctly parsed and rendered by the browser.

Comparison with other truncation filters

AnQi CMS also provides other truncation filters, understanding their differences can help you choose better:

  • truncatechars: It is suitable for plain text content. It simply truncates by character count, without considering HTML tags, which can cause label damage if used in HTML.
  • truncatewordsAlso applies to plain text content. It truncates by word count, not character count.
  • truncatewords_html: withtruncatechars_htmlSimilar, but it truncates by word count and also ensures proper closure of HTML tags.If your requirement is to truncate HTML content by 'word' rather than 'character', it would be a better choice.

Brings significant advantages

Usetruncatechars_htmlThe benefits of the filter are multifaceted:

  1. Page layout integrityAlways output valid HTML, avoid page clutter caused by unclosed tags.
  2. Enhancing user experienceThe summary content displayed to the user is neat and professional, without any garbled characters or broken layout.
  3. Beneficial for search engine optimization (SEO)Keep the HTML structure of the page valid, which helps search engines better crawl and index content.
  4. Enhance development efficiency: Automating the complexity of HTML truncation reduces the workload of manually checking and repairing invalid HTML.

Summary

truncatechars_htmlThe filter is a very practical feature provided by Anqi CMS in content presentation.It not only simplifies the display of summaries with HTML content, but more importantly, it fundamentally solves the structural problems brought about by truncation of HTML content through an intelligent tag closing mechanism, ensuring that the website content is always presented to visitors in a professional, beautiful, and effective manner.In content operation and template development,熟练运用this filter will greatly improve the quality and maintenance efficiency of the website.


Frequently Asked Questions (FAQ)

1. If my original HTML content already has unclosed tags or structural errors,truncatechars_htmlcan it still work and fix these errors?

truncatechars_htmlThe filter mainly ensures that it*itself*does not destroy the original HTML structure during truncation and will close all unclosed tags before the truncation point. If the original HTML content itself has serious structural errors (such as tag nesting errors, illegal attribute values, etc.),truncatechars_htmlCannot fix these existing errors. It will try to process it, but the validity of its output will depend on the quality of the original input.To ensure the effectiveness, it is recommended to check the validity of the HTML before publishing the content.

2.truncatechars_htmlCan the ellipsis "..." after truncation be modified?

Based on the document description of Anqi CMS and common template engine practices,truncatechars_htmlThe ellipsis used by the default filter is “…”, and this character is usually not directly modifiable through the filter parameters.If indeed a custom ellipsis is needed, it may be necessary to perform additional string replacement operations in the template, which will increase complexity and may affect performance.It is generally recommended to follow the default ellipsis to maintain consistency.

3.truncatechars_htmlWill the character length of the HTML tag itself be calculated when truncating? For example,<p>How many characters?

truncatechars_htmlThe filter will only calculate the character length ofvisible text contentThe character length, without counting the HTML tags (such as<p>/<a>etc.) themselves and their attributes. This means that you set100A character limit refers to the actual text characters that the user can see, rather than the length of the string containing all the tag codes. This makes the truncation length more in line with human reading expectations.