In the daily operation of websites, we all know the importance of user experience.An interface that is clear and tidy can significantly enhance visitor satisfaction.Especially in the comments section, if a user posts long content without proper line breaks, it may not only disrupt the page layout but also deter other visitors, affecting the reading experience.

Imagine that when you are browsing a fascinating article and are ready to see everyone's discussion, you find that the comment section is filled with lines of "endless" long text, which is undoubtedly tiring and may even lead you to skip it directly.尤其是English设备上,过长的评论行更容易导致页面横向滚动,极大地损害了响应式设计的美观和实用性。

幸运的是,AnQiCMS 提供了一个非常实用的模板过滤器——EnglishwordwrapIt can help us elegantly solve the problem of long text display in comment sections, making your website's comment section look fresh.

use cleverlywordwrapFilter, to automatically wrap text

wordwrapThe core function of the filter is to automatically wrap a long text according to the specified character length.Here, regardless of the length of the comment content, it will be split into shorter lines for easier reading, avoiding the visual burden and layout issues caused by excessively long lines.

In AnQiCMS template syntax, it useswordwrapThe filter is very intuitive, its basic usage is:{{ 变量|wordwrap:长度 }}

For example, if you want the comment content to wrap automatically when it reaches 50 characters, you can write it like this:{{ item.Content|wordwrap:50 }}

Here,item.Contentrepresented the actual content of the comment. However, since the comment content may contain some HTML tags (such as bold, italic input by the user), it is directly appliedwordwrapAfter, if you want these HTML tags to be parsed normally rather than displayed as plain text, we also need to cooperate withlinebreaksbrThe filter willwordwrapthe newline characters inserted converted to HTML's<br/>tags and usesafeThe filter informs the system that this content is safe and can be rendered as HTML. Therefore, the more complete usage is:{{ item.Content|wordwrap:50|linebreaksbr|safe }}

Apply in comment templatewordwrap

Where should we specifically modify?

The comment list template file of AnQiCMS is usually located in your template directory.comment/list.html文件中。You can find and edit this file through the "Template Design" feature of the AnQiCMS backend.

Incomment/list.html文件中,You need to find the tag responsible for displaying the comment content. According to the document description of the AnQiCMS comment list tag, the comment content is usually by{{item.Content}}or similar variables to display.

Find a code snippet similar to the following:

{# 示例:评论内容显示 #}
<div>
  <p>{{item.Content}}</p>
</div>

Modify it to applywordwrapthe filtered form:

{# 示例:应用 wordwrap 过滤器后的评论内容显示 #}
<div>
  <p>{{ item.Content|wordwrap:50|linebreaksbr|safe }}</p>
</div>

After completing the modification and saving, refresh your website page and you will find that the long text comments that were once 'endless' are now in perfect order, greatly enhancing the visual comfort.

Considerations when applying

  1. Selecting an appropriate line length: wordwrap:50of50It is an example value.You can adjust this number based on your website design, font size, and the reading habits of your target audience.Generally speaking, the desktop version can be a bit longer, while the mobile version is recommended to be shorter.**The practice is to test several lengths and view the effects on different devices.

  2. Understanding Chinese text:What needs to be paid special attention to is,wordwrapThe filter mainly uses spaces to identify "words" and to perform line breaks. This means that if your comment content is continuous Chinese text without spaces, even if the text is very long, wordwrapIt will not break the line either.It will maintain the integrity of Chinese sentences, only breaking lines at the boundaries of English words or numbers.This is particularly important in designing Chinese websites, we need to understand and make choices.word-break: break-all;oroverflow-wrap: break-word;to achieve.

  3. CombinesafeFilter:As mentioned earlier, if your comment content allows users to input HTML tags, be sure towordwrapafter addinglinebreaksbr|safeFilter, to ensure that HTML content can be correctly parsed and displayed by the browser, and to avoid potential XSS risks (becausesafeFilter is based on your trusted content source.

PasswordwrapFilter, we can easily improve the user interface of AnQiCMS website comment section, making your content more readable and providing visitors with a more pleasant browsing experience.Now, go try your AnQiCMS backend!


Common Questions and Answers (FAQ)

Q1:wordwrapDoes the filter also format the content within HTML tags? For example<p>这是一个很长的文本</p>text.A1:wordwrapThe filter acts on the original string content of the variable. When you apply it toitem.Contentifitem.Contentit contains HTML tags,wordwrapIt will try to process the entire string according to its rules (newline on space) including the tag itself.This may cause the HTML structure to be incorrectly broken.{{ item.Content|wordwrap:长度|linebreaksbr|safe }}.wordwrapit will insert line breaks in the text,linebreaksbrand convert them to<br/>whilesafeEnsure that the browser parses the entire result as HTML.

Q2: Why did my applicationwordwrapAfter applying the filter, some long Chinese comments still do not wrap?A2:wordwrapThe filter primarily relies on identifying spaces to determine the line breaks. If your Chinese comment content is continuous, with no English words or numbers, and no spaces, thenwordwrapThe filter will not force line breaks in the middle of Chinese text.This is because Chinese is not like English which has clear word boundaries (spaces).word-break: break-all;oroverflow-wrap: break-word;