In content operation, we often need to manage the long text content on the website in detail, whether it is for layout beauty, 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 through built-in filters, and set character length limits.
Understanding the core of long text processing: template filters
In AnQiCMS, the content display logic is mainly controlled by templates.When we talk about 'setting the character length limit for automatic text wrapping of long text', it actually refers to using the filters provided by the template engine to process the text when it is output to the front-end page, in order to achieve the expected length control and display effect.AnQiCMS's template engine is similar to Django syntax, providing a variety of practical filters for string processing.
The true 'auto-wrap':wordwrapFilter
If you want to implement a 'soft wrap', that is, to automatically insert a line break when the text reaches a certain length while trying to keep the integrity of the words, thenwordwrapThe filter is an excellent choice. It will insert line breaks at the specified character length limit in the text at spaces, thus visually forming a line break effect.
How to use:
wordwrapThe filter usage is very intuitive, you just need to add it after the variable in the output text|wordwrap:数字The 'number' here 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 break lines at words or spaces:
AnQiCMS致力于为
企业内容营销和
全球化布局提供
强有力的支撑
Please note the details:
wordwrapThe filter mainly uses spaces to identify line breaks. This means that if there are continuous and uninterrupted Chinese, Japanese, or Korean characters (CJK characters) in your text, even if they exceed the set length,wordwrapIt will not force line breaks in the middle of these consecutive characters, but will wait for the next space to appear.If you need a more strict character length limit, even Chinese characters need to be forcibly truncated, you may need to consider other filters.
Precise length control:truncatecharsSeries filter
In many cases, what we need is not a simple newline, but to truncate the text to a specified length and add an ellipsis (…), commonly used in article summaries, list previews, or SEO descriptions. AnQiCMS provides for thistruncatecharsandtruncatechars_htmlThese two filters.
truncatechars: Truncate plain text by character count
This filter calculates the number of characters from the beginning of the string, and once it reaches the length you set, it truncates the text and adds an ellipsis at the end.It is suitable for processing plain text without any HTML tags.
How to use:
Similarly, use the output variable after|truncatechars:数字Just do it.
{# 假设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 directlytruncatecharsMay destroy HTML structure, causing the page to display abnormally.truncatechars_htmlThe filter can solve this problem well, while truncating characters, it will intelligently close all unclosed HTML tags to ensure that the output HTML fragment is still valid.
How to use:
When outputting rich text content, using|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 filter
If you feel that truncating by characters may not be natural, especially in the English context, you hope that the text is truncated by complete words, AnQiCMS also providestruncatewordsandtruncatewords_htmlfilter.
truncatewords: Truncate plain text by word count
This filter calculates the number of words in the text, truncates it after reaching the specified number, and adds an ellipsis.
How to use:
Use after outputting a plain text variable|truncatewords:数字.
{# 假设archive.Description是英文纯文本,按单词截断 #}
<p>{{ archive.Description|truncatewords:10 }}</p>
truncatewords_html: Truncate HTML text by word count while preserving structure
withtruncatechars_htmlsimilar,truncatewords_htmlSimilarly, the HTML tags will be intelligently handled when truncating HTML text by word count to avoid structure damage.
How to use:
Use when outputting rich text variables|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 (no HTML included): Recommended to use
truncatecharsIt can precisely control the length of characters, ensuring that key information is displayed within a limited space. - For directory generation or navigation hints in article details (excluding HTML)If you need to soft wrap according to 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 content: Must use
truncatechars_htmlortruncatewords_htmland combining|safeFilter, so that it can control the length and avoid the destruction of HTML structure - When processing English content: If you want to truncate while keeping the integrity of the word,
truncatewordsortruncatewords_htmlit will provide a more natural reading experience.
These template filters of AnQiCMS provide you with great flexibility in content display, allowing you to choose the most suitable filter for your website based on specific page design and content type.By making reasonable use of these tools, your website content will be neater, more readable, and better able to adapt to various display needs.
Frequently Asked Questions (FAQ)
Q1: Where should I set these long text length limits in AnQiCMS?A1: These length limits are not set globally in the background, but rather in your website template files, specifically at each variable output location where long text needs to be displayed. For example, inarchiveListWhen displaying article summaries, 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 will convert Markdown content to HTML before storing or outputting.When you use these filters in the template, they will act on the final string or HTML content.It is still recommended to use if you need to truncate HTML after Markdown conversion_htmlsuffix, such astruncatechars_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