What is the specific syntax of the `wordwrap` filter in the AnQiCMS template?

Calendar 👁️ 67

When displaying content on a website, we often encounter issues with long text not being displayed well on different devices, especially on mobile devices, where overly long line widths can reduce the reading experience and even damage the page layout. Anqi CMS provides a series of practical filters to help us better control the display of text, among whichwordwrapThe filter is the tool to solve this problem. It ensures that the text wraps automatically when it reaches a specified length, thereby improving the readability and aesthetic beauty of the content.

What iswordwrapFilter?

wordwrapThe filter, as the name implies, is a feature used to automatically wrap long text content according to the character length you set.Its core goal is to optimize the visual presentation of text on the front-end page, avoiding reading difficulties or layout overflow caused by overly long single-line text.By inserting a line break, it makes the text block visually neat and easier for users to read.

wordwrapThe syntax of the filter

In the AnQi CMS template system,wordwrapThere are two main ways to use filters, all of which follow the syntax style of Django template engines:

  1. Applied directly to variablesThis is the most common and intuitive way, through the pipe symbol|Apply the filter to the variable to be processed. Syntax:{{ 变量名 | wordwrap: 长度数值 }}For example, if you have a variable namedarticle.Contentand you want it to wrap at about 20 characters, you can write it like this:{{ article.Content | wordwrap:20 }}.

  2. asfilterTag usageWhen you need to process text content that is not a single variable, but a more complex template content (such as text blocks containing HTML tags, or combinations of other variables) you can usefilterLabel this part of the content, then applywordwrapFilter. Syntax:

    {% filter wordwrap: 长度数值 %}
        需要处理的文本内容
    {% endfilter %}
    

    The advantage of this method is that it can uniformly handle line breaks for any content within the tag, providing greater flexibility.

Parameter details

wordwrapThe filter accepts a required parameter:

  • 长度数值 (number)This is an integer representing the maximum number of characters you want for each line of text. When the text reaches this length, the filter will try to insert a newline character.

It should be noted that this "length" is based onwordscalculated. That is to say,wordwrapThe filter will try to insert line breaks between words.

Application scenarios and precautions

  1. Improve readabilityIn news, blog articles, product descriptions, and other areas with a lot of text, usewordwrapEffectively limit the line width to make it more in line with human reading habits and avoid long sentences that can't be seen at a glance.
  2. Layout adaptabilityIn responsive design,配合CSS styles,wordwrapIt helps the text adapt to different screen sizes, prevent text from overflowing the container, and thus maintain a tidy and consistent layout of the page.
  3. “word”and“character”:wordwrapThe default behavior is based onwordsPerform a line break. This means it will break between words with spaces.If you encounter a very long English word, or a language like Chinese, Japanese, Korean (CJK) that does not have explicit space separation, it may not wrap at the specified length precisely, but rather wait for the next 'break point' (such as a punctuation mark, or until the next space).For continuous Chinese text, it treats it as a very long 'word', causing it to not automatically wrap even if it exceeds the set length.
  4. Browser display:wordwrapThe filter will insert line breaks in the processed text (\n)。In HTML, by default, browsers ignore these newline characters and treat them as spaces.To make these line breaks actually display as line breaks on the page, you usually need:
    • Wrap the content in<pre>tags (preThe tags will preserve spaces and line breaks in the text).
    • Use CSS propertieswhite-space: pre-wrap;Or `white-space: pre-line;

Related articles

How to use the `wordwrap` filter in AnQiCMS to implement automatic line breaks for long text?

When using AnQiCMS to build and manage websites, we often need to handle text content of various lengths.Especially in today's increasingly popular responsive design, long text can lead to layout chaos on different devices, such as text overflowing the container, destroying the beauty of the page, and even affecting user experience.Fortunately, AnQiCMS's powerful template engine provides a variety of practical filters to solve such problems, including the `wordwrap` filter that can automatically wrap long text.The `wordwrap` filter is self-explanatory

2025-11-09

How to handle the impact of punctuation marks at the beginning and end of strings when using the `wordcount` filter?

In content operations, accurately counting the number of characters or words in an article is an important link in measuring the length of content, estimating reading time, and even conducting SEO optimization.AnQiCMS (AnQiCMS) provides a convenient `wordcount` filter to help us quickly implement this feature.However, during use, we may encounter a common detail problem: whether the punctuation marks at the beginning and end of strings, or attached to words, will affect the `wordcount` statistics result??Understand this and master the corresponding handling method

2025-11-09

How to quickly view the output results of the `wordcount` filter for different strings while debugging AnQiCMS templates?

When developing or debugging templates in AnQiCMS, we often need to verify the output effect of specific data or filters (filter).Especially filters like `wordcount` that may have different counting logic for text in different languages and formats.To ensure that the template works as expected, it is particularly important to quickly view the output results of the `wordcount` filter for various strings.AnQiCMS's template system is based on syntax similar to Django

2025-11-09

Does the `wordcount` filter support counting the frequency of specific "word" like the `count` filter?

During the template development and content management process of AnQi CMS, we often need to perform various text analyses, including counting the vocabulary of text or the frequency of specific words.AnQi CMS provides us with a variety of practical template filters to handle such requirements.Today, let's discuss in detail the similarities and differences between the `wordcount` filter and the `count` filter in terms of word frequency.### Understanding the `wordcount` filter's function When you need to understand the overall length of a text, or how many 'words' it contains

2025-11-09

How to set the character length limit for automatic line break of long text in AnQiCMS?

In content operations, we often need to manage long text content on websites in a fine manner, whether it is for layout aesthetics, improving user reading experience, or optimizing search engine display, controlling the display length of text is very important.AnQiCMS is a powerful content management system that 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

2025-11-09

Why does the `wordwrap` filter not automatically wrap continuous Chinese text?

When using AnQiCMS for content display, many friends may encounter a question: why does the `wordwrap` filter in the template seem unable to automatically wrap at specified lengths for continuous Chinese text, as it does for English?This often leads to unexpected extension of the page layout, affecting aesthetics and reading experience.To understand this phenomenon, we first need to deeply understand the working principle of the `wordwrap` filter in the AnQiCMS template engine, as well as the characteristics of Chinese text itself.###

2025-11-09

How to ensure that long text content does not overflow the container in AnQiCMS?

In the operation of the AnQiCMS website, displaying long text content is a common and critical issue.If handled improperly, the content may overflow its container, causing the page layout to become disordered and severely affecting user experience and the professional image of the website. 幸运的是,AnQiCMS provides a variety of tools and strategies to help us effectively manage and control the display of long text. ### Learn about content overflow: Why does it happen?Content overflow usually refers to text or images that exceed the boundary of the HTML element (such as `div`, `p`, or other containers) they are in.

2025-11-09

What is the main difference between the `wordwrap` and `truncatechars` filters in AnQiCMS?

In Anqi CMS template development, flexibly handling and displaying text content is the key to improving user experience and website aesthetics.Among them, the `wordwrap` and `truncatechars` filters are both related to text length control, but they have different focuses and use cases.Understanding the core differences between these two can help us more accurately meet our content display needs.## The `truncatechars` filter: A tool for truncating text The `truncatechars` filter, as the name implies

2025-11-09