In the Anqi CMS, we often encounter the need to convert text into colorful web elements.Especially when the content is written in Markdown format, how to ensure that they can be presented correctly and beautifully to the user is a problem that requires a deep understanding.renderFilter is how the filter works in the security CMS Markdown content rendering.

The company is favored for its flexible content model and support for diverse content display.It not only comes with a powerful Markdown editor, allowing content creators to write rich text in a concise and efficient manner, but also provides fine-grained controls to handle these Markdown contents, ensuring the final web output meets expectations.renderThe filter is the key link in this control mechanism.

In short,renderThe core function of the filter is to convert the original Markdown text content into HTML format that can be directly parsed and displayed by the browser.Imagine, you have written an article in Markdown with headings, lists, and code blocks. If it is not rendered, the user will see only a pile of plain text with special symbols.renderThe filter acts like an interpreter, translating Markdown syntax into HTML language that browsers can understand, so that content is displayed on the page in the appropriate style.

Then, why do we need thisrenderFilter?The backend content settings of Anqi CMS usually have an option called 'Enable Markdown Editor'.ContentWhen a field is set, it will default to converting Markdown to HTML automatically. This is very convenient for most standard content.

However, in some specific scenarios, we may need to have more flexible control over the Markdown rendering process. For example:

  • Process the Markdown field of the custom content model: EnglishIn addition to the main content of the article, we may add some additional fields that also need to support Markdown syntax in the custom content model, such as the 'detailed features' of the product or the 'service description' of the page. The content of these custom fields is usually not rendered in Markdown by default, at which point manual use is required.renderFilter to trigger conversion.
  • Refined control of rendering timing:Sometimes we may want to render Markdown content only under specific conditions, or perform some pre-processing on the content before rendering.renderThe filter allows us to explicitly specify when to render in the template logic.
  • Avoid unnecessary rendering or forced rendering:Even if the global Markdown editor is enabled, we may sometimes need to explicitly specify aContentwhether a field should be rendered in Markdown. For example, byarchiveDetailThe label can obtain the article content by using its built-inrenderparameters to control.

renderHow to use the filter

In the template syntax of AnQi CMS,renderThe filter can be applied very intuitively to any variable containing Markdown text. Its basic syntax is: {{ 变量名|render }}.

Give an example, suppose you defined a custom field namedintroductionin the article content model and want it to support Markdown. When you go througharchiveDetailThe label can use this field's value in this way after obtaining itrenderFilter:

{% archiveDetail introduction with name="introduction" %}
{{ introduction|render|safe }}

Here, special attention is needed.|safeThis filter.In Django template engine syntax, in order to prevent cross-site scripting (XSS) attacks, all HTML tags and special characters output through variables will be automatically escaped.renderThe filter converts Markdown to HTML and outputs it directly, HTML tags are also escaped to plain text, causing the style to be lost. Therefore, we must followrenderuse it after the filter|safe明确告诉系统这些内容是安全的HTML,不需要进行转义,从而让浏览器能够正确解析并显示出Markdown渲染后的效果。

English translation: Besides directly usingrenderAs an independent filter, Anqi CMS provides built-in parameters to control rendering behavior whenContentthis type of core content field is accessedrenderfor controlling rendering behavior. For example,archiveDetail/categoryDetail/pageDetailandtagDetailThese tags, getContentfields can be specified directlyrender=trueorrender=false:

{# 强制将当前文档内容渲染为HTML,即使后台Markdown编辑器关闭 #}
<div>文档内容:{% archiveDetail archiveContent with name="Content" render=true %}{{archiveContent|safe}}</div>

{# 不对当前文档内容进行Markdown渲染,即使后台Markdown编辑器开启 #}
<div>原始Markdown内容:{% archiveDetail archiveContent with name="Content" render=false %}{{archiveContent|safe}}</div>

Here are therender=truemeans to force Markdown rendering,render=falseThis parameter indicates that rendering is not performed. The priority of this parameter is higher than the global setting of "Whether to enable Markdown editor" in the background, providing you with more detailed control.

Reasonably utilizedrenderThe filter and its related parameters allow you to easily navigate Markdown content in the Safe CMS, whether it's standard articles, custom fields, or conditional rendering based on specific requirements. This ensures that your website content is presented to visitors in the most elegant and accurate way, providing an excellent reading experience.


Common Questions (FAQ)

  1. When should it be usedrenderFilter, rather than relying on the background Markdown editor settings?In most cases, if your content is published in articles, products, and other standard content models,ContentField in, and you want it to be automatically rendered, just enable the Markdown editor setting in the background.renderThe filter is mainly used in the following scenarios:

    • You have added additional fields that need to support Markdown in the custom content model.
    • You need to obtain the original Markdown text and then decide whether to render it in the template according to specific logic.
    • You want to force render the Markdown of a specific content when the global Markdown editor settings are turned off.
    • You need to preprocess or post-process the Markdown content on the frontend before rendering it.
  2. Why do you need to addrenderAfter the filter, is the content still not displayed correctly as HTML?The most common reason is that you forget torenderafter the filter|safeThe Filter. For security, the default behavior of the Safe CMS is to escape HTML content output by variables.|safeThe filter tells the system that you trust this HTML content is safe and does not need to be escaped, so the browser can parse and display it correctly. Make sure your code looks like this.{{ 变量|render|safe }}.

  3. I can use it in the custom content model fieldrenderFilter?Absolutely. This isrenderFilter is one of the most common application scenarios. When you create a text field in a custom content model and want users to input Markdown in this field, and then render these Markdown as HTML on the front end, you just need to get the variable of this custom field and then apply{{ 变量名|render|safe }}It can be realized.