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.