In content operation, we often need to manage the long text content on the website in a fine manner, whether it is for the beauty of the layout, improving the user reading experience, or optimizing the search engine display, controlling the display length of the text is very important.AnQiCMS as a powerful content management system, provides a very flexible template mechanism, allowing us to easily implement automatic line breaks or truncation of long text with built-in filters, and set the character length limit.

Understanding the core of long text processing: template filters

In AnQiCMS, the display logic of content is mainly controlled by templates.When discussing the 'set the character length limit for automatic line breaks in long text', it actually refers to using the filter provided by the template engine to process the text when the content is output to the front-end page, in order to achieve the desired length control and display effect.AnQiCMS's template engine is similar to Django syntax, providing a variety of practical filters for string processing.

The real "auto-wrap":wordwrapFilter

If you want to implement a 'soft wrap', that is, to automatically insert a newline character when the text reaches a certain length, while trying to maintain the integrity of the words, thenwordwrapThe filter is a very good choice. It will insert line breaks at the spaces in the text according to the character length limit you specify, thus creating a line break effect visually.

How to use:

wordwrapThe usage of the filter is very intuitive, you just need to add it after the variable of the output text|wordwrap:数字Here, the 'number' is the character length limit you want to display per line.

{# 假设archive.Description是一个长文本字段 #}
<p>{{ archive.Description|wordwrap:30|safe }}</p>

Effect example:

If your text is 'AnQiCMS is committed to providing strong support for enterprise content marketing and global layout', and you set the length to 10, it will try to wrap at words or spaces:

AnQiCMS致力于为
企业内容营销和
全球化布局提供
强有力的支撑

The details that need to be noticed:

wordwrapThe filter breaks lines by recognizing spaces. This means that if there are continuous characters without spaces in your text, such as Chinese, Japanese, or Korean characters (CJK characters), even if they exceed the set length,wordwrapIt will not force line breaks in these consecutive characters, but will wait for the next space to appear.If you need a stricter character length limit, even Chinese characters need to be forcibly truncated, you may need to consider other filters.

Precise control of length:truncatecharsSeries of filters

In many cases, what we need is not just a simple line break, but to accurately truncate the text to the specified length and add an ellipsis (...) at the end. This is commonly used in article summaries, list previews, or SEO descriptions. AnQiCMS providestruncatecharsandtruncatechars_htmlThese two filters.

truncatechars:Truncate plain text by character count

This filter will count the characters from the beginning of the string, and once it reaches the length you set, it will truncate the text and add an ellipsis at the end.It is suitable for processing plain text content without any HTML tags.

How to use:

Similarly, use after outputting a variable|truncatechars:数字.

{# 假设archive.Description是纯文本摘要 #}
<p>{{ archive.Description|truncatechars:50 }}</p>

Effect example:

If your text is "AnQiCMS is committed to providing strong support for enterprise content marketing and global layout", and you set the length to 20:

AnQiCMS致力于为企业内容营销...

truncatechars_html:Truncate HTML text by character count while retaining structure

When your long text content contains HTML tags (such as rich text content of article details), use it directlytruncatecharsIt may destroy the HTML structure, causing the page to display abnormally.truncatechars_htmlThe filter can effectively solve this problem, while truncating characters, it also intelligently closes all unclosed HTML tags to ensure that the output HTML fragment is still valid.

How to use:

In outputting rich text content, use|truncatechars_html:数字|safe.safeThe filter is required because it tells the template engine that this HTML content is safe and does not need to be escaped twice.

{# 假设archive.Content包含HTML内容,用于生成摘要 #}
<div>{{ archive.Content|truncatechars_html:100|safe }}</div>

Effect example:

If your text is<h3>这是一个标题</h3><p>AnQiCMS致力于为企业内容营销和全球化布局提供强有力的支撑。</p>And you set the length to 30:

<h3>这是一个标题</h3><p>AnQiCMS致力于为企业内容营销...</p>

Truncate by word:truncatewordsSeries of filters

If you feel that truncating by characters may not be natural, especially in an English context, and you hope that the text is truncated by complete words, AnQiCMS also providestruncatewordsandtruncatewords_htmlFilter.

truncatewords:Truncate plain text by word count

This filter will calculate the number of words in the text and truncate it after reaching the specified number, then add an ellipsis.

How to use:

Use after outputting plain text variable|truncatewords:数字.

{# 假设archive.Description是英文纯文本,按单词截断 #}
<p>{{ archive.Description|truncatewords:10 }}</p>

truncatewords_html:Truncate HTML text by word count while preserving structure

Withtruncatechars_htmlis similar,truncatewords_htmlTruncate HTML text by word count while intelligently handling HTML tags to avoid structure damage.

How to use:

When outputting rich text variables, use|truncatewords_html:数字|safe.

{# 假设archive.Content是英文富文本,按单词截断 #}
<div>{{ archive.Content|truncatewords_html:20|safe }}</div>

Actual application scenarios and selection suggestions

  • For the introduction or SEO description of article lists (without HTML): Recommended to usetruncatecharsIt can accurately control the character length, ensuring that key information is displayed within a limited space.
  • For generating a table of contents or navigation prompts in article details (excluding HTML)If you need to perform a soft line break based on the length of the title or abstract,wordwrapit would be a good choice, but pay attention to the continuity of Chinese characters.
  • For extracting summaries (including HTML) from rich text contentMake sure to usetruncatechars_htmlortruncatewords_htmland combine.|safea filter, which can control the length and prevent the HTML structure from being destroyed.
  • Processing English content.: If you want to truncate while maintaining word integrity,truncatewordsortruncatewords_htmlit will provide a more natural reading experience.

These template filters of AnQiCMS provide you with great flexibility in content display. You can choose the most suitable filter to optimize your website according to specific page design and content type.By using these tools reasonably, your website content will be more organized, readable, and better adapt to various display needs.


Common Questions and Answers (FAQ)

Q1: Where should I set the length limit for these long texts in AnQiCMS?A1: These length limits are not set globally in the background, but are set in your website template files, specifically at the output of each variable that needs to display long text. For example,archiveListWhen displaying the article summary, you will use{{ item.Description|truncatechars:100 }}to limit the length of the summary. This means you can set different display lengths for the same field according to the needs of different pages and modules.

Q2: If my content is in Markdown format, will these filters take effect?A2: Yes, AnQiCMS's Markdown editor converts Markdown content to HTML before storing or outputting it.When you use these filters in the template, they will act on the final string or HTML content._htmlsuffix, for exampletruncatechars_htmlTo ensure the integrity of the HTML structure.

Q3:wordwrapCan the filter force Chinese to wrap at a specified length?A3:wordwrapThe filter mainly identifies