In website content display, how to present the pure text content entered by users, especially the text containing line breaks, in a way that conforms to web semantics and visual effects, is a frequently encountered problem in template development. AnqiCMS provideslinebreaksandlinebreaksbrThese practical filters allow us to flexibly handle newline characters in text.What is more important, through the conditional judgment in the template, we can also dynamically select and use them according to different situations.

UnderstandinglinebreakswithlinebreaksbrFilter

First, let's briefly review the core functions and differences between these two filters:

  1. linebreaksFilterThis filter will convert double line breaks (i.e., two consecutive carriage returns) in text to<p>and</p>Labelled paragraphs, while single line breaks are converted to<br/>Label.It is suitable for scenarios where user input text needs to be formatted into standard HTML paragraphs, such as article content, long descriptions, etc.This makes the text visually structured, with each paragraph able to display independently.

    Example input text:

    这是一段文字。
    
    
    这是另一段文字。
    这里有一个单独的换行。
    

    Use{{ 文本变量|linebreaks|safe }}The output effect:

    <p>这是一段文字。</p>
    <p>这是另一段文字。<br />这里有一个单独的换行。</p>
    
  2. linebreaksbrFilterAndlinebreaksbrIt is more direct, it will replace all line breaks in the text with<br/>tags without generating any<p>Tags. This applies to those who do not need a strict paragraph structure, but simply need to retain all the line breaks of the user's input, such as short introductions, address information, or list items.

    Example input text:

    这是一段文字。
    
    
    这是另一段文字。
    这里有一个单独的换行。
    

    Use{{ 文本变量|linebreaksbr|safe }}The output effect:

    这是一段文字。<br /><br />这是另一段文字。<br />这里有一个单独的换行。<br />
    

An important reminder:It should be noted in particular that both of these filters will generate HTML tags in the output. To ensure that these tags can be parsed correctly by the browser rather than displayed as plain text (for example, seeing 需要特别注意的是,这两个过滤器都会在输出中生成 HTML 标签。为了确保这些标签能够被浏览器正确解析,而不是作为纯文本显示(例如看到<p>or<br/>Such characters), we must use them in a chained manner after these filters|safeFilter. This is the automatic escaping feature enabled by default in the AnqiCMS template engine to prevent XSS attacks, used|safeTell the template engine that this content is safe and does not require escaping.

Dynamic selection strategy: apply flexibly based on different conditions.

AnqiCMS powerful template engine allows us to combine conditional judgment tags{% if %}to implement dynamic selectionlinebreaksorlinebreaksbrstrategy. The following are two common scenarios of dynamic selection:

Scenario one: Control based on custom content fields

In some cases, we may want the content editor to be able to decide the display format of the text at the time of publication.This can be achieved by adding a custom field to the content model in the AnqiCMS backend.

Suppose we added a nameddisplay_modecustom field to the article (archive) model, which provides two options:paragraphParagraph mode andsimple_breaksSimple line break). The editor can choose the appropriate mode when filling in the article content.

In the template, we can write logic like this to dynamically apply filters:

{# 假设archive是当前文章对象,其包含一个名为display_mode的自定义字段 #}
{% if archive.display_mode == 'paragraph' %}
    {# 当编辑者选择段落模式时,使用linebreaks过滤器 #}
    <div class="content-paragraph">
        {{ archive.Content|linebreaks|safe }}
    </div>
{% elif archive.display_mode == 'simple_breaks' %}
    {# 当编辑者选择简单换行模式时,使用linebreaksbr过滤器 #}
    <div class="content-simple-breaks">
        {{ archive.Content|linebreaksbr|safe }}
    </div>
{% else %}
    {# 如果未设置或设置了其他值,提供一个默认处理,例如统一使用linebreaksbr #}
    <div class="content-default">
        {{ archive.Content|linebreaksbr|safe }}
    </div>
{% endif %}

This method provides the greatest flexibility, allowing content editors to directly control the rendering of text without modifying the template code.

Scenario two: automatically judge based on content length.

Sometimes, we may want the system to automatically select the most suitable format based on the actual length of the text content.For short descriptive text, we may tend to use simple line breaks; while for longer text, it is more suitable to display in sections.

We can make use of the templates provided by AnqiCMS|lengthThe filter to get the length of the text content and make conditional judgments accordingly:

{# 假设archive.Description是文章的简介文本 #}
{% if archive.Description|length > 150 %}
    {# 如果描述文本超过150个字符,我们认为它是长文本,使用linebreaks分段 #}
    <div class="description-long">
        {{ archive.Description|linebreaks|safe }}
    </div>
{% else %}
    {# 如果描述文本较短,使用linebreaksbr保留所有换行,保持简洁 #}
    <div class="description-short">
        {{ archive.Description|linebreaksbr|safe }}
    </div>
{% endif %}

This example demonstrates how to dynamically adjust text formatting through programmatic logic without relying on manual selection by the content editor. You can adjust it according to your actual needs.150This threshold.

Practical tips and suggestions

  • Always use|safe:This is a reiteration of the key point, ensure that your HTML tags are rendered correctly.
  • Combined with actual business: Dynamic selection strategies should be closely integrated with the actual content display needs of your website. For example, in the "Product Features" section of the product details page, it may be more suitablelinebreaksbr; while in the "Detailed Introduction" section,linebreaksIt is more appropriate.
  • Maintain consistency:Once the logic of dynamic selection is determined, try to maintain consistency throughout the similar content of the entire website to provide a good user experience.
  • Thorough testing:Before deploying any dynamic processing logic, be sure to thoroughly test on different types of text content to ensure the display effect meets expectations.

By flexible applicationlinebreaksandlinebreaksbrThe filter, combined with the powerful conditional judgment capabilities of AnqiCMS templates, allows you to create more expressive and user-friendly website content.


Frequently Asked Questions (FAQ)

Q1: Why did I uselinebreaksorlinebreaksbrBut what is displayed on the page is<p>and<br/>The text of the tag, not the actual paragraph or line break?

A1:This is usually because you forget to add at the end of the filter chain|safeFilter. The AnqiCMS template engine, for security reasons, defaults to automatically escaping all output HTML tags. When you uselinebreaksorlinebreaksbrgenerate<p>or<br/>tags, if missing|safeThese tags themselves will be escaped&lt;p&gt;and&lt;br/&gt;Therefore, they will be displayed as plain text. Make sure your code is similar to{{ 文本变量|linebreaks|safe }}.

Q2: Are there other methods to dynamically select these two filters besides custom fields?

A2:Of course there is. In addition to the conditions mentioned in the article, such as judging according to custom fields and content length, you can also consider other conditions:

  • Based on a specific template file path:If some template files (such as `