How to use the `autoescape` tag to control the automatic escaping of HTML content in AnQiCMS templates?

Calendar 👁️ 73

It is crucial to understand and effectively control the escaping behavior of HTML content during AnQiCMS template development.This concerns not only the correct display of page content, but also has a direct impact on the security of the website, especially in preventing cross-site scripting (XSS) attacks.autoescapeTags play a core role.

Understand the necessity of HTML content escaping

In dynamic websites, we often need to output content from databases or other sources.If this content includes HTML tags or JavaScript code (such as user submitted comments, article content, etc.), and is directly rendered to the browser without processing, it may pose security risks.<script>Tags to execute arbitrary JavaScript code, steal user information, or destroy the page.

The essence of HTML content escaping is to convert special characters in HTML (such as</>/&/"/') to their corresponding HTML entities (such as&lt;/&gt;/&amp;/&quot;/&#39;)。Thus, the browser will not parse these special characters as HTML structure or executable scripts, but will display them as plain text.

AnQiCMS's template system follows the principle of security first by default, and will automatically escape HTML for all content passed through{{ 变量 }}This means that even if variables contain<script>alert('xss');</script>Such code will also be displayed on the page&lt;script&gt;alert(&#39;xss&#39;);&lt;/script&gt;Thus, it effectively avoids potential XSS attacks.

autoescapeThe use of tags

Although automatic escaping is the default and recommended behavior, there may be certain specific scenarios where we may need to temporarily disable it, such as when you are sure that the content you are outputting is completely trustworthy and strictly reviewed HTML code. In such cases, you can useautoescapeThe tag is used to finely control the escaping behavior.

autoescapeThe tag can turn on or off the automatic HTML escaping within the template code block. It is followed by{% autoescape on %}turning on,{% autoescape off %}turning off, and it needs to be followed by{% endautoescape %}end.

1. Disable automatic escaping ({% autoescape off %})

When you need to ensure that all variables within a code block are output directly as HTML without escaping, you can useautoescape off.

{# 默认情况下,以下代码会被转义 #}
<p>默认输出: {{ "<p>这是一个<strong>安全的</strong>段落。</p>" }}</p>
{# 实际页面输出: <p>默认输出: &lt;p&gt;这是一个&lt;strong&gt;安全的&lt;/strong&gt;段落。&lt;/p&gt;</p> #}

{% autoescape off %}
{# 在此块内,自动转义被禁用 #}
<p>禁用转义输出: {{ "<p>这是一个<strong>不转义的</strong>段落。</p>" }}</p>
{# 实际页面输出: <p>禁用转义输出: <p>这是一个<strong>不转义的</strong>段落。</p></p> #}
{% endautoescape %}

<p>块外再次默认输出: {{ "<p>又是一个<strong>安全的</strong>段落。</p>" }}</p>
{# 实际页面输出: <p>块外再次默认输出: &lt;p&gt;又是一个&lt;strong&gt;安全的&lt;/strong&gt;段落。&lt;/p&gt;</p> #}

Please note that disabling automatic escaping poses a security risk, please make sure that all the content within this code block is completely trusted and verified.

2. Enforce automatic escaping ({% autoescape on %})

autoescape onThe tag is used to force HTML escaping within its code block, even if escaping may be disabled in the external environment or parent template.This helps to ensure that the code in a specific area remains secure.

{% autoescape off %}
    {# 外部环境禁用转义 #}
    <p>外部禁用转义:{{ "<b>Hello World!</b>" }}</p> {# 输出: <b>Hello World!</b> #}

    {% autoescape on %}
        {# 在此块内,强制开启转义 #}
        <p>内部强制转义:{{ "<b>Hello World!</b>" }}</p> {# 输出: &lt;b&gt;Hello World!&lt;/b&gt; #}
    {% endautoescape %}

    <p>回到外部禁用转义:{{ "<b>Hello Again!</b>" }}</p> {# 输出: <b>Hello Again!</b> #}
{% endautoescape %}

withsafeCollaboration of the filter

exceptautoescapeTags, AnQiCMS also providessafeA filter used to mark the content of a single variable as 'safe', indicating to the template engine not to HTML-escape it.

safeThe usage of the filter is very simple: add it after the variable that needs to be output|safe.

{% set trusted_html_content = "<h2>这是后台编辑的<i>可信</i>HTML标题</h2>" %}
{% set user_input_comment = "<p>用户评论:<script>alert('危险!');</script></p>" %}

<p>原始输出 (默认转义):</p>
{{ trusted_html_content }}
{{ user_input_comment }}

<p>使用 `safe` 过滤器输出 (不转义):</p>
{{ trusted_html_content|safe }}

<p>即使使用 `safe`,但如果内容本身不安全,后果自负:</p>
{{ user_input_comment|safe }}

autoescapewith the tag andsafeThe difference between filters:

  • Scope of action: autoescapeThe tag controls a Code blockThe escape behavior inside it takes effect on the output of all variables.safeThe filter only acts ona single variableoutput.
  • Flexibility: safeFilters provide finer control

Related articles

escape` filter and `e` filter in AnQiCMS template are they functionally the same?

During the development of AnQiCMS templates, the security of data output is a focus we need to pay attention to.Frequently encounter issues about `escape` filter and `e` filter, many users are curious about whether there are differences in their functions.

2025-11-08

How does the AnQiCMS template escape special characters in HTML or JavaScript code to prevent XSS attacks?

When building a website with AnQiCMS, we often need to fill dynamic content into the page template, which includes text that may come from user input.However, if not handled properly, the content entered by these users may be exploited by malicious attackers to plant malicious scripts, thereby triggering cross-site scripting (XSS) attacks.XSS attacks can steal user data, tamper with page content, even hijack user sessions, causing serious harm to websites and users.AnQiCMS as a content management system that focuses on security

2025-11-08

The `dump` filter has what help for understanding complex data structures in AnQiCMS template development?

During the template development process of AnQi CMS, we often need to deal with various data passed from the backend.AnQiCMS is a powerful content model with flexible tag system, which allows us to easily obtain articles, categories, pages, and even custom fields of data.However, when the data structure becomes complex, or when we are unsure of what content a variable contains, the efficiency of development debugging will be greatly reduced.At this moment, the `dump` filter acts like a powerful 'data perspective mirror', which can help us clearly understand these complex data structures

2025-11-08

How can you view the internal structure and value of a variable in AnQiCMS templates for debugging?

During the development and content operation of Anqi CMS templates, a deep understanding of the internal structure and specific values of variables in the templates is the key to efficient debugging.When you are faced with abnormal data displayed on a page or are unsure about the available properties of an object returned by a certain tag, being able to quickly view the detailed information of variables will undoubtedly greatly enhance the efficiency of solving problems. AnQi CMS provides a flexible and powerful template engine, which draws on the syntax of Django templates, and also includes some very practical debugging tools, allowing you to directly check variables in template files. Below

2025-11-08

How to split a long string into an array of strings in AnQiCMS template?

During AnQiCMS template development, we often encounter situations where we need to flexibly handle data, one common requirement being to split a long string containing multiple pieces of information into an independent string array using a specific delimiter (such as a space), so as to facilitate loop display or further operations.AnQiCMS's template engine provides a concise and efficient filter (Filter) function, which can easily achieve this goal.### Understand `split`

2025-11-08

What is the type of the string array after splitting the `fields` filter in AnQiCMS template?

In AnQiCMS template development, we often need to process text content, such as splitting a string of keywords, tags, or descriptions into independent entries for list display or further operations.At this time, the `fields` filter has become a very practical tool.It can efficiently complete the splitting of strings, but many users may be curious about the data types of the split strings after the `fields` filter is applied.Understand the working mechanism of the `fields` filter

2025-11-08

How to get the string in AnQiCMS template?

In AnQiCMS template development, text and strings are the foundation for building website content.Flexible and efficient string retrieval and manipulation are skills that template developers must master, whether it is to display article titles, website names, or process user input.AnQiCMS's powerful template engine is based on Go language, providing rich tags and filters, making string acquisition and processing intuitive and practical.### Core Mechanism: Understanding the String Retrieval Method of AnQiCMS Template The most direct way to retrieve strings in AnQiCMS templates is through variable references

2025-11-08

How to display the title and content of articles in AnQiCMS templates?

Manage website content in AnQiCMS, the ultimate goal is to be able to present this content to visitors in an elegant and efficient manner.For the most common content type of articles, how to accurately display the title and body in front-end templates is a basic skill that every website operator needs to master.AnQiCMS powerful template function, providing us with a flexible and intuitive implementation method.### Core: Get to know the `archiveDetail` tag AnQiCMS's template system is designed to be very user-friendly

2025-11-08