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 on different devices can lead to layout chaos, such as text overflowing the container, destroying the beauty of the page, and even affecting the user experience.Fortunately, AnQiCMS's powerful template engine provides a variety of practical filters to solve such problems, including the ability to automatically wrap long text.wordwrapfilter.

wordwrapThe filter, as the name implies, is mainly used to automatically wrap long text content to the specified length.This is like setting a 'width limit' for a paragraph of text, when the text reaches this width, it will automatically find a suitable breakpoint for line breaks, ensuring that the text is displayed within the preset range, thus making the page layout more tidy and improving the readability of the content.

How to applywordwrapFilter

Using in AnQiCMS template fileswordwrapThe filter is very intuitive. Its basic syntax is{{ obj|wordwrap:number }}.

  • obj: Represents the text content variable you want to process, which can be any string type of data such as article title, description, content snippet, etc.
  • numberThis is an integer representing the maximum number of characters you want to display per line of text. When the text reaches this character limit,wordwrapthe filter will try to insert a line break.

Let's understand its usage through some specific examples:

Assuming you have a variablearchive.DescriptionIt stores a long article description, and you want it to automatically wrap every 20 characters:

<p>{{ archive.Description|wordwrap:20 }}</p>

This code will convertarchive.DescriptionThe content processed will be displayed in a paragraph<p>The length of each line of text can be a maximum of 20 characters

If you want the line break effect to be truly displayed in the HTML page<br/>Present in the form of a label, it needs towordwrapthe filter meetslinebreaksbrFilter combined use.linebreaksbrThe filter will replace line breaks (`)转换为 HTML 的
` tag.

<p>{{ archive.Description|wordwrap:20|linebreaksbr|safe }}</p>

This is something that needs to be noted,|safeThe filter is indispensable because it tells the template engine that this content is safe and does not need to be HTML escaped, so<br/>Tags can be parsed correctly by the browser and displayed as line breaks.

For longer or multi-processed text, you can also use{% filter %}tag blocks to wrap.wordwrapFilter, so it can process all content contained in the tag block:

{% filter wordwrap:30 %}
    这里是一段非常非常长的文本内容,它将会被wordwrap过滤器自动在每30个字符左右进行换行处理,以确保在页面上的显示效果更加美观和易读。
    您可以使用这个过滤器来优化文章摘要、产品描述等,让它们在有限的显示空间内保持良好的视觉呈现。
{% endfilter %}

Deep understandingwordwrapThe working principle and precautions

wordwrapWhen the filter performs line breaks, it mainly relies on the text in thespacesTo identify word boundaries and perform line breaks. This means that if your text is a continuous string without spaces, such as a word without spaces in English, or languages such as Chinese, Japanese, Korean (CJK) where there are no natural spaces between characters, wordwrapThe filter willUnableForce a newline between characters. It will treat the entire continuous string as a whole until it encounters the first space or reaches the specified number of characters, but it will not break in the middle of words.

For example, forThisisalongwordwithoutspacesEven if you setwordwrap:5It will still display as a single line. The same goes for Chinese text,安企内容管理系统Even if it exceeds the character limit, it will not break between '安' and '企' because there is no space as a breakpoint.

Therefore, when processing CJK language content, if it is necessary to force line breaks at any character position, it may be necessary to consider other methods, such as CSS stylesword-break: break-all;oroverflow-wrap: break-word;To implement more flexible word segmentation and line break control. However, for languages separated by spaces (such as English),wordwrapThe filter is a powerful and practical tool for automatically wrapping long text.

By reasonable applicationwordwrapFilter, you can make your AnQiCMS website content display **visual effects on various devices and layouts, providing visitors with a smoother and more comfortable reading experience.


Frequently Asked Questions (FAQ)

1.wordwrapDoes the filter work for Chinese text?

wordwrapThe filter mainly breaks lines based on spaces in the text. Since Chinese, Japanese, Korean, and other CJK languages do not have natural spaces between characters,wordwrapThe filter cannot break in the middle of continuous CJK characters.It treats a long continuous sequence of CJK characters as a whole and only breaks the line when it encounters a space (if any).Therefore, when processing a Chinese long text without spaces,wordwrapThe automatic line break effect may not meet expectations.

2. BesideswordwrapAre there other methods to truncate or format long text in AnQiCMS?

Of course there is. AnQiCMS provides various filters to handle text content:

  • truncatechars:number: Truncate text by character count, using...as a replacement. It will truncate words.
  • truncatewords:numberTruncate text by word count, and replace the exceeded part with....
  • truncatechars_html:numberandtruncatewords_html:number: Similar to the above, but specifically designed to truncate text containing HTML tags while attempting to maintain the HTML structure.
  • linebreaksbr: Replace line breaks (`)转换为 HTML 的
    ` tag. You can choose the appropriate filter according to your specific needs.

3. What should I do if I need to force a line break at any character position (even without spaces)?

wordwrapThe filter cannot force a line break in the middle of consecutive characters without spaces.If you have such a need, especially when dealing with CJK text or very long English words, it is usually combined with CSS styles to solve the problem.For example, in your CSS file, you can add elements that contain long textword-break: break-all;oroverflow-wrap: break-word;Properties. These CSS properties can indicate to the browser to force a break at any character position if necessary to prevent text overflow.