How to use the `wordwrap` filter in AnQi CMS template to automatically wrap long text and avoid layout chaos or overflow?

Calendar 👁️ 76

In website content operation, we often encounter scenarios such as article details, product descriptions, or user comments, where long text content causes page layout to be disordered, even overflowing the container, seriously affecting user experience.Especially when a continuous English word without spaces or a long string of Chinese appears in a narrow area, the default line breaking mechanism of the browser may not meet our design requirements.Fortunately, AnQi CMS provides a very practical tool for template developers to elegantly solve this problem - that iswordwrapfilter.

ThiswordwrapThe filter, as the name implies, is used to process long text into 'word wrapping'.It will base on the number you set (i.e., the maximum number of characters per line), find spaces in the text to break lines, thus avoiding the text being too long in one line.By using it reasonably, we can ensure that long text automatically wraps without破坏 the page structure, making the content present more neat and beautiful.

How to usewordwrapFilter

Using in Anqi CMS template,wordwrapThe filter is very intuitive, its basic syntax is:{{ 变量 | wordwrap: 长度 }}.

  • 变量It refers to the text content you want to be wrapped in a new line, such as the main text of an article, description, etc., which is usually obtained through template tags, for example{{ archive.Content }}.
  • 长度It is an integer representing the maximum number of characters you want each line of text to display. When the text length exceeds this number, the filter will try to wrap at the nearest space.

This means if your content is a continuous string of English characters without spaces, or a continuous segment of Chinese text,wordwrapThe filter may not wrap as expected in the middle. This is because the filter mainly relies on the text inspacesTo judge the line break point. For Chinese content, since there are no spaces between characters, this filter defaults to not breaking in the middle of words.If you need to force a newline for Chinese or English without spaces, it may be necessary to combine other methods or CSS properties.

Here are some actual examples to help you understand its usage:

Example 1: Basic English text wrapping

Assuming you have an English description, you want each line to be no more than 15 characters (wrapping at spaces):

{% set longText = "This is a very long sentence that needs to be wrapped beautifully on the webpage." %}
<p>{{ longText | wordwrap: 15 }}</p>

This code may output content similar to this:

This is a very
long sentence
that needs to
be wrapped
beautifully on
the webpage.

As you can see,wordwrapThe filter performed line breaks when encountering spaces, maintaining the integrity of the words.

Example 2: Combine<pre>Tags andlinebreaksbrFilter, retain line break effect

Sometimes, we hope not only to break lines, but also to be able to reflect directly in HTML<br/>The effect of the tag, especially when the text content itself may contain newline characters. Anqicms provideslinebreaksbra filter that can remove newline characters from text\nto HTML<br/>.

<pre>{% filter wordwrap: 20 %}{% lorem 50 w %}{% endfilter %}</pre>

or combinelinebreaksbr:

<p>{{ "Lorem ipsum dolor sit amet, consectetur adipiscing elit." | wordwrap: 10 | linebreaksbr | safe }}</p>

This has been usedloremTag generates random Latin text for demonstration.wordwrap: 10It will limit the maximum length of each line to 10 characters,linebreaksbrThen it will convert the newly generated newline characters to<br/>,safeThe filter ensures that HTML tags can be normally parsed and displayed by the browser.

Example 3: The limitation of line breaks in Chinese text

If you try to use a continuous Chinese textwordwrapFilter:

This is a very very long Chinese text, which does not contain any spaces, so the wordwrap filter may not be able to perform effective line breaks based on spaces.

Related articles

How to quickly count the number of words (or Chinese words) in the content of an article in AnQi CMS using the `wordcount` filter, for SEO or reading time evaluation?

In website operation, the quality and presentation of content are crucial.The number of words in an article not only affects its performance in search engines, but is also a key indicator for evaluating user reading experience and estimating reading time.No doubt, manually counting the number of words in content is a tedious task, especially when the amount of content is vast.Luckily, AnQiCMS (AnQiCMS) has provided us with a small yet powerful tool—the `wordcount` filter, making content word count easy. ### The Importance of Word Count in Content

2025-11-08

How to center, left-align, and right-align Chinese string with specified width using `center`, `ljust`, and `rjust` filters in the Anqi CMS template?

When building and maintaining website content, we often need to pay attention to the visual presentation of information, especially the alignment of text content.It is particularly important to accurately control the centering, left alignment, or right alignment of strings, whether it is for the neatness and beauty of the table or to maintain visual balance within a fixed width area.AnQiCMS with its flexible and powerful template engine, provides us with several very practical filters to easily achieve this goal, even for strings containing Chinese characters.

2025-11-08

How to split a normal string into an array of individual characters using the `make_list` filter in the Anqi CMS template for more refined text processing?

In the world of AnQi CMS templates, we often need to process content in various ways, sometimes by paragraphs, sometimes by sentences, and sometimes, we need to refine the text more finely, down to every character.When conventional string processing methods fail to meet such refined needs, the `make_list` filter can excel, as it can split a common string into an array of individual characters, providing unprecedented flexibility for template designers.

2025-11-08

How to use the `split` filter to convert the custom delimiter string returned by the Anqie CMS backend (such as "keyword1|keyword2") into an array for easy traversal?

In AnQi CMS content management practice, flexibility is the key to improving efficiency and achieving personalized display.Sometimes, we may encounter such a requirement: the backend returns a string of text separated by a special symbol through a custom field, such as a product introduction may have multiple related keywords, and it is stored as 'keyword1|keyword2|keyword3' such a format.How can I beautifully split this string of text into individual keywords and make them convenient for display or iteration in a front-end template?

2025-11-08

How to automatically identify URLs and email addresses in the text and convert them into clickable links in `urlize` and `urlizetrunc` filters in AnQi CMS?

In Anqi CMS, we often encounter scenarios where we need to display website or email addresses in the article content, comments, or other user input text.If these addresses are only plain text, users will not be able to click and jump directly, which will greatly reduce the user experience of the website.Fortunately, Anqi CMS is built-in with powerful template filters, among which `urlize` and `urlizetrunc` are specifically designed to solve this problem. They can automatically convert identified URLs and email addresses into clickable HTML links.

2025-11-08

How to safely output HTML code passed from the backend in Anqi CMS template using the `safe` filter without escaping?

Build and manage websites in AnQi CMS, we often make good use of its powerful and flexible template system to present colorful content.The template engine of AnQi CMS has adopted the syntax of Django templates, which brings us a familiar development experience and powerful features.However, when handling HTML code passed from the backend to the frontend, we encounter an important security mechanism: **automatic escaping**.

2025-11-08

The `dump` filter has what practices in debugging Anqi CMS templates, and how to clearly view the structure and value of complex variables?

During the template development process of AnQi CMS, we often encounter the need to view variable content and structure, especially when dealing with complex objects passed from the background.If you cannot clearly know what is inside the variable, debugging will be as difficult as a blind man feeling an elephant.The AnqiCMS adopts the Pongo2 template engine syntax similar to Django, providing rich tags and filters to help us build dynamic pages, one extremely powerful debugging tool is the `dump` filter.### `dump` filter

2025-11-08

How to concatenate the article title (Title) with the system-defined website name (SiteName) through the `tdk` filter and output it to the <title> tag in the Anqi CMS template?

In website operation, the importance of the `<title>` tag is self-evident. It is not only the key for search engines to understand the theme of the page, but also the content that users see first in the search results.A well-crafted page title that can effectively increase click-through rates and have a positive impact on SEO optimization.Therefore, how to organically integrate the core title of the article with the brand name of the website to form an accurate and attractive `<title>` tag is a fundamental and important link in the design of website templates.AnQiCMS as a feature-rich enterprise-level content management system

2025-11-08