When managing content in AnQi CMS, we often encounter issues with the display of multi-line text on article detail pages. Traditionally, to ensure that content is displayed correctly segmented, many users may be accustomed to manually adding<p>Label. However, AnQi CMS provides a more intelligent and efficient way to automatically segment your text content, greatly enhancing the convenience and maintainability of content creation.
To implement the automatic paragraph segmentation of multi-line text content on the Anqi CMS article detail page, the core lies in utilizing its built-in Markdown editor and flexible template rendering mechanism.
Core Mechanism: Intelligent Processing of Markdown
The strength of AnQi CMS lies in its integration of the Markdown editor.Markdown is a lightweight markup language that allows you to write documents using a simple plain text format, then convert it into structured HTML.This means that when you are writing content in the Markdown editor, you just need toTwo consecutive newline characters (i.e., one blank line) represent a new paragraph, Markdown is automatically converted to HTML when rendered<p>tags to achieve automatic paragraphing
Enable Markdown editor: Start the journey of automatic paragraphing
First, you need to make sure that the Markdown editor is enabled in the Anq CMS backend.
- Log in to your AnQi CMS backend management interface.
- Navigate to the "Backend Settings" menu under the "Content settings” option.
- In the content settings page, find andEnable Markdown editor.
Once enabled, when you publish or edit an article, the document content area will switch to Markdown editing mode.Here, you just need to follow the Markdown syntax conventions, using blank lines to separate paragraphs, and the system will intelligently process these segmented information when saved.
Template content rendering: ensure correct display
Even though the backend content has already been saved as plain text with paragraph logic through a Markdown editor, the frontend template also needs to render it correctly as HTML. Especially in the template files of AnQi CMS, especially usingarchiveDetailorpageDetailWhen calling a tag to display article or page content, you need to ensure that the content is correctly processed.
For example, in your article detail page template (usually{模型table}/detail.htmlIn the code block, you may see something like this to display the article content:
<div>
{%- archiveDetail articleContent with name="Content" %}
{{articleContent|safe}}
</div>
Or for a single page:
<div>
{% pageDetail pageContent with name="Content" %}{{pageContent|safe}}
</div>
It is important to note that this is the case:{{articleContent|safe}}or{{pageContent|safe}}of|safefilter.safeThe filter's role is to inform the template engine that this part of the content is safe HTML and does not need to be escaped. When the Markdown editor is enabled, the system has already converted the Markdown formatted text into text that contains<p>HTML code of the tag. If not|safefilters, these<p>the tag may be treated as plain text and outputted as is, resulting in the content still not being segmented. Therefore,Must be used.|safeFilterMake sure the HTML code can be correctly parsed and displayed by the browser.
Flexible handling for specific scenarios: skillfully using filters
Not all multiline text content comes from Markdown editors, or you may have some old content that has not been processed with Markdown. The Anqi CMS template system also provides flexible filters to deal with these situations:
Handle automatic segmentation of plain text:
linebreaksFilterIf you have a plain text field (such as a custom multi-line text field, or content entered before the Markdown editor was enabled), in which paragraphs are only separated by simple line breaks (Enter key), you can uselinebreaksA filter to convert it into HTML segments.{# 假设myPlainTextContent是一个包含简单换行符的纯文本变量 #} <div> {{ myPlainTextContent|linebreaks|safe }} </div>linebreaksA filter will convert a single newline character in the text to.<br/>While two consecutive newline characters will be wrapped in.<p>In the tag, it is very suitable for formatting simple plain text into an HTML paragraph. If you only need to replace line breaks with<br/>you can uselinebreaksbrfilter, but for paragraph segmentation,linebreaksis the better choice.Render Markdown content from a custom field:
renderFilterIf your content model defines a 'multi-line text' custom field and you want to use Markdown syntax to automatically create paragraph breaks in that field, you can use it in the templaterenderfilter.For example, if your custom field name is
introductionand you want the Markdown content you write to be automatically paragraphed and rendered:{% archiveDetail introduction with name="introduction" %} <div> {{introduction|render|safe}} </div>here,
|renderThe filter will be responsible forintroductionThe Markdown text in the field is then converted to HTML|safeThe filter ensures that this HTML is displayed correctly.
Summary
By enabling the Markdown editor of AnQi CMS, combined with the template'sContentField usage|safefilter, and applying to plain text fields when necessary|linebreaks|safeor apply to custom Markdown fields|render|safeEasily achieve automatic paragraph splitting of multi-line text content on article detail pages, say goodbye to manual addition<p>Labeling can make content creation more smooth and efficient.
Frequently Asked Questions (FAQ)
I enabled the Markdown editor, but the content on the article detail page is still cramped together without paragraphs, what's the matter?This is usually because your template is missing
|safeFilter. When Markdown content is converted to HTML (including<p>tags) after, if the template does not use|safeThese HTML tags will be escaped to plain text, causing the page to look unsegmented. Please check your template code to ensure that content is output using{{您的内容变量|safe}}in the form of.How to automatically segment some of my multi-line custom fields, what should I do?If the content of the custom field is plain text and segmented by simple line breaks, you can use
|linebreaks|safeThe filter is processed. For example:{{ archive.MyCustomField|linebreaks|safe }}If the custom field content is written in Markdown syntax, it should use|render|safeFilter, such as:{{ archive.MyCustomField|render|safe }}This will convert the Markdown to HTML.If I don't want to use the Markdown editor, can the multiline text still be automatically segmented?Can. If your content is entered through a rich text editor (non-Markdown mode), it usually already contains HTML tags (such as
<p>),no additional processing is required, just make sure the template uses|safeFilter. If your content is plain text (such as copied from an external source, or some legacy content), and is only segmented by simple line breaks, then it is used in the template.|linebreaks|safeThe filter can convert it to include<p>HTML tags to achieve automatic segmented display.