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

Calendar 👁️ 70

When using AnQiCMS to build a website and design templates, 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>这是段落</p>Became&lt;p&gt;这是段落&lt;/p&gt;This has lost the original style and structure of the content. Understanding this issue and knowing how to handle it is very important for template developers.

Why does AnQiCMS template automatically escape HTML tags?

It should be clearly stated that this automatic escaping is not a system failure, but an important security mechanism designed to prevent cross-site scripting (XSS) attacks. Imagine if the system were to output any user input without distinction, malicious users might insert a segment<script>alert('XSS攻击');</script>Code. Once this code is rendered on the page, it will execute in the browsers of other visitors, potentially stealing user information, tampering with page content, and so on.

To avoid such security risks, AnQiCMS (and many other modern template engines like Django) defaults to escaping HTML content retrieved from the backend database. This means that all special characters that may be parsed as tags by the browser, such as</>/&/"/'It will be converted to the corresponding HTML entity, ensuring that they are displayed as plain text and not executed by the browser.

However, in some cases, we indeed need content to be displayed in its original HTML form, such as the content of articles edited in rich text editors, specific advertising codes, or HTML fragments that we ourselves have written and confirmed to be safe.This is when you need to tell the AnQiCMS template which content is trustworthy and does not need to be escaped.

How to output content directly: two main methods.

AnQiCMS template engine provides two main ways to control the escaping behavior of HTML content, allowing you to directly output the original content according to your actual needs.

Method one: use|safeFilter

This is the most commonly used and most direct method, suitable for when you need to output the original HTML content contained in a single variable. In the AnQiCMS template, you can add|safeTo implement a filter.

For example, if you have a variable of article content fetched from the backendarchive.ContentIt contains HTML tags, and if you want these tags to be parsed and displayed normally by the browser, you can write it like this:

<div>
    {{ archive.Content | safe }}
</div>

This is like telling the system, 'I believe this content is safe, please display it directly without any processing.'This filter will prevent the system from escaping the content of the variable, thereby allowing HTML tags to be rendered correctly.This method is very suitable for displaying articles, product details, and other content entered from rich text editors.

Method two: use{% autoescape off %}Tag

If you have a large code block, or you are sure that all variable outputs should be displayed directly as HTML, then use{% autoescape off %}and{% endautoescape %}The tag pair will make it more convenient to wrap this part of the content.

All the content wrapped in this pair of tags will no longer be subject to the default HTML escaping rules. For example:

{% autoescape off %}
    <div>
        <!-- 这里面的所有变量输出,包括 {{ variable1 }} 和 {{ variable2 }} 等,都将直接输出原始HTML -->
        <p>欢迎来到我的网站!</p>
        {{ some_html_content_variable }}
        <p>请点击 <a href="/contact">这里</a> 联系我们。</p>
    </div>
{% endautoescape %}

This method is suitable when you need to embed a large block of known safe and preformatted HTML code, thus avoiding manual addition to each variable|safefilter.

When and how to safely use these methods

Though|safeFilters andautoescape offTags can solve the problem of HTML content escaping, but be cautious when using them. Once escaping is disabled, you are responsible for verifying content safety.

  1. Reliability of content sourceUse these methods only for content sources you completely trust.The most common scenario is the content published by backend administrators through rich text editors, as this content is usually manually reviewed.
  2. Content Review and FilteringEven content from administrators should be recommended to use content security management and sensitive word filtering functions on the background to further reduce risks. It is not recommended to use comments or other information submitted by users directly unless it is strictly purified on the server side|safeorautoescape offIn order to prevent normal users from maliciously submitting HTML/JS code.
  3. Processing Markdown content.If you have content written through the AnQiCMS backend Markdown editor, you may find that even if you have used|safe, Markdown syntax has not been converted to HTML. This is because|safemerely disable HTML escaping, not executing Markdown to HTML conversion. For Markdown content, AnQiCMS providesrenderparameters, for example{{ archive.Content | render | safe }}first pass throughrenderConvert it from Markdown to HTML, then through|safeEnsure that the converted HTML can be output directly, rather than being escaped again.

You have mastered these methods, and you can flexibly control the display of HTML content in the AnQiCMS template, making your website more rich and dynamic, while also ensuring the safety of the website.


Frequently Asked Questions (FAQ)

1. Is my website safe after disabling HTML escaping?Disabling HTML encoding increases the risk of a website facing XSS (cross-site scripting) attacks. You should only use content that you completely trust and confirm does not contain any malicious scripts.|safeOr filter.{% autoescape off %}Label. Any content submitted by users that has not been strictly reviewed and purified should avoid disabling escaping.AnQiCMS provides content security management and sensitive word filtering features that can help you manage content risks to a certain extent.

2. I used it in the template|safeBut why is the Markdown content not rendered as HTML? |safeThe filter's role is to inform the template engine not to escape the special HTML characters in the variables, so that they are displayed in their original HTML form.It itself does not convert Markdown syntax to HTML.If your content is in Markdown format, you need to go through the AnQiCMS providedrenderParameters are converted, for example{{ archive.Content | render | safe }}. So, Markdown will be converted to HTML first, and then safely output to the page.

3.|safeand{% autoescape off %}What is the difference between tags, and which one should I use?The main difference lies in the scope of action.|safeThe filter acts on the output of a single variable, for example{{ some_variable | safe }}However{% autoescape off %}The tag applies to all the content within the code block. If you only need to directly output the HTML content of a few variables, use|safeMore precise and recommended. If a large block of template code contains multiple variables, and you are sure that all the HTML content within this code block is safe, then use{% autoescape off %}Label pairs will make it more concise. No matter which method is used, it should fully assess the security risks it brings.

Related articles

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

On a website built with AnQiCMS, the core of the document detail page is often the main content of the article, namely the `Content` field.This field carries a wealth of information, ranging from simple text to complex text and image layouts, multimedia embedding, and even custom code segments.Therefore, how to correctly and safely display these contents containing HTML format in the template is a key skill that every AnQiCMS user needs to master.AnQiCMS when designing template rendering, fully considers the security of the content.

2025-11-08

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

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

How to handle JavaScript code output containing special characters (such as `&lt;script&gt;`) in AnQiCMS?

In website operation, we sometimes need to output custom JavaScript code on the page, which may be to implement specific interactive functions, integrate third-party service scripts (such as statistical codes, advertising codes), or add some dynamic effects to the page.However, when these JavaScript codes themselves contain some special characters, especially HTML tags (such as `<script>`), if not handled correctly, it may cause the page to display abnormally, disable functions, or even bring serious security vulnerabilities.

2025-11-08