How to safely output HTML code in the AnQiCMS template to prevent escaping?

Calendar 👁️ 71

In website operation, we often encounter situations where we need to output content containing HTML code.For example, images, links in the main text, or customized layout styles, etc.When you output this content in the AnQiCMS template, you may find that it is not displayed as interactive elements as expected, but rather in the form of raw HTML code, such as<p>这是一个段落</p>Became&lt;p&gt;这是一个段落&lt;/p&gt;This is the core issue we are discussing today: how to safely output HTML code containing content in the AnQiCMS template without escaping.

The default behavior of AnQiCMS template content

AnQiCMS template engine places the security of the website at a very important position during design.For the prevention of cross-site scripting attacks (XSS), it defaults to escaping all content output in the template.This means, any character that is recognized as a special character in HTML tags, such as</>/&/"/'will be automatically converted to their corresponding HTML entities.

For example, a content that originally is<script>alert('Hello!')</script>will be changed to after default escaping.&lt;script&gt;alert(&#39;Hello!&#39;)&lt;/script&gt;This way, the browser will not treat it as executable script but display it as plain text, effectively preventing the execution of malicious code.This is a very robust security strategy.

However, for the legitimate content that we do indeed hope to present in HTML format, such as carefully formatted content in a backend rich text editor, this default escaping behavior requires additional processing to release this 'safety restraint'.

Solution one: usesafeUnescape default filter escape

When you know for sure that the HTML code contained in a variable is safe and trustworthy, and you want the browser to parse and render it directly, AnQiCMS providessafeThe filter is used to handle this situation. Its purpose is to inform the template engine: "This content is safe, please do not escape it as HTML, and output it directly as is!"}

  • Usage method:safeThe use of the filter is very simple and intuitive. You just need to add it after the variable that needs to be unescaped.|safeFor example, if you have a variable namedarchiveContentThe variable stores the main body HTML of the article, you can output it like this:

    <div>{{ archiveContent|safe }}</div>
    

    here,archiveContentAll HTML tags will be parsed and rendered normally by the browser.

  • Safety warning: Although safeThe filter provides convenience, but its use is not without risk. Once you have used|safeit means that you guarantee the safety of this content to the template engine. IfarchiveContentAccidentally included unreviewed malicious scripts, which, if executed by the browser, could lead to severe XSS attacks, potentially leaking user information or damaging website functionality. Therefore, when usingsafeBefore the filter,Always confirm that the source of content is absolutely reliable and secure, or that the content itself has been strictly filtered and verified.

Solution two: usingrenderThe filter processes Markdown content

In AnQiCMS, you may take advantage of its powerful content model features, writing articles or page content through the Markdown editor. Markdown is a lightweight markup language that uses simple symbols such as#Indicates the title,*Italic can be quickly formatted. When this Markdown content needs to be displayed as formatted HTML on the front end, AnQiCMS providesrenderThe filter completes this transformation task.

  • Usage method:renderThe filter will process Markdown text and convert it to the corresponding HTML structure. Since the result is HTML, it usually also needs to be accompanied bysafeThe filter is used together to ensure that the converted HTML tags can be correctly parsed by the browser, rather than being escaped again.
    • For example, ifcategoryContentVariables store the Markdown format of category introductions, and you can output it like this:
      
      <div>{{ categoryContent|render|safe }}</div>
      
    • In this example,|renderThe filter first processes the Markdown text and converts it to HTML, then|safeThe filter ensures that these HTML tags can be parsed and displayed normally by the browser.It is worth noting that some core fields in the AnQiCMS backend content management (such as "document content", "category content", etc.), when you enable the Markdown editor in the backend, the system may automatically convert Markdown to HTML before the content enters the template.But when handling custom fields or when explicit rendering behavior control is needed, use it explicitly in the templaterenderThe filter is still the recommended practice.

Points to note in practice

When outputting content containing HTML code in the AnQiCMS template, be sure to pay attention to the following points to ensure the security and stability of the website:

  1. Trust the sourceUse only content sources you trust completelysafeFilter. For example, content edited and published by the website administrator through a rich text editor in the background, as it usually goes through the built-in security filtering of the system, it can be considered safe.
  2. Handle user input carefully: For any unprocessed HTML content submitted from the user's front-end (such as comments, messages, etc.), it must not be directly passed through|safeFilter output. This is the most common XSS attack entry point.If it is indeed necessary to display user-submitted content containing HTML, it must be strictly cleaned and filtered on the server side, for example, by using an HTML whitelist mechanism, only allowing safe HTML tags and attributes to be displayed, and removing all potential malicious scripts

Related articles

How does AnQiCMS's Markdown editor handle special characters and formatting?

AnQiCMS provides a powerful Markdown editor that not only simplifies content creation but also allows for flexible handling of special characters and formats to meet the needs of different content displays.For users, understanding these processing methods can better utilize the editor's potential to output high-quality page content. ### Enable Markdown Editor Firstly, to use the Markdown editor, you need to make simple settings in the Anqi CMS backend.

2025-11-08

`trimLeft` and `trimRight` filters are used to remove which parts of the string in AnQiCMS templates??

In AnQi CMS template development, we often need to make detailed control and adjustment of the content displayed on the page.The filter is a set of powerful tools provided by the Anqi CMS template engine that helps us easily modify the output format and content of variables.Today, we will focus on the `trimLeft` and `trimRight` filters, which play a crucial role in handling excess content at the beginning and end of strings.In order to better understand them, we will also briefly mention their 'sibling' filter `trim`. Understand

2025-11-08

How to remove extra spaces or specific characters from the beginning and end of a string in AnQiCMS template?

In AnQiCMS template development, fine-grained string processing is a key factor in improving the quality of website content display.We often encounter such a situation: when data is read from the database, user submissions, or text imported from external sources, the beginning or end may carry spaces that we do not want, or even specific punctuation marks or prefix/suffix characters.These extra characters not only affect the visual cleanliness of the page, but may also cause unnecessary trouble to front-end layout, data validation, and even search engine optimization (SEO).AnQiCMS uses similar

2025-11-08

In the AnQi CMS content settings, how does the 'Automatically filter external links' feature affect the published content?

In Anqi CMS content settings, there is a feature called 'Whether to automatically filter external links', which has a direct and profound impact on the publication of website content.This seemingly simple switch actually affects many aspects such as the website's search engine optimization (SEO), user experience, and the credibility of the content. To find this feature, you need to enter the Anqi CMS backend management interface, and you can see it under the "Content Settings" menu in the "Backend Settings".It provides two main options, each leading to different processing results after content is published.###

2025-11-08

What special characters are the `escape` and `escapejs` filters used to escape in the AnQiCMS template?

In AnQiCMS template development, to ensure the correct display of content and website security, we often use some built-in filters to handle special characters.Among them, the `escape` and `escapejs` filters are two very important tools, each serving different scenarios, and the types of special characters that need to be escaped are also different.

2025-11-08

How can keyword library management in AnQiCMS's SEO tool help optimize website content?

In the increasingly fierce online competition, optimizing website content is the key to attracting and retaining users.For operators who want to stand out in search engines and continuously obtain high-quality traffic, a set of efficient SEO tools is undoubtedly a powerful assistant.AnQiCMS as an enterprise-level content management system provides many practical functions in SEO, among which keyword library management is the core tool for us to optimize website content and improve search engine performance. Imagine your website as a library, and keywords are like indexes guiding readers to the books you provide.

2025-11-08

How to add and manage multiple Tag tags in AnQiCMS?

In a content management system, effectively organizing and presenting information is the key to improving user experience and search engine performance.AnQiCMS provides a flexible Tag feature, which allows for more refined categorization of content, making it easier for users to quickly find content of interest and also provides strong support for website SEO optimization.This article will provide a detailed introduction on how to add and manage multiple Tag tags in AnQiCMS, and discuss its practical value in content operation.

2025-11-08

How to call the Tag list of the specified document in AnQiCMS template?

In AnQiCMS, tagging content (Tag) is an effective way to enhance content organization and user experience.These tags not only help search engines better understand the page theme, but also guide users to discover more related content.When you need to display the Tag list associated with a specific document in a template, AnQiCMS provides an intuitive and powerful `tagList` tag to make this operation very simple.

2025-11-08