In website content management, we often encounter such situations: diligently typing multiple lines of text in the background editor, pressing the Enter key between each line, hoping they will maintain the same line break effect on the front page.However, after the content was published, it was found that all the line breaks had disappeared, and the text was compressed into a blob.This is because the web browser defaults to treating consecutive newline characters as a single space and does not automatically convert them to visual line breaks.

For friends who use AnQiCMS, solving this problem is actually very simple and elegant, especially when you make use of its powerful Markdown editor feature.Auto CMS fully considers the needs of content creators and provides various mechanisms to ensure that the layout of the content can be displayed accurately and correctly to users.

Core solution: AnQi CMS's Markdown editor

English CMS as a modern content management system, built-in good support for Markdown syntax.Markdown is a lightweight markup language that allows you to write documents using a plain text format, then the system converts it into structured HTML.<p>tags (paragraphs) or<br/>Tags (force line break), thus perfectly retaining your formatting intention.

To enable this feature, you just need to find the 'Content Settings' option in the 'Global Settings' of the Anqi CMS backend and make sure the Markdown editor is enabled. Once enabled, when you write content in modules such as 'Add Document', 'Page Management', or 'Document Categories' and use Markdown syntax, for example in the document'sContentField input content, when the system renders these contents on the front end, it will automatically perform the conversion from Markdown to HTML, and naturally handle the line break issues.

Example: How to use Markdown in document content and implement line breaks

Suppose you have entered the following content in the Markdown editor of the document content:

这是第一段文字。

这是第二段文字。

---

这是第三段文字,
这里有一个手动换行。

When the Anqi CMS renders this content on the front end, it automatically converts it to a similar HTML structure:

<p>这是第一段文字。</p>
<p>这是第二段文字。</p>
<hr>
<p>这是第三段文字,<br/>这里有一个手动换行。</p>

So, your content will be displayed correctly on the web page with paragraph and line break effects.

More flexible control:renderThe clever use of parameters

The template tags of AnQi CMS provide extremely high flexibility. Even if you have not enabled the Markdown editor globally, or in certain specific scenarios where you need to force a field to be rendered as Markdown,archiveDetail/pageDetailandtagDetailof the tagsContentAll fields support a namedrenderThe parameter.

ThisrenderParameter acceptancetrueorfalsetwo values:

  • render=true:Force the currentContentThe content of the field is converted from Markdown to HTML, even if the Markdown editor is not globally enabled on the backend.
  • render=false: Prevent access to the currentContentThe content of the field is converted from Markdown to HTML, even if the Markdown editor is globally enabled on the backend.

This provides great convenience for content operators, allowing for fine-grained rendering control over individual content fields according to actual needs. Please note that when content is rendered in Markdown, the output is HTML code. To ensure that the browser correctly parses it and does not display it as plain text, you need to use it in conjunction with templates.|safeFilter.

Example: UserenderParameter control content rendering

{# 假设archive是当前文档对象 #}

{# 强制渲染Markdown内容,并确保HTML安全输出 #}
<div>
    <h3>文档内容 (强制Markdown渲染):</h3>
    {% archiveDetail articleContent with name="Content" render=true %}
    {{ articleContent|safe }}
</div>

{# 如果不想渲染Markdown,即使编辑器开启 #}
<div>
    <h3>文档内容 (不渲染Markdown):</h3>
    {% archiveDetail pureTextContent with name="Content" render=false %}
    {{ pureTextContent }} {# 这里通常不需要|safe,因为它被当作纯文本处理 #}
</div>

Fallback solution for plain text content:linebreaksandlinebreaksbrFilter

Markdown rendering is the most recommended way to handle content line breaks.However, there are always some special cases where we may need to perform simple line breaks on plain text content, or some fields are not stored in Markdown format, but are simply multiline text.linebreaksbrandlinebreaksThey are equivalent tonl2brfunction, which can convert line breaks in text to HTML.<br/>Label.

  • linebreaksbrFilter:This is a relatively direct conversion method. It will simply replace each newline character in the text with\n)}<br/>Label. Whether you have single-line or multiple-line breaks in your text, it will generate them one by one.<br/>.

  • linebreaksFilter:Compared tolinebreaksbr,linebreaksthe filter is more intelligent. It will convert single-line breaks in the text into<br/>And two consecutive line breaks (i.e., blank lines) will be converted into a pair<p>Labelled paragraphs. This method can generate more semantically appropriate HTML when dealing with plain text that has paragraph structure.

Similarly, since these two filters output HTML tags, you need to cooperate|safefilters to ensure that HTML is correctly parsed by the browser.

Example: UselinebreaksbrandlinebreaksFilter

Suppose some content fielddescriptionContains the following plain text:

这是一行描述。
这是第二行描述。

这是一个新的段落。

UselinebreaksbrFilter:

<div>
    <h3>描述 (linebreaksbr 转换):</h3>
    {{ description|linebreaksbr|safe }}
</div>

The output HTML will be:

<div>
    <h3>描述 (linebreaksbr 转换):</h3>
    这是一行描述。<br/>这是第二行描述。<br/><br/>这是一个新的段落。<br/>
</div>

UselinebreaksFilter:

<div>
    <h3>描述 (linebreaks 转换):</h3>
    {{ description|linebreaks|safe }}
</div>

The output HTML will be:

<div>
    <h3>描述 (linebreaks 转换):</h3>
    <p>这是一行描述。<br/>这是第二行描述。</p><p>这是一个新的段落。</p>
</div>

Summary and Suggestions

In AnQi CMS, handling Markdown content line breaks into HTML conversion, the most recommended way is to fully utilize the built-in Markdown editor.It automatically and intelligently handles most of the formatting requirements, greatly improving content creation efficiency.renderThe parameter provides the ability to enable or disable Markdown rendering on demand. For content that is indeed plain text and does not involve Markdown syntax, linebreaksandlinebreaksbrFilter is a simple and effective solution.

Master these methods, and you can ensure that every piece of content you publish on the security CMS is presented to your website visitors with the layout that best suits your taste.


Common Questions and Answers (FAQ)

Q1: I have enabled the Markdown editor and the content has also used Markdown syntax, but the line breaks are still not working, why is that?

A1: The most common reason is that you forget to use|safeFilter.The rendered content of Markdown is HTML code, browsers will default to escaping it and displaying it as plain text instead of parsing it as HTML.{{ archiveContent|safe }}when you output content in the template|safe.

Q2:linebreaksandlinebreaksbrWhat is the difference between filters, and which one should I choose?

A2: The main difference lies in the handling of 'empty lines':

  • linebreaksbr: Replace all single-line breaks (\n) with<br/>. For empty lines (consecutive two\n), it generates two<br/>.
  • linebreaks:Replace single line breaks with<br/>,but will convert two consecutive line breaks (i.e., one blank line) into a pair<p>tags, which is more in line with semantic paragraph structure.

If you want simple line breaks between lines without distinguishing paragraphs, you can chooselinebreaksbr. If you want empty lines to create new HTML paragraphs for clearer structure, thenlinebreaksthis would be a better choice.

Q3: Can I write HTML tags manually in Markdown content?<br/>or<p>Can I use HTML tags?

A3: Yes, Markdown syntax usually supports embedding native HTML tags. This means you can directly write HTML tags in Markdown content.<br/>Make a forced line break, or use<p>Labels can be used to define paragraphs. The Aiqi CMS Markdown renderer will also recognize and preserve these native HTML tags. This is useful when local content needs to be more precisely