How to configure different `wordwrap` lengths for different modules (such as articles, products) in AnQiCMS?

Calendar 👁️ 59

In daily website operations, we often need to handle various types of text content.Sometimes, to maintain the neatness of the page layout and the reading experience, we may want long text to wrap automatically at specific lengths, especially in areas such as article lists, product descriptions, and other display areas.Further, different content modules, such as the main text on the article detail page and the description in the product list, often have different requirements for line break length.AnQiCMS (AnQiCMS) provides a flexible way to meet this requirement, allowing us to configure personalized line break lengths for different modules.

In Anqi CMS, implementing automatic line breaks relies mainly on its powerful template filter function, especiallywordwrapFilter. This filter can help us automatically wrap long text content to the specified number of characters.It works by distinguishing words based on spaces in the text and wrapping to a new line when a specified length is reached.It is worth noting that when dealing with continuous Chinese text, as Chinese does not have a natural space delimiter,wordwrapThe filter may not wrap at the middle of words, but keep continuous Chinese segments uninterrupted.

Different modules (such as articles and products) should be configured differently.wordwrapLength, we cannot rely solely on a global setting becausewordwrapIt is a filter applied in the template. This means we need to control the display level of the content on the template level.The most flexible method is to combine the custom field function of Anqin CMS content model.

Step 1: Add custom fields to the content model

First, we need to enter the Anqi CMS admin interface, find the "Content Management" option under "Content Model".The AnQi CMS provides the 'article model' and 'product model' by default.We can add a dedicated one for storing for these or any other custom modelswordwrapCustom fields of length.

For example, with the "Article Model", we can click edit and then add a new field, such as named "Line Break Length" (the field name can bewordwrap_lenThis field's type can be selected as 'number' and can have a default value, such as80This means that if the article is published without a specific specification, the default line length is 80 characters. Similarly, we can also add a similar field for the "product model", such as "product description line length" (the field name can be called...product_wordwrap_lenPlease specify a default value suitable for product description40.

In this way, we have created configurable line break length options for different content modules in the background, content editors can adjust this value according to actual needs when publishing or editing articles/products.

Step two: Apply in the templatewordwrapFilter

The critical step is to apply these custom settings in the front-end template. The template files of AnQi CMS are usually stored in/templateIn the directory, different modules will have corresponding detail page templates, for example, the article detail may bearchive/detail.html, the product detail may beproduct/detail.html.

Assuming we want to display a summary of the article content in a certain area of the article detail page, and apply a custom line break length. We can do this inarchive/detail.htmlthe template like this:

{# 首先,获取当前文章的自定义换行长度字段值 #}
{% archiveDetail archiveWordwrapLen with name="wordwrap_len" %}

{# 接下来,将文章内容(或其摘要)应用wordwrap过滤器,并使用获取到的长度值 #}
<div>
    {# 假设这里是文章的简介,需要进行换行处理,并确保HTML内容安全输出 #}
    {% archiveDetail articleDescription with name="Description" %}
    <p>{{ articleDescription|wordwrap:archiveWordwrapLen|safe }}</p>

    {# 如果是文章正文的部分截取,也可以类似操作 #}
    {# ... {{ archive.Content|wordwrap:archiveWordwrapLen|safe }} ... #}
</div>

HerearchiveWordwrapLenThe variable stores the 'line break length' value we configure for the article model in the background. When it is applied toarticleDescription(article summary), the summary will automatically wrap to the specified length.|safeThe filter is very important here, it ensures that if the text contains HTML tags, these tags can be parsed correctly by the browser instead of being displayed as plain text.

Similarly, for the product module, suppose we want to display a brief description of the product on the product list page and apply the 'product description line break length' configured in the product model. Inproduct/list.htmlorproduct/detail.htmlIn the template, we can also use a similar method:

{% archiveList products with type="page" moduleId="2" limit="10" %}
    {% for item in products %}
    <div>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        {# 获取产品模型中定义的换行长度 #}
        {% archiveParams productParams with id=item.Id sorted=false %}
        {% set productWordwrapLen = productParams.product_wordwrap_len.Value|integer %}

        {# 应用wordwrap过滤器到产品描述 #}
        <p>{{ item.Description|wordwrap:productWordwrapLen|safe }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In this example of the product list, we usearchiveParamsThe tag retrieved the custom fields of each product, then converted them to integers and applied them toitem.Description. Each product description can be wrapped at different lengths according to its module settings.

Summary

By adding custom fields to the content model in AnQi CMS and using them in the template layer,wordwrapThe filter reads these field values, allowing us to easily customize the line break length for different content modules (such as articles, products).This flexible configuration method brings great convenience and higher customization to the presentation of our content, allowing the website content to maintain beauty while also adapting better to various display scenarios.


Frequently Asked Questions (FAQ)

1.wordwrapCan the filter be applied to any text field, not just the content description of articles or products?

Yes,wordwrapThe filter can be applied to any string variable output in the template, including article titles, category descriptions, single-page content, and so on.If you wish to apply automatic line breaks to a string based on character length, you can use this filter. For example,{{ category.Description|wordwrap:50 }}The category description will be wrapped at 50 character length.

2. If I set a customwordwraplength for a module, but the field is not filled in, what will happen?

If a custom field (such aswordwrap_len) No value has been filled in, so when trying to access it in the template, it may return an empty value or0In this case,wordwrapThe filter may not work as expected or may cause no line break effect.In order to avoid this situation, you can set a default value for this custom field in the background, or add a conditional judgment in the template. For example, if the field value is empty, use a preset alternative length.

{% archiveDetail archiveWordwrapLen with name="wordwrap_len" %}
{% set currentWordwrapLen = archiveWordwrapLen|integer %} {# 转换为整数,如果为空会是0 #}
{% if currentWordwrapLen == 0 %}{% set currentWordwrapLen = 80 %}{% endif %} {# 如果是0,设为默认80 #}
<p>{{ articleDescription|wordwrap:currentWordwrapLen|safe }}</p>

Why doesn't my Chinese text automatically wrap at the specified length but remains a long continuous string?

wordwrapThe filter mainly determines the line break point by identifying spaces in the text. Since the Chinese writing habit does not use spaces to separate words, therefore when faced with continuous Chinese text, wordwrapThe filter treats it as an indivisible whole until it encounters an English word, punctuation, or reaches the end of a paragraph, at which point it may wrap. If you need Chinese text to wrap at a specified length, even in the middle of a word, wordwrapThe filter may not be the **choice, or you may need to combine it with other front-end JavaScript libraries to achieve more refined Chinese tokenization and line breaking processing.

Related articles

Does the `wordwrap` filter support automatic line wrapping of HTML content in AnQiCMS templates?

In AnQi CMS template development, the `wordwrap` filter is a small utility used to handle automatic line breaks in long text, designed to make plain text content automatically break lines at specified widths when displayed to avoid content overflow layout.However, when we consider applying it to content that includes HTML tags, the situation becomes somewhat complex.According to the understanding of Anqi CMS template filters, the `wordwrap` filter mainly identifies spaces in the text to determine word boundaries, and then performs line breaks based on the set character length. For example

2025-11-09

How to debug the issue of the `wordwrap` filter not working in AnQiCMS?

In AnQiCMS template development, the `wordwrap` filter is a very practical feature that helps us control the display of long text, prevent content overflow, and improve the readability of the page.However, sometimes we may find that it does not work as expected, resulting in the text not automatically wrapping.In this situation, there is no need to rush. This is usually due to a misunderstanding of the `wordwrap` principle or incorrect usage.Understanding the `wordwrap` function principle first

2025-11-09

How to use `wordwrap` to enhance the reading experience on the AnQiCMS article detail page?

## Optimizing AnQiCMS Article Detail Page Long Content Reading Experience: Efficient Application of `wordwrap` Filter In website operation, we often need to publish long articles with a large amount of text, such as in-depth reports, tutorials, or detailed product introductions.This content is rich in information and is the key to attracting and retaining readers, but if the layout is not done properly, especially in a multi-device environment, the long line width is easy to make readers feel tired of reading, even leading them to give up reading halfway.In AnQiCMS, we can cleverly utilize the powerful template system provided by

2025-11-09

In AnQiCMS product description, how does the `wordwrap` filter optimize text formatting?

On e-commerce websites or product display pages, a good product description not only attracts the attention of users but is also a key factor in driving conversions.However, lengthy or poorly formatted text, even if the content is excellent, may deter users.Fortunately, AnQiCMS provides a series of powerful template functions that help us finely control content display, among which the `wordwrap` filter is an unobtrusive but extremely effective tool that can significantly optimize the layout of long texts, making your product description more attractive.### `wordwrap` filter

2025-11-09

What is the optimization effect of the `wordwrap` filter on the AnQiCMS mobile display?

On a website built with AnQiCMS, the user experience on mobile devices is crucial.With the popularity of smartphones and tablets, users are increasingly accustomed to browsing content on various screen sizes.However, if the website content is not well optimized, it may appear with chaotic layout, text overflow and other issues on small screen devices, seriously affecting the reading experience.Among many optimization methods, the `wordwrap` filter provided by AnQiCMS plays an indispensable role in mobile display optimization with its unique text processing capabilities.###

2025-11-09

How to make the long text in AnQiCMS keep word integrity after automatic line break?

In website content operation, we often encounter such situations: a long text, especially when it contains long words, links without spaces, or technical terms, when it is automatically wrapped on the web page, it may be cut off abruptly in the middle of the word, affecting the reading experience and the beauty of the page.This not only makes the content unprofessional, but may also hinder the effective communication of information.How can we cleverly handle such long text in AnQiCMS to ensure that the words remain intact during automatic line breaks?Understanding this problem, we first need to understand the basic logic of the browser when handling text wrapping

2025-11-09

How to use the `wordwrap` filter in AnQiCMS along with other text processing filters (such as `linebreaks`)?

In the template design of AnQi CMS, we often encounter scenarios where it is necessary to format long text content.Whether it is the main text of an article, product description, or user comments, the clear presentation of text directly affects user experience.Safe CMS provides a variety of text processing filters, among which `wordwrap` and `linebreaks` (or `linebreaksbr`) are two great tools for handling text wrapping and paragraph structure.Each has its focus, but combined cleverly, it can make your content show better structure and readability.

2025-11-09

What is the practice of applying the `wordwrap` filter to the `archive.Content` field in AnQiCMS?

In AnQiCMS, the `archive.Content` field carries the core part of the website content, usually including article details, product descriptions, and other rich text information.How to properly handle the layout of fields that may contain a large amount of text and images, especially the automatic line break of text, is crucial for improving the user's reading experience.This article will discuss the practice of applying the `wordwrap` filter to the `archive.Content` field in AnQiCMS, helping content operators and template developers to better utilize this feature

2025-11-09