In website content management, we often encounter such needs: the user inputs a pure text content in the background, which contains some natural segments and line breaks, and we hope that these segments and line breaks can be correctly rendered as HTML paragraphs in the website front-end (<p>tags) and line breaks (<br/>Label), rather than being crammed together or ignored by the browser.The Anqi CMS provides a very convenient and powerful template filter to solve this problem, allowing plain text content to be presented elegantly on the web page.

Understand the default behavior of plain text on the web

Firstly, we need to understand the default handling of newline characters in plain text by the browser. In HTML, a single newline character (\nA space is typically treated as empty by the browser and will not automatically generate a new paragraph or force a line break.Only when two consecutive newline characters appear will the browser visually create a new blank line, but this is still not a structured HTML paragraph.This resulted in the neat layout entered by the user in the background becoming chaotic on the front end, affecting the reading experience.

The AnQi CMS solution: Text Processing Filter

The AnQi CMS template engine provides powerful content processing capabilities, among whichlinebreaksandlinebreaksbrThese two filters are the key to solving this problem. They can intelligently convert newline characters in plain text to HTML-compliant tags.

1. UselinebreaksFilters handle paragraph and newline processing.

linebreaksThe filter can intelligently parse plain text content and convert consecutive two newline characters (usually indicating the start of a new paragraph) into HTML.<p>...</p>Paragraph tag. While a single newline character is converted to<br/>Label, to implement a forced line break within a paragraph. This method is very suitable for articles, blog posts, and other content that requires a clear paragraph structure.

Example usage:

Suppose your plain text content is stored in a variable namedarchive.Content, you can call it in the template like this:

{{ archive.Content|linebreaks|safe }}

Key points explanation:

  • |linebreaks: This is the core filter that converts<p>and<br/>from line breaks in plain text.
  • |safeThis filter is crucial. Sincelinebreaksit generates HTML tags, and by default, the template engine escapes all output HTML for security reasons (for example, converting<to&lt;)|safeThe purpose is to inform the template engine that this content is safe HTML and can be output directly without escaping. If it is not added|safe, what you see on the page will be&lt;p&gt;...&lt;/p&gt;Such original label text.

2. UselinebreaksbrFilter performs forced line break processing.

If your requirement is simpler, you just want to convert all line breaks (whether single or consecutive) in the text to<br/>tags without generating separate<p>paragraphs, thenlinebreaksbrThe filter would be a better choice. This method is suitable for those who only want the text to be forced to wrap, but do not want additional paragraph spacing, such as displaying address information, poems, product specifications, etc.

Example usage:

If your content is address information, it is stored incontact.Addressvariables:

{{ contact.Address|linebreaksbr|safe }}

Key points explanation:

  • |linebreaksbr: Convert all newline characters directly to<br/>.
  • |safe: It is also essential to ensure the correct rendering of HTML tags.

When should you choose which filter?

  • Selectlinebreaks:When your content is articles, comments, long descriptions, etc., that require clear paragraph demarcation and spacing. It can better simulate traditional article formatting.
  • Selectlinebreaksbr:When your content is a short list, address, poem, code snippet, or other text where you only want to force line breaks without extra paragraph spacing.It provides a more compact display method.

Actual operation: content entry and template invocation

To achieve the above effect, when entering content in the Anqi CMS backend, you need to enter the text into a plain text field, ensuring that the content is original and contains no HTML tags automatically added by a rich text editor.Then, find the area in your template file where you need to display this plain text content, and apply the above filter.

For example, on the document detail page (such asarchiveDetail.html) you may need to display document content or custom plain text fields:

<div class="article-content">
    {% archiveDetail articleContent with name="Content" %}
    {{ articleContent|linebreaks|safe }}
</div>

<div class="custom-text-info">
    {% archiveDetail customInfo with name="YourCustomTextField" %}
    {{ customInfo|linebreaksbr|safe }}
</div>

Points to note

  • |safeFilter security:Use|safeWhen using a filter, be sure to pay attention to the source of the content. It will indicate to the template engine that the content should be output as unprocessed HTML.Therefore, ensure that the source of the text to be processed is reliable. If the content comes from user input and has not been strictly filtered, it is recommended to perform appropriate security checks to avoid potential cross-site scripting (XSS) attack risks.
  • Rich text editor vs. plain text:If the content field itself is a rich text editor (such as the powerful editor provided by AnQiCMS), it already generates the content with<p>/<div>The HTML structure of the tag. It is usually not necessary to use it anymorelinebreaksorlinebreaksbrFilter, otherwise it may insert additional structures into the existing HTML<p>or<br/>causing the page to display abnormally.
  • Markdown content:The AnQi CMS also supports Markdown editors. If your content is input in Markdown format and you want to render it as HTML, the system will usually handle it automatically, or you may need to userenderfor example, a filter (such as{{ archive.Content|render|safe }})。This is a different scenario and filter for handling pure text line breaks.

Summary

By flexibly using the Anqi CMS providedlinebreaksandlinebreaksbrFilter, you can easily display user input plain text content in a structured HTML format on the website, whether it is a neat paragraph article or simple forced line break text, it can be perfectly presented.This not only improves the readability of the content, but also ensures the neat and professional layout of the website, making your content operation work more proficient.


Frequently Asked Questions (FAQ)

  1. Ask: Why did I uselinebreaksThe filter is there, but there is still no paragraph effect on the page, it's just the text stuck together.Answer: The most common reason is that you forget to add at the end of the filter chain.|safeIf not added.|safeThe template engine will escape all by `linebreaks'.