In Anqi CMS template development, the flexibility of content display often determines the quality of user experience.For displaying long text content, we often need to perform formatting, such as automatic line breaks and line numbering.This raises a common question: The security CMS provided by AnqiwordwrapFilters andlinenumbersCan the filter be used together? The answer is affirmative, they can not only be used together, but also can play a very practical effect in specific scenarios.

UnderstandingwordwrapThe principle of the filter.

First, let's take a look backwordwrapThe function of the filter. The main function of this filter is to automatically wrap text according to the specified length. When a piece of text is too long and exceeds the preset character count,wordwrapAttempts to insert a newline at the nearest word boundary (usually a space) to avoid text overflowing the container or breaking the page layout.

For example, if you have a long English paragraph and want to display a maximum of 20 characters per line, you can use it like thiswordwrap:

{{ "This is a very long sentence that needs to be wrapped."|wordwrap:20 }}

This code will process the original text into a form similar to the following:

This is a very long
sentence that needs
to be wrapped.

It should be noted that,wordwrapThe filter does not typically insert line breaks in the middle of Chinese characters when processing Chinese text, as there are no spaces between consecutive Chinese characters to serve as word boundaries.It tries to maintain the integrity of Chinese words, or breaks the line at natural breakpoints such as punctuation marks.

Get to knowlinenumbersFilter

Next, let's take a look at it.linenumbersA filter. As the name suggests,linenumbersThe purpose is to add line numbers to each line of text. This is very useful for displaying code snippets, poems, or any content that needs to be referenced by line.The line number usually starts from 1 and is displayed at the beginning of each line in the format "1.", "2.", etc.

If you have a text that already contains line breaks and you want to add line numbers to it, you can do it like thislinenumbers:

{{ "第一行文本第二行文本第三行文本"|linenumbers }}

The processed text will be displayed as:

1. 第一行文本
2. 第二行文本
3. 第三行文本

Combine cleverlywordwrapandlinenumbers

Now let's return to our core question: Can these filters be used together?It is absolutely possible. In the Anqi CMS template engine, filters support chaining, which means you can take the output of one filter as the input of another.

When you need to add line numbers after auto-wrapping long text, the correct order is to apply firstwordwrapA filter that allows text to be split into multiple lines based on a specified length, and then passes these processed multiple lines of text tolinenumbersa filter for numbering.

The specific usage is as follows:

{% set long_content = "安企CMS是一个基于Go语言开发的企业级内容管理系统,致力于提供高效、可定制、易扩展的内容管理解决方案。它拥有多站点管理、灵活的内容模型、多语言支持、伪静态和SEO优化等功能。这使得它成为中小企业和内容运营团队的理想选择。" %}
<pre>
{{ long_content|wordwrap:15|linenumbers }}
</pre>

In this example code, we first define a variable namedlong_contentwhich contains a long piece of Chinese text. Next, we applywordwrap:15Limit each line to 15 characters (the specific break point depends on spaces or Chinese punctuation), thenwordwraptake the output aslinenumbersthe input.

The final display effect will be:

1. 安企CMS是一个基于Go
2. 语言开发的企业级内容
3. 管理系统,致力于提供
4. 高效、可定制、易扩展
5. 的内容管理解决方案。
6. 它拥有多站点管理、
7. 灵活的内容模型、多语言
8. 支持、伪静态和SEO
9. 优化等功能。这使得它
10. 成为中小企业和内容
11. 运营团队的理想选择。

As the result shows,wordwrapSuccessfully performed the initial line break of Chinese text (although not strictly every 15 characters), followed bylinenumbersAccurately adds numbering to each line. This combination provides a clearer and more readable layout when displaying code blocks, document summaries, or any long text that requires structured presentation.

In practice, you should adjust flexibly according to the specific language, length, and expected final display effectwordwrapThe length parameter, and test its performance under different content.The Anqi CMS template engine's chained filter call provides a powerful tool for content presentation, making good use of it can greatly enhance the professionalism and user-friendliness of the website content.

Frequently Asked Questions (FAQ)

Q1: Combine usewordwrapandlinenumbersIs the priority or application order important?

A:Yes, the application order is very important. It is usually recommended to usewordwrapto wrap text at a specified length first, and then applylinenumbersAdd line numbers to these already newline-separated lines. If the order is reversed,linenumbersit may first number the natural lines of the original text, andwordwrapLater, these lines will be changed to change the structure, causing the numbering to be inconsistent with the actual display or logic.

Q2:linenumbersCan the filter number the content containing HTML tags?

A: linenumbersThe filter will simply number each line (including the HTML tags) as a whole. If you want to number only the plain text content and ignore the structure of the HTML tags, you may need to usestriptagsorremovetagsRemove HTML tags with this filter and then applywordwrapandlinenumbers. However, it depends on your specific needs and the final display effect, as removing tags may lose the semantic structure of the content.

Q3: If the text contains a lot of Chinese,wordwrapHow will the filter process? Will it perform precise length line breaks like it does for English words?

A:For continuous Chinese text,wordwrapThe filter mainly identifies 'words' based on spaces and performs line breaks. Since Chinese characters are continuous without spaces,wordwrapThe filter usually does not force a line break in the middle of Chinese characters, even if the length of the Chinese paragraph exceeds the specified value.It tries to maintain the integrity of Chinese words, or breaks lines at natural breakpoints such as punctuation marks.Therefore, when dealing with a large amount of Chinese text, you will find that its line break effect may not be as precisely in compliance with the character length limit as English text.