How to safely display rich text content containing HTML tags in AnQiCMS templates?

Calendar 👁️ 71

In website operation, we often need to display text content that includes rich formatting and interactive elements, which is what we commonly call rich text.This content may include bold, italic, links, images, and even tables with HTML tags.In AnQiCMS templates, how to ensure that these HTML tags can be rendered correctly while also considering the security of the website is a topic worth discussing.

Learn about the default security mechanism of AnQiCMS template

Firstly, we need to understand one of the core security designs of the AnQiCMS template system: by default, to prevent security vulnerabilities such as cross-site scripting attacks (XSS), the template automatically escapes HTML tags when outputting variables. This means that if you directly output rich text content containing HTML tags, such as the details of an article,{{ archive.Content }}put into the template, what you see will not be formatted content, but content with angle brackets<and>will be converted to&lt;and&gt;the original HTML code.

This default escaping behavior is an important aspect of website security. Imagine if a malicious user injected a segment into comments or article content.<script>alert('您被攻击了!')</script>And if the system does not process it and simply displays it, then all users visiting the page may be at risk.Therefore, this default handling method of AnQiCMS is to protect the website and its visitors.

UsesafeThe filter correctly renders HTML content.

When you confirm that certain rich text content is safe and trustworthy, and you want it to be displayed on the page in its original HTML format, AnQiCMS providessafeThe filter explicitly informs the template system that this content is safe and should not be HTML-escaped.

UsesafeThe filter is very simple, you just need to add it after the variable you need to output|safeIt can be. For example, when displaying the article detail page, the article content is usually rich text, and we need it to be displayed in formatted HTML:

{# 假设archive.Content包含了文章的HTML富文本内容 #}
<div class="article-content">
    {{ archive.Content|safe }}
</div>

By|safeThe browser will then convertarchive.Contentof<p>/<a>/<img>Tags are identified and rendered, thus presenting the format you expect. This is particularly important for content that needs to retain original formatting, such as articles, product descriptions, or custom pages.

Special handling of Markdown content andrenderParameter

AnQiCMS also supports Markdown editor, which brings great convenience to content creators.When you enable the Markdown editor in the "Global Settings" -> "Content Settings" in the background, and the content is saved in Markdown format, the template will have an additional step when processing these contents.

Regarding the Markdown-formatted content of AnQiCMS'sarchiveDetailorpageDetailetc. tags, in theContentfield, can be combined withrenderParameters to control the conversion of Markdown to HTML.

  • render=true: Clearly indicates that the template converts Markdown content to HTML.
  • render=falseThe template does not convert Markdown content, and outputs the original Markdown text directly.

Even if you have usedrender=trueConvert Markdown to HTML, the converted HTML content may still contain tags that need to be parsed. Therefore, to ensure that these converted HTML can be displayed correctly, you usually still need to combine|safefilter.

{# 假设archive.Content是Markdown格式,需要转换为HTML并安全显示 #}
<div class="article-markdown-content">
    {% archiveDetail articleMarkdownContent with name="Content" render=true %}
    {{ articleMarkdownContent|safe }}
</div>

Thus,render=trueWill first parse Markdown syntax (such as## 标题,**加粗**) into HTML tags (such as<h2>标题</h2>,<strong>加粗</strong>Then,|safea filter to ensure that these HTML tags can be rendered normally by the browser.

Deep considerations of security: when to usesafe?

AlthoughsafeFilters can solve HTML rendering problems, but the words 'safe' are not easy to talk about. UsesafeThis means you completely trust the source of the variable's content. Therefore, when usingsafeyou must carefully evaluate:

  1. Is the source content可信

    • credible sources:Website administrators or editors can directly edit and publish content in the background. AnQiCMS usually performs backend security filtering (such as sensitive word filtering) on this type of content to reduce XSS risks.
    • Untrusted source:Comments, messages, forum posts, and other content submitted by users. This content is highly likely to be maliciously injected with code. For such content,It is strongly recommended not to use directly|safeThe backend of AnQiCMS should have strict input filtering and output escaping mechanisms to handle submitted user data.Even if you need to display rich text submitted by users, you should ensure that the backend has performed sufficient filtering and sanitization.
  2. Is AnQiCMS's backend security in place?AnQiCMS project advantages include 'security mechanisms: including anti-capture interference code, content security management, sensitive word filtering, and other functions to ensure content security and compliance.'This indicates that the system has multiple considerations for content security.For content entered through the backend editor (not submitted directly by the user), it is usually processed once for security, reducing the risk of direct XSS attacks.But when displaying such content on the front end, combine|safeFilters mean you trust AnQiCMS's backend filtering is sufficient to handle it.

In short,safeThe filter is the key to displaying rich text content, but it also requires developers to take on the responsibility of content safety. Always ensure that only content from trusted sources and fully filtered on the backend is marked assafe.

###

Related articles

The role of the `count` filter in template data analysis for counting the occurrence of substrings?

In content operation, data analysis is an indispensable link.It can help us understand user behavior, evaluate content effectiveness, and guide future content strategy.AnQi CMS, with its flexible template engine syntax, provides us with powerful data processing capabilities, allowing us to perform some basic and practical data analysis directly at the template level.Among them, the `count` filter is like an efficient 'data detective' that helps us quickly understand the frequency of specific elements in the template data.Understand and make good use of this filter, it will greatly enhance our accuracy in content presentation and strategy formulation

2025-11-08

How to determine if a string contains a substring in AnQiCMS template?

In AnQiCMS daily operation and template design, we often need to decide how to display it based on the specific attributes of the content.For example, you may want to highlight articles that contain a certain keyword in the title, or adjust the style based on whether there is a specific phrase in the product description.How to determine whether a string contains another substring in a template has become a very practical skill.AnQiCMS's template system is based on the Django template engine syntax, providing a rich set of tags and filters to help us flexibly process data.for string inclusion judgment

2025-11-08

What are the benefits of directly defining an array (`list`) in the AnQiCMS template? How to define it?

In the AnQiCMS template, we often encounter some scenarios where we need to display some small, relatively fixed list data, such as the featured functions of a page, the advantages of a product series, or auxiliary navigation links, etc.The traditional method may require creating a special content model on the backend, then publishing several data items, and finally calling through template tags.But this seems a bit繁琐 for those data that do not change often and are limited in quantity.

2025-11-08

How does the `add` filter implement flexible connection of text content?

In the Anqi CMS template world, we often need to dynamically combine different content blocks or data, whether it is the sum of numbers or the concatenation of text.At this time, the `add` filter acts as a flexible bridge, helping us easily achieve content concatenation, making the website display more vivid and personalized. ### Deep Understanding of `add` Filter: The Bridge Between Text and Data The `add` filter is a very practical feature in the Anqi CMS template engine, whose core function is to perform addition of numbers and concatenation of strings.It lies in its intelligent processing style

2025-11-08

The `safe` filter: when to use, what potential security risks need to be paid special attention to?

During the template development process of Anqi CMS, the way content is displayed is flexible and diverse, and one of the very important filters is `safe`. Understanding the working principle of the `safe` filter, when to use it, and the security considerations it may bring, is crucial for building a website that is both functional and secure. ### The core function of the `safe` filter First, let's understand a basic security mechanism of the Anqi CMS template engine: the default automatic escaping.

2025-11-08

The role of `escape` and `escapejs` filters in preventing XSS attacks or handling special characters?

In website content operation, the display method and security of the content are equally important.AnQi CMS is an efficient content management system that provides comprehensive considerations for website security, among which the `escape` and `escapejs` filters are important tools for us to combat network attacks and ensure the correct display of content.Understanding their role can help us better manage and publish content.

2025-11-08

How to convert a numeric string to an integer or floating-point number type in AnQiCMS template?

In AnQiCMS template development, we often encounter situations where we need to handle various types of data.Sometimes, data obtained from a database or through user input, even if they represent numbers, may be treated as strings at the template layer.In this case, if direct mathematical operations or numerical comparisons are performed on these 'number strings', unexpected results or even errors may occur.Therefore, understanding how to convert a numeric string to an integer or floating-point number type in the AnQiCMS template is crucial for ensuring the accuracy of data processing and the correctness of logic

2025-11-08

`date` filter: How to format a GoLang `time.Time` type into a specific date string?

In AnQi CMS template development, flexibly displaying dates and times is an indispensable part of content presentation.You may often need to present date information in the system or content in a specific format, such as "YYYY-MM-DD" or "MM/DD HH:MM" and the like.At this point, the `date` filter has become your powerful assistant.

2025-11-08