How to call and safely display the `Content` field that contains HTML on the AnQiCMS document detail page?

Calendar 👁️ 71

On AnQiCMS-built websites, the core of the document detail page is often the main content of the article, that isContentThis field carries a variety of information, from simple text to complex text layout, multimedia embedding, and even custom code segments.Therefore, how to correctly and safely display the content containing HTML format in the template is a key skill that every AnQiCMS user needs to master.

AnQiCMS designs templates for rendering, fully considering the security of the content.By default, to prevent potential cross-site scripting (XSS) attacks, the system automatically escapes any field that may contain HTML tags extracted from the database.This means, if you use directly in the template{{archive.Content}}to display content, including all HTML tags (such as<p>/<img>/<a>An HTML entity (such as&lt;p&gt;/&lt;img&gt;/&lt;a&gt;) will be converted to the corresponding HTML entity, and the final display on the page will be the code text with angle brackets, rather than the rendered graphic effect of the browser.

To makeContentThe HTML content within the field can be normally parsed and rendered by the browser. We need to explicitly tell AnQiCMS that this content has been carefully edited and confirmed to be safe and does not require escaping. At this point, it is necessary to use the AnQiCMS template engine providedsafefilter.

For example, in the template file of the document detail page (usuallyarchive/detail.htmlor a custom document template), you can call and display it like thisContentField:

<div class="article-content">
    {{ archive.Content|safe }}
</div>

here,archiveIt is usually a context variable on the document detail page, representing the document object being viewed.ContentIt is an attribute of the document object, storing the main content with HTML formatting.|safeis applied toarchive.ContentThe filter indicates that the template engine should treat this content as 'safe', thus avoiding the default HTML escaping and directly outputting the original HTML code so that the browser can correctly parse and render the expected text and graphic effects.

It is noteworthy that AnQiCMS also supports Markdown editors. If you have enabled the Markdown editor in the background to write documents, thenContentThe field may contain text formatted in Markdown.In this case, the AnQiCMS template engine will automatically convert it to HTML.Document Details LabelarchiveDetailofContentThe field usage also mentions that it can be achieved throughrenderParameter manually control the conversion from Markdown to HTML:

{# 假设 Content 字段包含 Markdown 内容,并希望手动控制渲染 #}
<div>
    {%- archiveDetail articleContent with name="Content" render=true %}
    {{ articleContent|safe }}
</div>

Or more commonly, directly througharchiveObject access and rendering:

<div class="article-content">
    {{ archive.Content|render|safe }}
</div>

Here|renderThe filter ensures that Markdown text is correctly parsed and converted into HTML structure. Following it is the|safeThe filter is responsible for safely outputting the converted HTML content to the page, preventing it from being escaped again. Usually, if the backend is set up with a Markdown editor, the system will automatically render it, but explicit usage is recommended|renderThis can be ensured, especially whenContentIt may be a mix of rich text and Markdown.

In summary, on the document detail page of AnQiCMS, no matterContentThe core practice of displaying field content directly containing HTML rich text or Markdown text safely and correctly on the page is to add it after.|safeFilter. If the content is in Markdown format, you can consider using it first|renderto convert it, then use|safeoutput. Use it correctlysafeFilter, it is an important aspect of balancing content richness and website security.


Frequently Asked Questions (FAQ)

1. Why do I show directly.{{archive.Content}}When, the page displays the HTML code with angle brackets instead of the rendered graphic effect?

This is because of the default security mechanism of AnQiCMS template engine.To prevent potential XSS attacks, the system will automatically escape any string content that may contain HTML tags.This means<p>Will become&lt;p&gt;,<img>Will become&lt;img&gt;etc. If you are sure that the content is safe and needs to render HTML, you need to{{archive.Content}}then add|safea filter such as{{archive.Content|safe}}.

2.|safeDoes the filter always safely display all content?

|safeThe filter indicates that the template engine treats the content as “safe”, no longer performs HTML escaping, and outputs the original HTML directly.Therefore, its security depends on your trust in the content source. IfContentThe content of the field is generated by the backend rich text editor or Markdown editor, which is usually safe. However, if the content comes from untrusted user input or external uncleaned data, use|safeThe risk of bringing XSS vulnerabilities. When handling such data, it should be strictly filtered and sanitized before being saved to the database.

3. If myContentHow should I handle a field that contains both HTML edited through a rich text editor and Markdown text written or pasted?

The AnQiCMS Markdown editor, when enabled, usuallyContentautomatically identifies and renders the Markdown syntax in the field. When youContentWhen a field may mix rich text and Markdown, **the practice is to use first,|renderThe filter ensures that the Markdown section is correctly converted to HTML and then used|safeThe filter output. For example:{{ archive.Content|render|safe }}. This can maximize compatibility with two content formats and ensure that the final output is rendered as HTML.

Related articles

Can I control whether Markdown content in AnQiCMS templates is automatically converted to HTML?

In website content management, we often need to balance the writing efficiency of content and the final presentation effect.Markdown with its concise syntax greatly enhances the speed of content creation.But the question that follows is: do we always want the content to be automatically converted to HTML when it enters the template?Or in some specific scenarios, we want to maintain the original Markdown format, or manually control the conversion process?

2025-11-08

How does AnQiCMS render Markdown formatted article content to HTML?

AnQiCMS boasts its efficient content management capabilities, is favored by users, especially in handling text content, providing flexible and diverse options.For users accustomed to using Markdown format, AnQiCMS also provides comprehensive support, able to seamlessly render Markdown formatted article content into user-friendly HTML pages.To understand how AnQiCMS achieves this transformation, we can explore from three aspects: content creation, system configuration, and template rendering.

2025-11-08

How to avoid AnQiCMS from destroying the tag structure when truncating HTML text?

In content management, in order to maintain the neatness and loading efficiency of the website page, we often need to truncate articles, product descriptions, or other long text content, and only display part of the abstract.However, if the content itself contains HTML tags, simple character truncation often breaks the structure of these tags, causing the page display to become chaotic and even affecting the layout and functionality of the website.For example, the content of a `<p>This is an <b>important</b> paragraph</p>`, if it is simply truncated to `<p>This is an <b>important</b`

2025-11-08

How to safely truncate a long text containing HTML tags in AnQiCMS template?

In AnQiCMS template design, we often encounter scenarios where it is necessary to display long text content, such as the abstract on the article list page, and the brief introduction of product details.If this long text content is displayed directly, it may cause the page to be long, affecting user experience and layout aesthetics.Therefore, truncating long text is a common requirement.However, when long text content contains HTML tags, simple character truncation may cause problems.For example, a content like this `\u003cp\u003eThis is an important\u003cb\u003e text\u003c/b\u003e\u003c/p\u003e`

2025-11-08

How to prevent AnQiCMS template from automatically escaping HTML tags and output the original content directly?

When using AnQiCMS to build a website and design a template, you may encounter a common problem: when outputting some content in the template, the tags that were originally expected to be displayed as HTML are automatically converted to plain text, for example, `<p>This is a paragraph</p>` becomes `&lt;p&gt;This is a paragraph&lt;/p&gt;`.This loses the original style and structure of the content.Understanding this problem and knowing how to handle it is very important for template developers.Why does the AnQiCMS template automatically escape HTML tags?

2025-11-08

What are the potential uses of the `safe` filter in AnQiCMS besides displaying HTML?

In AnQi CMS template engine, the default automatic escaping mechanism is an important security feature, which can convert special characters in HTML tags and JS scripts (such as `<`, `>`, `&`, etc.) to corresponding HTML entities, thereby effectively preventing cross-site scripting (XSS) attacks.However, in certain specific content output scenarios, we indeed need to allow the browser to parse and render the HTML or similar HTML code as it is, at which point the `safe` filter becomes crucial.

2025-11-08

How does AnQiCMS template escape HTML to prevent XSS attacks when displaying user submitted content?

In today's network environment, website security is of great concern to operators, among which cross-site scripting attacks (XSS) are one of the common security threats.XSS attacks inject malicious scripts into web pages, steal user data, alter page content, and even control user sessions.AnQiCMS as a content management system that focuses on security, built a series of powerful HTML escaping mechanisms to effectively prevent such attacks when processing user submitted content and displaying it in templates.

2025-11-08

What are the differences and usage scenarios between the `escape` filter and the `e` filter in AnQiCMS?

In the development of Anqi CMS templates, we often encounter the need to handle the security of content display, especially when the content may contain user input or be obtained from external sources.It is particularly important to escape special characters at this time to prevent potential cross-site scripting attacks (XSS).AnQi CMS provides the `escape` and `e` filters to help us deal with such issues, they have the same function, and `e` is just an abbreviation alias of `escape`.

2025-11-08