The `safe` filter in Anqi CMS template, when and why to use it to cancel the default escaping of HTML content?

Calendar 👁️ 82

Mastering AnQi CMS template insafeFilter: Unlocking the correct rendering and security boundaries of HTML content

AnQiCMS as an enterprise-level content management system based on Go language, has adopted the syntax similar to Django template engine in template design, which provides great flexibility for content display.When building website content, we often need to display the information edited on the back-end on the front-end page.However, during this process, a mechanism named 'HTML escaping' quietly plays a crucial role in ensuring website security.safeThe filter used to manage its behavior is a skill that every AnQiCMS user, especially when dealing with rich text content, needs to master.

Understanding HTML Escaping: The Safe Default Barrier

What is HTML escaping, simply put, is to convert some special characters in HTML (such as</>/&/"/') is converted to their corresponding HTML entities (such as&lt;/&gt;/&amp;/&quot;/&#39;)。AnQi CMS template engine defaults to escaping all data output from the backend to the frontend. Why do this? The core reason is thatPrevent Cross-Site Scripting (XSS).

Imagine if a malicious user entered a piece of<script>alert('你被攻击了');</script>Such code, while the system did not escape it, this code will be executed in the browsers of other users accessing the page.This could lead to serious consequences such as user information being stolen and page content being tampered with.AnQiCMS adheres to the philosophy of 'making all websites in the world safe', the default HTML escaping is exactly to treat these potential malicious scripts as ordinary text, so that they can be displayed safely in the browser instead of being executed.This is like putting on a 'protection suit' for the data, ensuring the safety of the content output.

safeFilter: The Bridge of Trust and Presentation

However, in actual content operation, we often encounter such situations: we hope that the HTML content input in the background editor can be displayed as it is on the front end, such as a beautifully typeset article, an introduction containing pictures, or a table with special style design.In this case, the default HTML escaping mechanism may actually do more harm than good, displaying our carefully formatted HTML code as raw text strings, causing layout chaos on the page.

At this time,safeThe filter comes into play.Its role is to explicitly tell the AnQiCMS template engine: 'I know this content is safe, please do not HTML encode it, output it directly as HTML code.'|safeWe can cancel the default escaping behavior, allowing the browser to correctly parse and render the HTML content we expect.

For example, in the AnQi CMS template, when we want to display the full text of an article, if the content of the text is edited by a rich text editor, we usually use it like this:

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

Here{{articleContent|safe}}EnsuredarticleContentHTML code contained in variables such as<b>/<img>/<p>etc

Related articles

How to enable or disable automatic escaping for specific code blocks in templates using the `autoescape` tag?

In AnQi CMS template development, how to balance flexibility and security when handling dynamic content is an important consideration.Amongst, the HTML automatic escaping mechanism (`autoescape`) plays a key role, which is aimed at preventing cross-site scripting (XSS) attacks, while also allowing us to display native HTML content when needed.Understand and master the usage of the `autoescape` tag, which will make your template creation more skillful.Understanding automatic escaping

2025-11-08

What security escaping features do the `escape`, `e`, and `escapejs` filters provide when outputting HTML or JavaScript code?

It is crucial for the safety of output content in website operations.Especially when displaying user-submitted data or information obtained from external sources, if not properly processed, websites are easily vulnerable to cross-site scripting attacks (XSS) and other threats.AnQiCMS as a content management system that focuses on security, provides a powerful security escaping mechanism at the template rendering level, among which the `escape`, `e`, and `escapejs` filters are the key tools to ensure output security.AnQiCMS's template engine draws inspiration from Django

2025-11-08

How can the `dump` filter be used in the template development process for debugging and viewing the structure and value of variables?

During the development of Anqi CMS templates, we often need to handle various dynamic data.This data may come from a database, system configuration, or user input, and it is passed to the template as variables for display.However, sometimes the results displayed by the page are not what we expect-a field may be empty, the data format is incorrect, or the elements contained in a set may not be as expected.In this case, efficiently "viewing" the internal structure, type, and specific values of variables becomes the key to troubleshooting and speeding up development progress

2025-11-08

How does the `divisibleby` filter determine if a number is divisible by another number, and what conditions are commonly used?

The template engine of AnQiCMS provides a powerful and flexible tool for the dynamic display of website content.Amongst them, the `divisibleby` filter is a small but effective feature that enhances the logic of templates, mainly used to determine if a number can be evenly divided by another number.Understand and make good use of this filter, which can help us achieve more intelligent effects in content presentation and page layout.### `divisibleby` filter's core function

2025-11-08

How does the `fields` filter split a single line string into an array of strings?

During the template development process of AnQi CMS, flexibly handling and displaying data is the key to improving website functionality and user experience.Sometimes, the content we receive from the backend may be a single line string containing multiple pieces of information, such as a list of keywords, product tags, or a set of feature descriptions.If we need to treat this information as separate elements for processing or display, for example, as a list, we would need a method to split this single string into multiple independent items.At this time, the `fields` filter provided by the Anqi CMS template engine can come into play.

2025-11-08

How do the `first` and `last` filters get the first or last element of a string or array?

In AnQi CMS template development, in order to display content more efficiently and flexibly, we often use various filters to process the data.These filters are like various tools in a toolbox that can help us quickly format, cut, or extract data.Today, let's talk about two very practical and intuitive filters: `first` and `last`, and how they help us easily obtain the first or last element of a string or array.### 1. Getting to know the `first` and `last` filters Imagine that

2025-11-08

How to precisely control the decimal place display of floating-point numbers using the `floatformat` filter, including positive and negative digit settings?

In website content operations, accurately displaying numbers, especially floating-point numbers, is often a key factor affecting user experience and data professionalism.It is crucial to ensure that numbers are presented consistently and legibly in product prices, statistical data, or scientific reports.AnQiCMS (AnQiCMS) is well aware of this need, providing a powerful `floatformat` filter in the template engine to allow content creators to flexibly control the display precision of floating-point numbers.### `floatformat` filter

2025-11-08

How does the `get_digit` filter retrieve a digit from a number at a specified position?

In the AnQi CMS template world, flexible handling and displaying data is a key link in content operation.When faced with the need to accurately extract a specific digit from a sequence of numbers, the `get_digit` filter is a very practical tool.It can help us achieve some detailed display requirements, such as grouping or highlighting based on the specific position of numbers. ### Core Function and Usage The main function of the `get_digit` filter, as the name implies, is to obtain a single digit from a number at a specific position.

2025-11-08