How can I display long text content automatically with line breaks at specified lengths in the Anqi CMS template?

Calendar 👁️ 74

In website operation, we often encounter situations where we need to display long text content within limited layout space.If not processed, these texts may cause page layout chaos and affect the user's reading experience.AnQiCMS (AnQiCMS) provides flexible template functions and rich filters to help us easily solve this problem.This article will discuss in detail how to display long text content with automatic line breaks at specified lengths in the Anqi CMS template, and introduce the methods for truncating related content.

Understanding the need for long text processing

Imagine, on a list page of articles, if the introduction area of each article is too long, it will occupy the position of other elements, even exceed the container boundary.Or on the product detail page, the description of a product parameter is very detailed, but also hope that it can be displayed neatly.At this moment, automatic line breaks or truncation display is particularly important.The AnQi CMS template engine supports syntax similar to Django, allowing us to easily format and process text content through powerful filters.

Core strategy: utilizingwordwrapThe filter implements automatic line wrapping

The main tool for handling long text automatic line wrapping in the Anqi CMS template iswordwrapFilter. This filter can automatically insert line breaks in a long text content according to the specified character length, thus visually achieving neat arrangement of the content.

wordwrapThe principle of the filter.

wordwrapThe core function of the filter is to insert line breaks at appropriate blank spaces in the text content based on the given number length.It will prioritize line breaks based on words (separated by spaces).For example, if you set a line break every 50 characters, the filter will try to break at the word boundary near the 50th character to maintain the integrity of the word.

It should be noted that if the text contains consecutive Chinese characters without spaces between them,wordwrapThe filter will not force line breaks in the middle of these consecutive Chinese characters. This is because the filter defaults to using spaces as word separators.

How to usewordwrapFilter

wordwrapThe basic usage syntax of the filter is very intuitive:

{{ 你的文本变量 | wordwrap: 指定长度 }}

Among them,你的文本变量Is the long text you want to process,指定长度It is an integer representing how many characters you want to display per line after.

In AnQi CMS, long text content usually comes from document details (archive.Content), category description (category.Content) or single page content (page.Content)Fields. Suppose you want to display a summary part of the article content in a sidebar on the article detail page, and you want it to wrap automatically every 40 characters or so, you can do it like this:

First, througharchiveDetailTag to retrieve article content:

{% archiveDetail articleContent with name="Content" %}

Then, toarticleContentapplywordwrapFilter, and to ensure that HTML tags are parsed correctly (if the content contains HTML), add:safeFilter:

<div style="width: 300px; border: 1px solid #ccc; padding: 10px;">
    <h4>文章摘要</h4>
    <p>{{ articleContent | wordwrap: 40 | safe }}</p>
</div>

This code will try to convert:articleContentThe text should be wrapped every 40 characters or so and the processed content should be output safely to the page.

Auxiliary strategy: utilizetruncatecharsandtruncatewordsFilter to extract content

In addition to automatic line breaks, sometimes we would like to directly extract the content and add an ellipsis at the end, which is particularly common when displaying list introductions. Anqi CMS providestruncatecharsandtruncatewordsFilter to meet this requirement.

truncatecharsandtruncatewordsDifference

  • truncatechars: Truncate based on the number of characters. For example,{{ content | truncatechars: 100 }}The content will be truncated to 100 characters (including an ellipsis), regardless of how many words are contained within these 100 characters.
  • truncatewords: Truncated based on the number of words. For example,{{ content | truncatewords: 20 }}It will truncate the first 20 words and add an ellipsis at the end.

If your content may contain HTML tags and you wish to retain the integrity of these tags during extraction, preventing the destruction of the HTML structure, you can use their HTML versions:truncatechars_htmlandtruncatewords_html.

How to use the truncate filter

Assuming you need to display the first 150 characters of each article as a summary on the article list page:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
            <p>{{ item.Description | truncatechars: 150 | safe }}</p>
            {# 如果 item.Description 可能含有HTML,建议使用: #}
            {# <p>{{ item.Description | truncatechars_html: 150 | safe }}</p> #}
            <a href="{{ item.Link }}">阅读更多 &raquo;</a>
        </div>
    {% empty %}
        <p>暂无文章。</p>
    {% endfor %}
{% endarchiveList %}

here,item.DescriptionThe variable istruncatechars: 150The filter processes, ensuring the summary will not be too long.|safeIt is also for safely outputting HTML content.

Choose the appropriate text processing method

  • When to usewordwrap?When you want the long text to be visually tidy but also need to display the full text content.It modifies the content by inserting actual line breaks, which is suitable for displaying detailed descriptions or code blocks within fixed-width containers.
  • When to usetruncatecharsortruncatewords?When you need to display a brief preview or summary of text in a limited space, and want to clearly control the length of the text, while also prompting the user that the content is not fully displayed.This is very practical in list pages and card layouts.
  • When processing HTML content:If the text content contains HTML tags and you are using a truncation filter, please make sure to usetruncatechars_htmlortruncatewords_htmlto avoid damaging the HTML structure.wordwrapThe filter itself does not destroy the HTML structure, but if you expect to insert by<br/>To implement visual line breaks, while there are already other block-level HTML elements in the content, it may lead to unexpected display effects.

By flexibly using these text processing filters provided by the Anqi CMS template, we can effectively manage and optimize the display of long text content, thereby enhancing the overall aesthetics and user experience of the website.


Frequently Asked Questions (FAQ)

1.wordwrapDoes the filter work for Chinese content? What if I want Chinese to also wrap at a specified word count?

wordwrapThe filter defaults to using spaces as word separators, so it will not force line breaks in the middle of continuous Chinese characters without spaces.If you need to strictly break lines in Chinese text by word count (even in the middle of Chinese characters), the built-in filter of Anqi CMS cannot directly achieve this. In this case, you may need to consider custom template functions or further processing through front-end JavaScript.

2. Can I use them simultaneously?wordwrapandtruncatecharsFilter?

Can, you can use them together. For example, you can use it first totruncatecharscut the text length, and then use it againwordwrapTo format the extracted text with line breaks to fit specific layout requirements. The order of processing is important, usually extract first and then format. For example:{{ 你的文本变量 | truncatechars: 200 | wordwrap: 50 | safe }}.

**3.wordwrap

Related articles

How to quickly fill and display website content using the 'Content Collection and Bulk Import' function of AnQi CMS?

Today, as digital content becomes increasingly a core asset of enterprises, how to efficiently and quickly fill and update website content is an important issue facing every website operator.Manual data entry often fails to keep up with the update of massive information, it takes time and effort and is prone to errors.AnQiCMS (AnQiCMS) understands this pain point, its built-in 'Content Collection and Batch Import' feature is designed to solve this challenge, as if it is providing an automated production line for your website content operations, helping your website content to recover quickly and accurately.

2025-11-08

How to set an independent display template and TDK for a specific Tag label in Anqi CMS?

Aq CMS provides a powerful and flexible content management system, where the Tag label is not only an important tool for content classification, but also a key link for fine-grained SEO optimization and improving user experience.Many operators may want to display unique visual styles for different Tag label pages, or configure exclusive search engine optimization information (TDK) to achieve more accurate marketing goals.Next, we will delve into how to achieve this goal in Anqi CMS.Understanding Tag Label Management in AnQi CMS

2025-11-08

How does AnQi CMS manage and display website image resources, including categories and details?

In website operation, image resources play a crucial role, not only directly affecting user experience, but also being a key factor in the attractiveness of website content and search engine optimization (SEO).A high-efficient CMS system should provide a complete set of image management and display mechanisms.AnQiCMS (AnQiCMS) considers this aspect quite thoroughly, it provides a series of features to help users easily manage and optimize the image resources of their websites, whether it is from classification and archiving, intelligent processing to multi-scene display, it strives to be simple and efficient.

2025-11-08

How to retrieve and display detailed information of different user groups (such as VIP levels) in Anqi CMS template?

In Anqi CMS, implementing personalized information display based on the user's group membership, such as their VIP level, is a key step to improving website user experience and content monetization capabilities.AnQi CMS provides a powerful and flexible user group management function, and we can easily implement this requirement in the front-end template through simple template tag combinations. ### Understanding Anqi CMS User Groups and VIP System One of the core strengths of Anqi CMS is its user group management and VIP system.

2025-11-08

How to format numbers to display in Anqi CMS template (such as floating-point precision)?

In website content management, we often need to display various numerical information, such as product prices, discount percentages, and statistical data, etc.These numbers may sometimes have complex decimal places, and if displayed directly, they may not look beautiful or be difficult to read.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine, which adopts the syntax features of Django templates. Through the built-in filter (filter) function, it can conveniently format numbers, especially the precision control of floating-point numbers.

2025-11-08

How does the custom static rule of AnQi CMS affect the display of the category list URL?

In AnQiCMS, the URL structure of the website is crucial for search engine optimization (SEO) and user experience.A clear and meaningful URL can not only help search engines better understand the content of the page, but also make it easier for users to remember and share the link.Aqii CMS provides powerful pseudo-static functions, especially its custom mode, which allows users to finely control the display method of the URL for category list pages.

2025-11-08

How to enable or disable the external link filter and its display effect in the AnQi CMS background setting?

In website content operation, the management of external links is an important factor in improving user experience and optimizing search engine rankings.AnQiCMS provides a practical external link filtering feature, allowing us to flexibly control the display form of external links in the content according to our actual needs.This article will introduce in detail how to enable or disable external link filtering in the Anqi CMS background settings, as well as the display effects under different settings.Why is it necessary to manage external links in the content?

2025-11-08

How does AnQi CMS ensure the quick display of content under high traffic conditions by using the high concurrency features of the Go language?

How Go language high concurrency technology creates ultra-fast content presentation?In today's internet environment, the speed of website access directly affects user experience and the success or failure of business.Especially during peak traffic periods, how to ensure that website content can be displayed quickly and stably is a major concern for every website operator.AnQiCMS (AnQiCMS) is an content management system developed based on the Go language, which shows unique advantages in this aspect.It cleverly utilizes the inherent high concurrency characteristics of the Go language, combining a series of optimization strategies

2025-11-08