How to safely display HTML tags in a template without being parsed by the browser?

Calendar 👁️ 74

In website content management, we often encounter the need to display HTML code snippets, such as code examples in tutorials, and language annotations in technical articles.However, if these contents containing HTML tags are output directly to the page, the browser will parse and render them, rather than displaying them as plain text.<script>alert('XSS')</script>It will trigger a cross-site scripting (XSS) attack, posing a huge security risk to the website.

The template engine design of AnQiCMS (AnQiCMS) fully considers this point, it provides a set of secure and flexible mechanisms to handle the display of HTML content.Understanding these mechanisms can help us safely display content while also effectively preventing potential security risks.

Understand the default behavior of the template: automatic escaping.

AnQiCMS's template engine, similar to mainstream template engines like Django, defaults to processing all through{{ 变量 }}The content output in this wayAutomatically escape HTMLThis means that when template variables contain such as</>/&/"/'etc. special HTML characters, they are automatically converted to the corresponding HTML entities, such as<Will become&lt;,>Will become&gt;.

This default escaping behavior is the first line of defense for website security, ensuring that even if users unintentionally or maliciously insert HTML tags or JavaScript code when submitting content, these contents will be "harmless" processed and displayed as plain text on the page without being parsed and executed by the browser.

For example, if you enter the following content in a field in the background:

<p>这是一段包含<strong>粗体</strong>文字的HTML。</p>
<script>alert('Hello, AnQiCMS!');</script>

and try to use it in a template:{{ archive.Content }}then what you actually see in the browser is:

<p>这是一段包含<strong>粗体</strong>文字的HTML。</p>
<script>alert('Hello, AnQiCMS!');</script>

It is not a rendered paragraph and a pop-up. This is the automatic escaping in action.

Explicit escaping:escapeFilter

Although the default automatic escaping has provided good protection, in certain specific scenarios, we may need to explicitly indicate to the template engine that it should escape a variable as HTML, especially when dealing with complex data structures or when overriding other possible behaviors. At this point, you can useescapefilter.

escapeThe filter's role is to convert special HTML characters in the content to HTML entities. Its usage is very intuitive, just add it after the variable name|escapeand it is done:

{# 假设 raw_html_string 变量包含未经处理的HTML内容 #}
<div>
    我们想显示这段代码:<code>{{ raw_html_string|escape }}</code>
</div>

For example, ifraw_html_stringThe value is<img src="x" onerror="alert('XSS')">, then through{{ raw_html_string|escape }}After output, the content will be displayed on the page<img src="x" onerror="alert('XSS')">This plain text, rather than an image element that may trigger an attack.

escapeThe filter is a critical tool to ensure that any content that may contain HTML characters is displayed in plain text form, working in collaboration with the default auto-escape to provide double protection for content display.

Clean as needed:striptagsandremovetagsFilter

Sometimes, our need is not to display HTML tags, but to completelyremovethem, only retaining plain text content. AnQiCMS providesstriptagsandremovetagsTwo filters are used to achieve this.

  • striptagsFilter: As the name implies, it will remove all HTML tags from the content, leaving only the text within the tags. This is very useful in scenarios such as extracting article summaries, generating plain text descriptions, etc.

    {# 假设 article.Description 包含 HTML 内容 #}
    <p>文章摘要:{{ article.Description|striptags }}</p>
    

    Ifarticle.DescriptionIs<span>这是一段<em>加粗</em>的描述。</span>then{{ article.Description|striptags }}will output这是一段加粗的描述。.

  • removetagsFilterIf you only need to remove specific HTML tags instead of all,removetagsthe filter can be used. It accepts a comma-separated list of tag names as parameters.

    {# 移除内容中的 <b> 和 <i> 标签 #}
    <div>
        过滤后的内容:{{ some_html_content|removetags:"b,i"|safe }}
    </div>
    

    It should be noted that,striptagsandremovetagsThe purpose is to clean up HTML, rather than display it as text.In practical applications, they are often used to preprocess user-generated content to meet page display or search engine crawling requirements.

Summary and **practice**

In AnQiCMS template, to safely display HTML tags without being parsed by the browser, the core is to use the template engine'sautomatic HTML escaping mechanismAnd thenescapeFilterPerform clear, mandatory escaping.

Here are some **practical suggestions:

  1. Trust default escapingFor all content retrieved from the backend or user input, unless you explicitly know its source is safe and intended to be rendered as HTML, you should trust the default escaping behavior of the template engine.
  2. Use actively|escapeWhen you need to display code snippets, HTML examples, or any text that may contain special HTML characters, use actively{{ 变量|escape }}Filter to ensure they are displayed as plain text.
  3. Use with caution.|safeConversely, if you want the HTML content in a variable to be parsed and rendered by the browser (for example, when editing rich text in a backend editor), you need to use{{ 变量|safe }}Filter. But please ensure that these contents are from reliable sources and have been strictly filtered and verified, as abuse|safeis the main cause of XSS attacks.
  4. Utilizestriptagsandremovetagsclean the contentWhen the goal is to completely remove HTML tags to obtain plain text, these two filters are your good assistants.

By reasonably using these template functions, you can easily achieve the safe display of HTML tags in AnQiCMS, while maintaining the overall security of the website.


Frequently Asked Questions (FAQ)

Q1: Why did I output in the template<p>Hello</p>, but the browser didn't display a paragraph?A1: This is because of AnQiCMS template

Related articles

How to dynamically display the canonical URL corresponding to the current page in the template?

In website operation, maintaining search engine friendliness is one of the key links to improving website visibility.Among them, the 'Canonical URL' plays a crucial role.It acts like a compass for search engines, telling them which page is the main version of the content, thus effectively avoiding ranking fragmentation due to content duplication or similarity.For content operators, correctly setting and displaying standard links is the foundation for ensuring that website content is efficiently crawled and indexed by search engines

2025-11-08

How to prevent content collection interference code and watermark display in AnQi CMS?

Today, with the increasing richness of digital content, the value of original content becomes more and more prominent, but the problems of content plagiarism and collection that come with it also make many content creators and operators headache.AnQiCMS (AnQiCMS) fully understands this pain point, and therefore built-in strong content protection mechanisms from the very beginning of the system design, including anti-crawling interference codes and image watermark functions.These features are designed to help users effectively defend original copyright, ensure content exclusivity, and thereby enhance content value.

2025-11-08

How to display the document list matching the keywords on the search results page?

When visitors come to your website and hope to quickly find the information they need, a highly efficient and accurate on-site search feature is particularly important.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, providing you with a flexible and powerful content management and template system, allowing you to easily display a list of documents matching the search keywords on the search results page.Next, let's see how to implement this function together.### Learn about AnQi CMS search mechanism AnQi CMS has built-in powerful search capabilities, it will automatically index the content you publish

2025-11-08

How does AnQi CMS display or hide specific content based on user group permissions?

In daily website operation, we often encounter such needs: some content is intended for everyone to see, while some is only open to specific members, or shows different information for users of different levels.AnQiCMS (AnQiCMS) provides powerful user group permission management features, allowing us to flexibly control the visibility of website content and achieve the 'thousand faces of content'. ### Why do you need to display or hide content by user group?Imagine you are running a website that offers a variety of services, including free basic articles and paid in-depth reports

2025-11-08

How to display custom site parameters on the front-end page?

When operating a website, we often encounter such needs: we need to display some flexible and variable site information on the front-end page, such as the company's establishment year, customer service hotline, promotional slogans for specific pages, or a brief announcement, etc.This information may need to be updated frequently, and it is inefficient and prone to errors to modify the template file each time.Fortunately, Anqi CMS has provided us with a very convenient way to manage and display customized site parameters on the back-end, making content operation more efficient and flexible.

2025-11-08

How does AnQi CMS display the prompt information when the website is closed?

During the operation of the website, we often encounter situations where we need to maintain, upgrade the website, or temporarily close the website for some special reasons.How can you inform the visitor in a friendly manner about the current status of the website, to avoid them seeing a chaotic or error page, which is particularly important.AnQiCMS (AnQiCMS) fully considers this requirement, built-in convenient and flexible shutdown prompt function, allowing you to easily deal with such scenarios. ### Flexible website status configuration, easily enable shutdown mode The shutdown function of Anqi CMS is very intuitive to configure. You just need to log in to the website backend

2025-11-08

How to ensure that the website content of AnQi CMS adapts to display on different devices (PC, mobile)?

One of the most painful problems we face in running websites in this multi-screen era is how to make the content of the website display effectively on different devices.Users may browse using a PC, smartphone, or tablet. If the content does not adapt to display, the user experience will be greatly reduced.AnQiCMS (AnQiCMS) provides a very mature and flexible solution in this aspect, aiming to ensure that your website content looks pleasing on any device.### AnQiCMS' Various Content Adaptation Modes AnQiCMS understands that different websites have different needs

2025-11-08

How to customize the content display layout of the Anqi CMS homepage?

Your website's homepage is the first impression for visitors, as well as the showcase of the brand image and core content.How to make this "facade" both aesthetically pleasing and efficient in conveying information is a concern for every website operator.AnQiCMS (AnQiCMS) as a content management system that focuses on customization and flexibility, provides us with a very convenient way to freely create a dedicated homepage layout.

2025-11-08