In the daily use of the content management system, we often need to display the stored plain text content in rich HTML form to the users. AnQiCMS (AnQiCMS) provides powerful template rendering capabilities, whererenderThe filter is one of the key tools for dealing with such needs. Many users may already know that it can convert Markdown formatted text to HTML, so in addition to Markdown, thisrenderWhat specific string formats can the filter handle?

By delving into the documentation and template tag descriptions of Anqi CMS, we can understand,renderThe filter is mainly and explicitly designed forRender Markdown formatted string content to HTML outputThis means that when your content is written in Markdown syntax,renderthe filter can parse these syntax tags (such as##indicating a second-level heading,*indicating a list item,**Indicates bold text, and it is accurately converted into the corresponding HTML tags that browsers can recognize (such as<h2>/<li>/<strong>)

This conversion feature is reflected in many core content display scenarios of AnQi CMS. For example, in the article (archiveDetail)、Single page(pageDetail)、Category(categoryDetail) and tags (tagDetail) 'Content' (Content) field processing, the document explicitly states that: ContentThe field will automatically convert content to Markdown to html when the Markdown editor is turned on. Furthermore, even if the Markdown editor is not turned on, you can specify manuallyrenderParameters control this conversion process, i.e.render=trueWill force the Markdown to HTML conversion, whilerender=falseWill prevent this conversion.

Let's look at some specific application scenarios, such as when you are writing a document, the main content may contain rich Markdown syntax:

# 安企CMS的强大功能
这是一个段落,**突出显示**一些文本。
- 列表项1
- 列表项2

`这是一段代码`

When you call the template and want to render it as HTML, you can use it like thisrenderFilter:

{# 假设archiveContent变量中存储了上述Markdown内容 #}
{% archiveDetail archiveContent with name="Content" render=true %}
{{archiveContent|render|safe}}

Or, if your custom field stores a summary in Markdown format:

{# 无序的map对象,假设其中introduction字段包含Markdown #}
<div>
    {% archiveParams params with sorted=false %}
        <div>{{params.introduction.Name}}:{{params.introduction.Value|render|safe}}</div>
    {% endarchiveParams %}
</div>

Here, |renderIt will parse Markdown text and convert it to<p>/<h1>/<ul>/<li>/<strong>/<code>etc. HTML tags. Following|safeThe filter is crucial, it tells the template engine,renderThe HTML content generated by the filter is safe and can be directly output to the browser without additional escaping (to prevent<html>displayed as&lt;html&gt;)

Then, besides Markdown,renderCan the filter also convert other "specific format" strings to HTML? The existing document description does not mention it.renderThe filter supports conversion of specific markup languages such as reStructuredText, Textile, and more. Anqi CMS'srenderThe filter focuses on Markdown, which may be due to its lightweight, easy to learn, and widely popular characteristics, making it the mainstream format for content creation, especially suitable for users from non-technical backgrounds to quickly create structured content.

It is worth noting that there are also some other filters in AnQi CMS that can convert specific types of stringsProcess or packageInto HTML tags, but this is withrenderFilters that convert a markup languageTranslateThe working principle of HTML is different. For example:

  • urlizeThe filter can automatically recognize the URL strings in the text and convert them to clickable links.<a>links.
  • linebreaksorlinebreaksbrThe filter can convert newline characters in text to<p>or<br>tags, thus achieving paragraph and line break effects in HTML.
  • safeThe filter is used to mark a string (usually a string that already contains HTML) as 'safe', indicating to the template engine not to escape it as HTML entities.

These filters have their specific uses, but they are not likerenderA filter that is used to parse and convert a complete markup language system. Therefore, if you want to convert non-Markdown specific format text to HTML, the current Anqi CMSrenderThe filter mainly handles Markdown. For other formats, you may need to consider using HTML directly when entering content, or using other programming means to preprocess before storing content.

In summary, it is about AnQi CMS'srenderThe filter plays an important role in content operations, providing a concise and efficient way to seamlessly convert Markdown formatted text content into HTML structures that can be displayed on the web.


Frequently Asked Questions (FAQ)

  1. renderCan a filter be used to clean or filter a user's HTML input to prevent XSS attacks?

    • renderThe filter is mainly used to convert Markdown text to HTML and does not have the function of cleaning or secure filtering of HTML. If you need to handle HTML content submitted by users that may contain malicious code, you should strictly filter and verify it through server-side or other special security measures before storing or displaying the data, rather than relying onrenderfilter.
  2. If I use a plain text string that does not contain any Markdown syntaxrenderHow will the filter behave?

    • If you use a plain text string (without Markdown tags)renderFilter, it will try to parse Markdown.Since no Markdown syntax was detected, it usually outputs the plain text content as is. However, if you expect it to perform other complex transformations, you may find that it is not as expected.In this case, directly output the text content or combinesafeThe filter may be more suitable for your needs.
  3. renderFilters andsafeWhat are the differences between filters, and when should they be used together?

    • renderThe filter isContent converterIt converts Markdown syntax-marked text to HTML structure.safeThe filter isSafety indicatorIt tells the template engine that the marked string content is safe HTML, which does not need to be escaped as HTML entities and can be output directly as HTML. When you userenderThe filter converts Markdown to HTML, in order to ensure that these generated HTML tags can be normally parsed by the browser rather than displayed as plain text (for example, displayed<h1>标题</h1>instead of&lt;h1&gt;标题&lt;/h1&gt;), usually followed bysafeFilter, that is{{content|render|safe}}.