How to use the 'safe output' mechanism of AnQiCMS template to avoid HTML content from being escaped?

Calendar 👁️ 69

In website content management, we often need to display various text information on the page, including HTML content with specific formats or interactive effects.AnQiCMS (AnQiCMS) is a modern content management system that, when handling template rendering, defaults to taking an important security measure: automatically escaping HTML content.This mechanism is designed to prevent cross-site scripting attacks (XSS) and ensure the security of the website.However, in certain specific scenarios, such as when we want to display formatted content edited by a rich text editor, custom HTML code snippets, or even system-built copyright information, we do not want these contents to be escaped but to be presented in their original HTML form to the user.At this point, it is particularly important to understand and master the 'secure output' mechanism of AnQiCMS templates.

The security mechanism of AnQiCMS template: default escaping

The design philosophy of AnQiCMS template engine is similar to many modern web frameworks, it defaults to escaping all through{{变量}}The content output in this manner is treated as plain text. This means that if your content variable contains something like<script>/<img>or such HTML tags,&/</>Such special characters, the template engine will automatically convert them to HTML entities (for example<Will become&lt;,&Will become&amp;This is the default behavior that acts as the first line of defense for website security, effectively preventing malicious code (such as injected JavaScript) from executing in the user's browser, thereby greatly reducing the risk of XSS attacks.

For example, if you enter content in the background<p>这是一段<em>加粗</em>的文字。</p>That is what you might see on the front-end page by default&lt;p&gt;这是一段&lt;em&gt;加粗&lt;/em&gt;的文字。&lt;/p&gt;This is not a formatted text. This processing method ensures safety, but when it comes to displaying HTML formatted content, we need to actively inform the template engine to stop escaping.

The core of achieving 'safe output':|safeFilter

When you know for sure that a piece of content is safe and contains HTML that needs to be parsed, AnQiCMS provides|safea filter to indicate to the template engine not to escape the content.|safeThe filter's role is to tell the system: 'I have checked this content, it is safe, please parse and output it directly as HTML.'

The way it is used is very intuitive, just add it after the variable name you want to unescape|safeIt can be. For example, in AnQiCMS, the rich text editing area of document content, category content, or single page content will usually generate HTML code.If you want this content to be displayed in the correct format, you might use it like this:

{# 文档详情页显示文章内容,确保富文本格式正确展示 #}
<div>
    {% archiveDetail articleContent with name="Content" %}
    {{ articleContent|safe }}
</div>

{# 分类详情页显示分类描述或内容,若其中包含HTML #}
<div>
    {% categoryDetail categoryDescription with name="Description" %}{{ categoryDescription|safe }}
</div>

{# 系统版权信息可能包含链接等HTML标签 #}
<div>{% system siteCopyright with name="SiteCopyright" %}{{ siteCopyright|safe }}</div>

Please note that using|safeThe filter means that you will be responsible for the security of the content. If it is|safeThe content being processed contains malicious scripts that will be executed in the user's browser. Therefore, it should be used at any time|safeEnsure that the content source is reliable, or has been strictly reviewed and filtered.

###

Related articles

How to ensure that the URL strings in the content are automatically converted into clickable hyperlinks?

In daily website content operations, we often need to include various links in articles or descriptions, whether they point to other pages within the site or external references.One of the key aspects of user experience is that these URL strings can be automatically turned into clickable hyperlinks, rather than a series of cold texts.AnQiCMS (AnQiCMS) fully understands this and provides a variety of flexible and efficient methods to help us ensure that URLs in the content can be intelligently converted into clickable hyperlinks.Next, we will discuss how to achieve this goal in AnQiCMS

2025-11-09

How to automatically convert multiline text content to HTML format with <p> or <br> tags for display?

In website content display, we often encounter such needs: multi-line text edited in the background can be automatically converted into a format with HTML paragraph tags `<p>` or line break tags `<br>` on the front-end page, rather than simply being compressed into a long string of text.AnQiCMS provides a flexible and powerful method to solve this problem, whether it is through built-in editor features or by using template filters for precise control, it can be easily realized.### One, use the content editor to achieve automatic conversion When you go through AnQiCMS

2025-11-09

How to dynamically display copyright information and the current year at the bottom of a website or other areas?

In website operations, keeping information up to date is an important part of maintaining a professional image, and the copyright year at the bottom of the website is often overlooked. Manually modifying it every year is not only cumbersome but also prone to omission.Luckyly, using Anqi CMS, we can easily implement the dynamic display of copyright information, so that the information at the bottom of the website is always up-to-date.Why is dynamic copyright information so important?Imagine a user visiting a website and seeing the copyright year prominently displayed at the bottom.This could make the website look outdated and may give people an impression of unprofessionalism.For content operators

2025-11-09

How to correctly render Markdown document content as HTML in a template?

Content creators often choose Markdown as their preferred tool because it can organize content in a concise and clear manner, while also easily achieving formatting effects.However, simply inputting content in Markdown format in the background is not enough to allow the website front end to display it correctly. We need some additional steps to ensure that the content is rendered correctly as HTML.This article will introduce in detail how to make Markdown content turn beautiful in AnQiCMS templates, presenting it to users in the form of HTML.### One

2025-11-09

How do user group management and VIP systems affect the access permissions and display of specific content?

How to allow different users to see different content on a website, even charging for some high-value content, is the key to enhancing the value of the website and achieving monetization.AnQiCMS provides powerful and flexible features in user group management and VIP system, helping us achieve refined content distribution and differentiated display. ### Understanding the Core Value of User Groups and VIP Systems In Anqi CMS, user group management is the foundation for building personalized content experiences.

2025-11-09

What methods does AnQiCMS provide for template developers to debug variable output and structure type?

When developing templates in Anqi CMS, understanding and debugging variable output and data structure types is the key to improving efficiency.Although the AnQiCMS template engine (similar to Django or Blade syntax) is intuitive and easy to use, mastering some debugging techniques can make you twice as effective when encountering problems.We usually understand the variables and data structures in the template through the following ways. ### 1.

2025-11-09

How to define and assign variables in a template to temporarily control the display logic of content?

In AnQi CMS template development, flexibly defining and assigning variables is the key to dynamic display of website content and personalized layout.By cleverly using variables, we can temporarily adjust the presentation of content, making the template not only able to carry basic data display but also able to present a rich and varied display logic according to specific conditions.This article will deeply explore how to define and assign variables in AnQiCMS templates, as well as their actual application in controlling the display logic of content.### The Foundation of Template Variables

2025-11-09

How to use the 'Remove logical tag line occupation' feature to clean up extra blank lines rendered by the template?

When using AnQi CMS to manage a website, we all hope that the page presented to the visitor is both beautiful and efficient.The page loading speed and the neatness of the source code not only affect the user experience, but also have a subtle but important impact on search engine optimization (SEO).You may have encountered such a situation in the process of template development: although the template code looks neat, there are always some unwanted blank lines in the rendered HTML source code.

2025-11-09