What is the behavior of the `linebreaks` filter in the Anqi CMS template when processing hyperlink text?

Calendar 👁️ 68

In Anqi CMS template development, formatting text content is a common requirement, andlinebreaksThe filter is one of the convenient tools. It is mainly responsible for converting line breaks in plain text content into HTML paragraphs and line breaks.\n<p>)and line breaks.<br/>The tag makes the layout more suitable for web reading. However, when it comes to hyperlink text, there are some notable details to pay attention to.

linebreaksBasic function of the filter

First, let's reviewlinebreaksThe core function. According to the AnqiCMS documentation,linebreaksThe filter can identify single-line breaks in text (\n) and convert them to<br/>tags, while also handling double-line breaks (\n\nThis indicates that a blank line is recognized as a paragraph separator, and it is enclosed with<p>and</p>tags. For example, if there is a piece of text:

这是一段文本。
这是新的一行。

这是新的一个段落。

after{{ 变量 | linebreaks }}After processing, the output may be similar to:

<p>这是一段文本。<br />这是新的一行。</p><p>这是新的一个段落。</p>

It aims to automatically convert the plain text content entered by the user into a format with basic HTML structure, enhancing readability.

Process hyperlinks in ordinary text.

If your text only contains pure URL strings, for examplehttps://en.anqicms.comorwww.anqicms.com, thenlinebreaksThe filter will not magically convert these URLs into clickable hyperlinks (i.e., HTML's<a>Label). It will only handle the text in the same way as normal text, segmenting and formatting the URL string as ordinary content, but will not give it the interactivity of a link.

In order to achieve this automatic conversion, that is, to make plain text URLs clickable, you need to use the special template provided in the Anqi CMS.urlizeorurlizetruncFilters. These filters can intelligently identify URLs and email addresses in text and wrap them in<a>tags, and can also automatically addrel="nofollow"Property, this is very helpful for SEO friendliness.

Process text that already contains HTML hyperlinks.

It is especially important to note that when your text content already contains HTML formatted hyperlinks (such as<a href='https://en.anqicms.com'>安企CMS官网</a>),linebreaksthe handling method will become different.

By default, the template engine of AnQi CMS performs security escaping on the output content to prevent potential XSS attacks. This means that if you directly use{{ 变量 | linebreaks }}The original HTML hyperlink tag will be escaped as entity characters. For example,<a href="...">may be escaped as&lt;a href=&quot;...&quot;&gt;This way, the browser will not render it as a clickable link, but display it as the original HTML code text.

In order to avoid this situation and applylinebreaksAfter the filter still retains the interactivity of HTML hyperlinks, you need to use an additional one|safefilter.|safeTell the template engine that you believe this content is safe and does not require escaping. Therefore, the correct usage should be{{ 变量 | linebreaks | safe }}.

For example, assume the value ofarchive.Descriptionis:

欢迎访问 <a href="https://en.anqicms.com">安企CMS官网</a>,
这是一个功能强大的内容管理系统。
  1. without|safe: {{ archive.Description | linebreaks }}output:

    <p>欢迎访问 &lt;a href=&quot;https://en.anqicms.com&quot;&gt;安企CMS官网&lt;/a&gt;,<br />这是一个功能强大的内容管理系统。</p>
    

    Hyperlink is escaped and cannot be clicked.

  2. Add|safe: {{ archive.Description | linebreaks | safe }}output:

    <p>欢迎访问 <a href="https://en.anqicms.com">安企CMS官网</a>,<br />这是一个功能强大的内容管理系统。</p>
    

    Hyperlink is retained and can be clicked.

In summary,linebreaksThe filter is a pure text formatting tool that does not automatically create hyperlinks. If your content already contains HTML tags for hyperlinks, be sure to keep them separate to maintain their functionality while formatting.|safeFilter in conjunction with using.


Frequently Asked Questions (FAQ)

  1. If my text only contains pure URL strings, such as.www.anqicms.com,linebreaksCan the filter make it into a clickable link?No.linebreaksThe filter is responsible for converting line breaks in text to HTML paragraph and line break tags, and it does not automatically identify plain text URLs and convert them to<a>The function of the tag. If you need to automatically convert plain text URLs into clickable hyperlinks, you should useurlizeorurlizetruncfilter.

  2. Why did I uselinebreaksAfter the filter, the original HTML hyperlinks in the article content have become code text and cannot be clicked?This is because the template engine of Anqi CMS defaults to escaping output content to prevent potential XSS attacks. When you uselinebreaksWhen processing text that contains HTML hyperlinks, HTML tags (such as<a>) will be escaped as entity characters. To solve this problem, you need to uselinebreaksfilter chaining after|safeFilter, for example{{ 变量 | linebreaks | safe }}This will inform the template engine that the content is safe and does not require escaping.

  3. Can I use both at the same time?linebreaksandurlize(orurlizetruncAre the filters in a certain order??Yes, it can be combined. Generally speaking, if your goal is to first convert plain text URLs into hyperlinks, and then format the entire text (including the newly generated links) in paragraphs and line breaks, thenurlizeshould precedelinebreaksfor example{{ 变量 | urlize | linebreaks | safe }}. This ensures that the URL is recognized and converted to an HTML link first, thenlinebreaksand then it is formatted as a whole text, and finally use|safeEnsure HTML links are not escaped.

Related articles

How to adapt the `linenumbers` filter for different screen sizes on mobile pages?

In Anqi CMS, the `linenumbers` filter is a very practical feature that can automatically add line numbers to multi-line text content, which is particularly convenient for displaying code snippets, step-by-step instructions, and other scenarios.However, when this content with line numbers is displayed on the mobile page, how to ensure its good readability and layout adaptation is a problem we need to pay attention to in content operation.### Understand the working way of `linenumbers` filter Firstly, we need to clarify how the `linenumbers` filter generates line numbers

2025-11-08

How to prevent the `linebreaks` filter from wrapping unexpected content (such as phrases) inside `<p>` tags?

In Anqi CMS template design, we often use various filters to conveniently process and display content.Among other things, the `linebreaks` filter is a very practical tool that can intelligently convert line breaks in plain text to HTML paragraph `<p>` tags and line breaks `<br/>` tags, thus presenting more readable formatting on the web.However, some users may encounter such situations during use: Even short phrases or content that should not be used as independent paragraphs may be accidentally wrapped in `<p>`

2025-11-08

In AnQi CMS multilingual site, is the `linebreaks` filter consistent in handling line breaks for different languages?

When setting up a multilingual site on AnQi CMS, content managers often pay attention to a detail: does the `linebreaks` filter perform consistently when handling line breaks in text of different languages?This issue touches on the deep logic of template engine processing mechanism and multilingual content display.A deep understanding of the AnQiCMS template tag and filter functions allows us to clearly state that the `linebreaks` filter maintains a consistent underlying mechanism when handling line breaks in text of different languages.###

2025-11-08

Does the `linebreaks` filter affect Markdown formatted inline code (code)?

In AnQiCMS template creation, flexibly using various filters is the key to improving the display effect of content.Among them, the `linebreaks` filter is a frequently mentioned feature, intended to handle line breaks in plain text.However, when it comes to inline code in Markdown format (`code`), the mechanism of this filter is not very intuitive and may even raise some doubts.

2025-11-08

How can you use the `linebreaks` filter to beautify the display of multi-line descriptions on the list page of a custom module?

In the custom module list page of the website, the display effect of the introduction directly affects the user's reading experience and the overall beauty of the page.We often encounter such a situation: when we carefully enter multiple lines of introduction in the background, it includes paragraph breaks and line breaks, but when displayed on the front-end page, these formatting information disappears, and all the text is compressed into a mass, making it difficult to read.This is usually because HTML defaults to ignoring extra whitespace and newline characters in text.

2025-11-08

How to use the `linebreaks` filter correctly within the `{% filter %}{% endfilter %}` block?

In the template development of Anqi CMS, we often need to display the plain text content entered in the background, such as article summaries, multi-line descriptions in product details, or user comments, in a way that is more in line with web reading habits.Directly output this plain text, you will find that the original line breaks are lost, and all the content is squeezed together, which seriously affects readability.At this time, the `linebreaks` filter has become a powerful helper in solving this problem, as it cleverly converts newline characters in plain text to standard HTML paragraph and newline tags.

2025-11-08

Why does my `linenumbers` filter only show '1. ' without the subsequent line numbers?

Users of AnQiCMS may occasionally encounter some seemingly strange phenomena when using template filters, such as when trying to use the `linenumbers` filter to add line numbers to article content, but it only displays '1. ' and the subsequent line numbers do not appear.This is not a problem with the filter itself, but is closely related to the HTML rendering mechanism and the way content is processed.### Understanding the role of the `linenumbers` filter Firstly, let's briefly review one

2025-11-08

`linebreaksbr` filter generates empty `<br/>` tags when processing blank lines?

In AnQiCMS template development, handling newline characters in text is a common requirement.The `linebreaksbr` filter was created for this purpose, it aims to convert newline characters in the original text to the `<br/>` tags in HTML, thereby preserving the original format of the text on the web.However, when encountering blank lines in the text, how will the `linebreaksbr` filter behave, whether it will generate an 'empty' `<br/>` tag, which is indeed a concern for many users.To understand the behavior of `linebreaksbr`

2025-11-08