In the template development of AnQi CMS, the flexibility of content display often determines the quality of user experience.For the display of long text content, we often need to perform formatting operations, such as automatic line breaks and adding line numbers.wordwrapfilters andlinenumbersCan the filters be used together? The answer is affirmative, they can be combined and can even be very practical in certain scenarios.
UnderstandingwordwrapThe working principle of the filter
Firstly, let's review.wordwrapThe function of the filter. The main function of this filter is to automatically wrap text according to the specified length. When a paragraph of text is too long and exceeds the preset character count,wordwrapAttempts to insert a line break 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 very long English paragraph and you 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 is worth noting 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 characters to act as word boundaries.It will try to maintain the integrity of Chinese words, or break the line at natural breakpoints such as punctuation marks.
KnowlinenumbersFilter
Next, let's understandlinenumbersFilter. As the name suggests,linenumbersThe function 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 quoted by line.The line number usually starts from 1 and is displayed at the beginning of each line in the format of “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:
{{ "第一行文本\n第二行文本\n第三行文本"|linenumbers }}
The processed text will be displayed as:
1. 第一行文本
2. 第二行文本
3. 第三行文本
A clever combinationwordwrapandlinenumbers
Now let's return to our core issue: Can these two filters be used together?completely.In the template engine of AnQi CMS, 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 automatic line wrapping of long text, the correct order is to apply it firstwordwrapFilter, which allows text to be generated into multiple lines based on a specified length, and then passes these processed multiple-line texts tolinenumbersnumber the filter.
The specific usage is as follows:
{% set long_content = "安企CMS是一个基于Go语言开发的企业级内容管理系统,致力于提供高效、可定制、易扩展的内容管理解决方案。它拥有多站点管理、灵活的内容模型、多语言支持、伪静态和SEO优化等功能。这使得它成为中小企业和内容运营团队的理想选择。" %}
<pre>
{{ long_content|wordwrap:15|linenumbers }}
</pre>
In this sample code, we first defined a variable namedlong_contentwhich contains a longer Chinese text. Next, we appliedwordwrap:15Make each line have a character limit of about 15 (the specific break point depends on spaces or Chinese punctuation), and thenwordwraptake the output aslinenumbersinput.
The final display effect will be:
1. 安企CMS是一个基于Go
2. 语言开发的企业级内容
3. 管理系统,致力于提供
4. 高效、可定制、易扩展
5. 的内容管理解决方案。
6. 它拥有多站点管理、
7. 灵活的内容模型、多语言
8. 支持、伪静态和SEO
9. 优化等功能。这使得它
10. 成为中小企业和内容
11. 运营团队的理想选择。
It can be seen from the results that,wordwrapSuccessfully performed an initial line break on Chinese text (although not strictly every 15 characters), followed by,linenumbersAlso accurately added numbering to each line.This combination can provide a clearer and more readable layout when displaying code blocks, document summaries, or any long text that needs to be presented structurally.
In practical applications, you should flexibly adjust according to the specific language, length, and expected final display effectwordwrapThe length parameter and test its performance under different content.The filter chaining of the AnQi CMS template engine provides a powerful tool for content presentation, and proper use can greatly enhance the professionalism and user-friendliness of website content.
Common Questions (FAQ)
Q1: Combined usewordwrapandlinenumbersWhen, is the priority or application order important?
A:Yes, the application order is very important. It is usually recommended to usewordwrapto break the text into lines of a specified length first, and then applylinenumbersThe English translation of 'auto' is 'English'. Adding line numbers to these already newline-separated lines. If the order is reversed,linenumbersit might first number the natural lines of the original text instead,wordwrapThe structure of these lines will be changed, which may cause the numbering to be inconsistent with the actual display or logic to be confused.
Q2:linenumbersCan the filter number the content that includes HTML tags?
A: linenumbersThe filter will simply number each line of content (including any 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 filters, then applywordwrapandlinenumbers. However, this depends on your specific needs and the final display effect, because removing tags may lose the semantic structure of the content.
Q3: If the text contains a lot of Chinese,wordwrapFilter how to handle? Will it perform precise length line breaks like processing 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 between them,wordwrapThe filter usually will not force a line break in the middle of Chinese characters, even if the length of the Chinese paragraph exceeds the specified value.It will try to maintain the integrity of Chinese words, or wrap lines at natural breakpoints such as punctuation marks.Therefore, when processing a large amount of Chinese text, you may find that its line break effect may not be as accurately adhered to the character length limit as English text.