In the daily use of the content management system, we often need to present the stored plain text content to users in rich HTML form. AnQiCMS (AnQiCMS) provides powerful template rendering capabilities,renderThe filter is one of the key tools for handling such requirements. Many users may already know that it can convert Markdown-formatted text to HTML, so besides Markdown, thisrenderThe filter can handle which specific formats of strings?
By deeply exploring the documentation and template tags of Anqi CMS, we can understand,renderThe filter is mainly and explicitly designed forRenders 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,**English representation of bold text, and convert it accurately into the corresponding HTML tags that browsers can recognize (such as<h2>/<li>/<strong>).
This conversion feature is reflected in multiple core content display scenarios of the Anqi CMS. For example, in articles (archiveDetail)、Single Page(pageDetail)Categories(categoryDetail) as well as tags (tagDetail) content (Content)Field processing, the document clearly states: ContentThe field will automatically convert the content to html when the Markdown editor is enabled. Furthermore, even if the Markdown editor is not enabled, you can manually specifyrenderParameters to control this conversion process, that isrender=truewill enforce the Markdown to HTML conversion, whilerender=falsewill prevent this conversion.
Let's look at some specific application scenarios, for example, when you are writing a document, its main content may contain rich Markdown syntax:
# 安企CMS的强大功能
这是一个段落,**突出显示**一些文本。
- 列表项1
- 列表项2
`这是一段代码`
When you call it in a 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 the brief introduction is stored in your custom field 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,|renderThe Markdown text will be parsed and converted to<p>/<h1>/<ul>/<li>/<strong>/<code>the following HTML tags. The filter that comes next is essential, it tells the template engine,|safeto be translated into English.renderThe HTML content generated by the filter is safe and can be directly output to the browser without the need for further escaping (to prevent<html>displayed as<html>).
Then, in addition to Markdown,renderCan the filter also convert other 'specific format' strings to HTML? According to the existing document description, it does not mentionrenderThe filter supports conversion of specific markup languages such as reStructuredText, Textile, and so on. The Anqi CMS'srenderThe filter focuses on Markdown, which is likely due to Markdown's features of being lightweight, easy to learn and use, and widely popular, making it the mainstream format for content creation. It is particularly suitable for users from non-technical backgrounds to quickly create structured content.
It is worth noting that there are other filters in the Anqi CMS that can convert specific types of strings intoprocess or wrapinto HTML tags, but this is different fromrendera markup languageto EnglishThe working principle of HTML is different. For example:
urlizeFilters can automatically identify and convert URL strings in text to clickable links.<a>Link.linebreaksorlinebreaksbrThe filter can convert newline characters in text into<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 the HTML entities.
These filters all have specific uses, but they are not likerenderFilter such, used to parse and convert a complete system of markup languages. Therefore, if you want to convert non-Markdown specific format text to HTML, the current Anqi CMS'srenderFilter primarily handles Markdown.For other formats, you may need to consider using HTML directly when entering content, or using other programming means to preprocess the content before storage.
In summary, the security CMS ofrenderFilter plays an important role in content operation, providing a concise and efficient way to seamlessly convert Markdown formatted text content into HTML structure that can be displayed on web pages.
Common Questions and Answers (FAQ)
renderFilter can be used to clean or filter the HTML content entered by the user to prevent XSS attacks?renderThe filter is mainly used to convert Markdown text to HTML, it does not have the function of HTML cleaning or safe filtering. If you need to handle HTML content submitted by users that may contain malicious code, you should strictly filter and verify through server-side or other special security measures before storing or displaying the data, rather than relying onrenderFilter.
If I use a plain text string without any Markdown syntax
renderWhat will the filter do?- If you use a plain text string (without Markdown tags)
renderFilter, it will try to perform Markdown parsing.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 it not as expected.safeThe filter may be more suitable for your needs.
- If you use a plain text string (without Markdown tags)
renderfilters andsafeWhat are the differences between the filters, and when should they be used together?renderThe filter isContent converterIt converts the text marked by Markdown syntax to HTML structure.safeThe filter isSafety indicatorIt tells the template engine that the marked string content is safe HTML that 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 as plain text)<h1>标题</h1>Instead<h1>标题</h1>),usually need to be used immediately aftersafea filter, that is{{content|render|safe}}.