How to safely display HTML content retrieved from a database on the frontend without escaping?

Calendar 78

In website operation, we often need to display some content with rich formatting, such as mixed text and images on article detail pages, HTML tables on product introduction pages, or interactive codes embedded in custom pages.This content is usually stored in a database and rendered on the front-end page.However, many content management systems (including the familiar AnQiCMS) by default, for website security, will escape the HTML content retrieved from the database, causing the front-end to display the original HTML code instead of the expected effect.

Today, let's talk about how to safely display the HTML content from these databases on the front end in AnQiCMS, while avoiding unnecessary escaping.

Understand the default behavior and security considerations of AnQiCMS

Firstly, we need to understand why AnQiCMS (and many modern CMS) defaults to escaping HTML content.This is mainly for security reasons, especially to prevent cross-site scripting attacks (XSS).<script>alert('XSS')</script>Such code, and if the system does not escape it directly to the front end, then other users visiting the page may be attacked.AnQiCMS is a Go language CMS that focuses on security, one of its design philosophies is to make "all websites in the world safe", therefore, the default automatic escaping mechanism is an important part of its security protection.

When AnQiCMS's template engine retrieves content from the database and outputs it to the front end, it will<to&lt;,>to&gt;,"to&quot;Entities in HTML, so that the browser will not parse them as HTML tags or execute the script within them, but display them as plain text.

When do you need to display unescaped HTML content?

Of course, not all HTML content is malicious. In many legitimate scenarios, we indeed need to display rich text content stored in databases, such as:

  • Rich text editor (WYSIWYG) output content:When editing articles in the background, titles, paragraphs, images, links, and other elements created using the rich text editor are inherently valid HTML structures.
  • Complex layout of the product detail page:May contain tables, lists, special styles, etc.
  • Custom embedded code for pages:For example, embedding third-party video players, map components, etc.

In this case, we need to explicitly inform the AnQiCMS template engine that this content is "safe" and does not require escaping, and can be parsed directly as HTML.

AnQiCMS solution:|safeFilter

In the AnQiCMS template system, the most commonly used and direct method to display unescaped HTML content is to use|safefilter.

When you output a variable in a template, just add|safe,AnQiCMS will skip the HTML escaping process for this part of the content, and output it directly as HTML code to the browser, thereby achieving the desired effect.

For example, if there is one in our article detail page template,articleContenta variable stores the HTML content of the article, we would usually use it in this way:

<div>
    {%- archiveDetail articleContent with name="Content" %}
    {{ articleContent|safe }}
</div>

Here, archiveDetailThe tag is used to get document details and assign the document content toarticleContent. Then,{{ articleContent|safe }}ensuring that this HTML content can be correctly parsed and rendered by the browser. Whether it is the content of the article'sContentfield, or categorizedContentfield, even single-pageContentfield, can be displayed safely in this way

Additional processing for Markdown content:|renderwith|safecombination

With the popularity of Markdown editors, many operators are accustomed to writing content in Markdown format.AnQiCMS also supports enabling Markdown editor in the background.Markdown itself is a lightweight markup language that needs to be rendered into HTML before it can be displayed as rich text in a browser.

If your database stores content in Markdown format and you want to render it as HTML on the front end, you need to use|renderThe filter converts it to HTML, then, it also needs to use|safea filter to ensure that the converted HTML is not escaped.

For example, if you have a custom fieldintroductionThe text stored is Markdown:

{% archiveDetail introduction with name="introduction" %}
{{ introduction|render|safe }}

Here|renderThe Markdown text will be parsed and generated into HTML, followed by the|safeIt ensures that the generated HTML can be directly parsed by the browser, rather than displayed as plain text.

Greater control:autoescapeTag

Besides using individual variables|safeOutside the filter, AnQiCMS's template engine also providesautoescapetags to control the automatic escaping behavior of a certain block in the template.

You can use{% autoescape off %}and{% autoescape on %}Enclose a piece of code to explicitly turn off or on automatic escaping.

{% autoescape off %}
    {# 在这里的所有变量输出都将默认不被转义 #}
    {{ someHtmlContent }}
    <div>
        {{ anotherHtmlSnippet }}
    </div>
{% autoescape on %}
    {# 再次开启自动转义,恢复默认安全行为 #}
    {{ plainText }}
{% endautoescape %}

However, please be cautious in using it.{% autoescape off %}.It will turn off the automatic escaping feature of the block, if the block contains content from user input or other untrusted sources, it will greatly increase the risk of XSS attacks. Usually, we recommend using|safeThe filter, as it precisely allocates 'unescaped' permissions to each variable, making code easier to review and less prone to errors. It should only be considered when absolutely necessary and when there is absolute confidence in the source of the content.autoescape off.

Summary and **practice**

In AnQiCMS, safely displaying HTML content from the database without being escaped mainly depends on|safethe filter. For Markdown content, you need to|render|safeUse in combination.

  • Clarify your intention:Use HTML parsing only when you are sure that the content is valid and needs to be parsed as HTML.|safe.
  • Input purification:Though|safeAllow HTML content to be displayed, but frontend rendering is just the final step.The most critical protection should be completed before the content enters the database.Ensure that all user inputs (especially content from rich text editors) are strictly sanitized and validated on the server side, filtering out potential malicious scripts.AnQiCMS includes content security management and sensitive word filtering functions, which are a good first line of defense, but as operators and developers, we also need to be vigilant about the source and security of the content.
  • Local control:Use as much as possible|safeThe filter processes specific variables instead of globally closing themautoescapeto minimize safety risks.

By these methods, we can not only take advantage of the powerful functions of AnQiCMS to display a variety of content, but also effectively maintain the security of the website.


Frequently Asked Questions (FAQ)

Q1: If I forget to use HTML content|safeWhat will happen to the filter? A1:If I forget to use|safeThe filter, AnQiCMS template engine will default escape HTML tags and special characters retrieved from the database. This means that your users will see the original HTML code on the frontend page, for example<p>这是一个段落</p>It will be displayed as&lt;p&gt;这是一个段落&lt;/p&gt;It is not the actual rendered paragraph effect. The page layout may become chaotic, or lose the appropriate style and interaction.

Q2: Use|safeIs my website completely safe after the filter? A2:Not entirely.|safeThe filter just tells AnQiCMS template engine that this content is considered safe and does not need to be escaped.But the actual security of the content depends on its source.|safeThe filter allows these malicious scripts to execute on the front end, leading to XSS attacks.Therefore, ensuring the reliability of the content source and cleaning user input on the server side (filtering out malicious HTML tags and attributes) is the first crucial step.

Related articles

How to format a timestamp into a readable date in the template (such as: 2023-01-01)?

<ctrl94>thought Here's a thinking process that leads to the article above: 1. **Understand the Goal:** The user wants an article about formatting timestamps in AnQiCMS templates. I need to act as

2025-11-09

How to customize parameter filtering for an article or product list?

In daily website operations, we often encounter such needs: we hope that users can quickly filter out the content they are interested in based on the different attributes of articles or products.For example, on a real estate website, users may want to filter for 'two bedrooms and one living room' 'school district' housing;On a product showcase website, users may want to find 'red' new T-shirts.AnQiCMS (AnQiCMS) provides a powerful and flexible custom parameter function, which can help us easily achieve this refined content filtering

2025-11-09

How to implement pagination in the content list?

When managing website content, especially when the amount of content increases, a straightforward and efficient pagination feature becomes particularly important.It not only allows visitors to find the information they need faster, improves the browsing experience, but also avoids performance issues caused by loading too much content on a single page.In Anqi CMS, implementing the pagination function of the content list is flexible and direct, and it can be configured to completion in just a few steps.The Anqi CMS adopts the Django template engine syntax, allowing you to control the display logic of content through clear tags.Add pagination to your content list, we will mainly use two core tags

2025-11-09

How to display the friend link list of the website?

During the operation of a website, friendship links are a common way for websites to recommend each other and share traffic. They not only help to increase the number of external links on the website, but also have a positive effect on SEO optimization, and can provide visitors with more valuable resources.AnQiCMS (AnQiCMS) provides a very convenient function for us to manage and display friend links. ### Managing Friend Links: Getting Started with Backend Operations Managing friend links in AnQi CMS is very intuitive.First, we need to log in to the website backend, find the "Function Management" item in the left function menu.

2025-11-09

How to truncate long text content and add an ellipsis (...)

In the daily management of website content, we often encounter such situations: an article's summary, a product description, or a list item title, if the content is too long, it not only occupies too much page space but may also destroy the overall layout aesthetics.Especially on mobile and other small screen devices, overly long text can seriously affect user experience.How to elegantly truncate these texts and add common ellipses (...) to make the information complete and tidy, which is a very practical skill in content operation.

2025-11-09

How to automatically convert URL strings in text to clickable links?

In daily content operations, we often need to mention various URLs in articles, product descriptions, or page introductions.If these URLs are displayed as plain text, users will need to manually copy and paste them to access, which undoubtedly will greatly reduce their reading experience and the interactivity of our website.How can we automatically make the URL strings in these texts clickable?In AnQi CMS, this implementation is very simple, no complex code development is required, just use its powerful template filter function to easily handle it.

2025-11-09

How to perform basic arithmetic operations in the template?

In Anqi CMS template design, it can flexibly handle data, perform simple calculations, and is an indispensable part of creating dynamic and functional websites.You may find that in addition to displaying the content itself, it is also necessary to perform some basic arithmetic operations at the template level, such as calculating the total price of goods, displaying the discounted price, or dynamically adjusting the style of elements based on numerical values.The AnQi CMS template engine (similar to Django syntax) provides us with intuitive and powerful arithmetic calculation capabilities, allowing these requirements to be easily implemented in the template.###

2025-11-09

How to convert a string to uppercase, lowercase, or title case?

In website content operation, the way content is presented often determines the overall impression of users on the website.Whether it is to maintain brand style consistency or to improve the readability of text, flexible case conversion of strings is a basic and important operation.AnQiCMS (AnQiCMS) understands this, and it has built-in simple and easy-to-use filters (Filters) in its powerful template engine, allowing you to easily perform string transformations such as uppercase, lowercase, and capitalization without complex programming.We will introduce in detail how to use the Anqi CMS template

2025-11-09