How to safely display rich text content containing HTML tags on the front-end page?

Calendar 👁️ 94

In website operation, we often need to display rich text content containing images, links, bold text, and other rich formats, which is also known as rich text content.But it is a practical and cautious task to safely display these rich texts with HTML tags on the front-end page.AnQi CMS as an efficient content management system fully considers this point and provides us with a clear solution.

Understand rich text content in Anqi CMS

When we write articles, product descriptions, or single-page content in the Anqi CMS backend editor, the system provides an intuitive rich text editor.You can easily insert images, set text styles (such as bold, italic), create lists, and even embed videos or code blocks.These operations look normal in the background because the editor is responsible for converting your formatting intentions into standard HTML tags.

Moreover, Anqi CMS also supports Markdown editors, which means you can choose to write using a more concise Markdown syntax, and the system will automatically convert it to HTML when the content is published.No matter which editing method is used, the content stored in the database will ultimately be a string containing HTML tags.

The default security mechanism: HTML automatic escaping

When displaying content on the front end of a website, security is always the primary consideration.To prevent potential cross-site scripting (XSS) attacks, the Anqi CMS template engine (which uses a syntax similar to Django's Pongo2 template engine) performs a very important security operation by default when processing content read from the database: HTML automatic escaping.

In simple terms, this means that if you output rich text content as plain text on a page, such as:

<div>{{ archive.Content }}</div>

Then, like<script>alert('XSS')</script>Such HTML tags are not executed by the browser, but are escaped into&lt;script&gt;alert('XSS')&lt;/script&gt;Display in plain text. This default behavior greatly reduces the risk of websites being attacked by malicious code, ensuring user browsing safety.

Use to safely display rich text: |safeFilter

However, for the rich text content we create, which we hope the browser can render correctly with styles and structure, this default automatic escaping is no longer what we want. For example, the main text of an article, the description of a product detail page, we hope it can display in bold, images, and other effects, rather than a heap of&lt;p&gt;/&lt;img&gt;The original label.

This is when we need to explicitly tell the AnQi CMS template engine: 'This content has been reviewed by me, I believe it is safe HTML, please do not escape it, and render it directly.' The method to achieve this is to use|safefilter.

For example, assuming the article content is stored inarchive.Contentfield, you need to call it like this in the template:

<div>
    {{- archive.Content|safe }}
</div>

Or if it is a single page content, it may use:

<div>
    {{- pageContent|safe }}
</div>

Here|safeThe filter will cancel the default HTML escaping, allowing the browser to recognize and render HTML tags within the content, thereby correctly displaying rich text effects.

Markdown content rendering control

If you have enabled the Markdown editor in the Anqi CMS backend and the content is written in Markdown, then when displayed on the front end, in addition to using|safeFilter outside,archiveDetail/pageDetailsuch as tags,ContentThe field also supports arenderParameter.

In the content settings, you can choose whether to automatically convert Markdown content to HTML. If you want to fine-tune the conversion behavior or ensure that the content renders correctly even if the automatic conversion is turned off, you can explicitly specify it when calling itrender=true:

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

This, the system will first convert Markdown content to HTML, and then through|safeThe filter ensures it is rendered by the browser. If you do not want to perform Markdown conversion, you can setrender=false.

AnQi CMS even supports integrating third-party plugins such as MathJax and Mermaid to display mathematical formulas and flowcharts. These advanced features usually require adding specific JS/CSS references to your template file (such asbase.htmlThe header, and the same needs to be ensured when outputting relevant content|safeThe correct use of filters, as well as the correct display of Markdown contentrender.

Security practice recommendations

Use|safeThe filter is convenient, but always remember the security implications behind it.It is equivalent to you making a safety guarantee for this HTML content.Therefore, the following suggestions can help you better maintain website security when using rich text content:

  1. Always use prudently|safe:Use it only when you are sure of the reliability of the content source and that it does not contain malicious scripts.
  2. Strengthen backend content review:AnQi CMS provides sensitive word filtering, anti-crawling interference code and other security mechanisms.Combine manual review to ensure that the rich text content submitted by users or content contributors does not contain dangerous HTML or JavaScript code.Any content from an untrusted source should be strictly sanitized on the backend before storage and display.
  3. Regularly update the system:Keep your secure CMS system and all plugins up to date to obtain the latest security patches and protective measures.

In summary, Anqi CMS provides strong security protection by default when handling the display of rich text content. When we want to show the richness of the content, we can do so by understanding and using it correctly|safeFilter, and combine as necessaryrenderWith parameters, we can provide visitors with beautiful and functional page content while ensuring the safety of the website.


Frequently Asked Questions (FAQ)

1. Why does the content of the article I published, which clearly has images and links, only display plain text on the front page, and the HTML tags are all exposed?

This is usually because you have not used rich text content in the template|safeFilter. The Anqi CMS template engine defaults to escaping all HTML tags to prevent XSS attacks.If you want images and links to display correctly, you need to set the corresponding rich text variables (for example,{{ archive.Content }}or{{ pageContent }}) Change to{{ archive.Content|safe }}or{{ pageContent|safe }}.

2. If I use|safefilter, is my website completely safe?

No.|safeThe filter simply tells the template engine that this content is safe and should be rendered directly.It does not perform security checks or cleaning on the content itself. Therefore, if the rich text content stored in the database itself contains malicious scripts, using|safeThe filter ends up being executed, leading to an XSS attack.Ensure true security starts from the source: Make sure that the content input on the backend is strictly verified and filtered to prevent malicious HTML or JavaScript code from being saved to the database.AnQi CMS provides some built-in security features, such as sensitive word filtering, but combining manual review and security awareness is crucial.

How to correctly display Markdown format content in Anqi CMS?

If you have enabled the Markdown editor in the background and written Markdown content, in addition to using|safeOutside the filter, you also need to make sure that the Markdown content is correctly converted to HTML. When usingarchiveDetail/pageDetailto getContentfield, you can try to addrender=trueparameters, for example{% archiveDetail articleContent with name="Content" render=true %}{{articleContent|safe}}This will indicate that the system will convert Markdown to HTML before displaying.In most cases, if the background is set to automatically convert Markdown to HTML, this parameter may not be mandatory, but specifying it explicitly can provide stronger control.

Related articles

How to use logical tags like `if` and `for` in templates to control conditional and repeated display of content?

In website content management, displaying dynamic and diverse content is the key to improving user experience and website attractiveness.AnQiCMS (AnQiCMS) provides a powerful template system, drawing inspiration from the Django template engine syntax, allowing you to easily control the display of content through logical tags.Among them, the `if` tag is used for conditional judgment, while the `for` tag is used for loop iteration, they are the foundation for building flexible and variable web pages.

2025-11-07

How to truncate the title or abstract of an article and display it with an ellipsis?

In website content operation, titles and summaries are important elements to attract visitors to click and learn more details.Especially on list pages, recommendation positions, or the homepage, the limited space requires us to refine and cut these contents.To maintain the neatness and aesthetics of the page while fully conveying the core information, it is a common optimization strategy to truncate overly long article titles or summaries and end them with an ellipsis "..."}AnQiCMS (AnQi CMS) provided us with flexible and powerful tools to meet this requirement.

2025-11-07

How to display the custom parameter list in the article content on the front page?

AnQiCMS as an efficient and customizable content management system provides powerful content model features, allowing us to add various custom fields to articles according to different business needs.These custom parameters not only make the article content more rich and structured, but also allow for personalized display on the front page, greatly enhancing the flexibility and user experience of the website.Today, let's delve into how to flexibly display these custom parameter lists in the AnQiCMS front page.

2025-11-07

How to use the `pagination` tag to build standard pagination navigation in the template?

When the content of a website becomes richer, a clear and efficient pagination navigation system is crucial for improving user experience.The AnQiCMS template system provides a powerful `pagination` tag that allows us to easily build standard, beautiful pagination navigation.Next, we will delve deeper into how to use this tag in templates.

2025-11-07

How to dynamically display the current year or current time in a template?

In website operation, we often need to dynamically display the current year or time, such as displaying the latest copyright year at the bottom of the website, or marking the generation time of the page accurately in articles.AnqiCMS provides flexible and powerful template functions, making these operations very simple.Next, we will discuss how to implement this requirement in the AnqiCMS template.

2025-11-07

How to reflect the high concurrency characteristics of the Go language in the front-end page loading speed?

In today's fast-paced online world, website loading speed has become a core factor in user experience and search engine rankings.Nobody likes to wait for a slow page, and search engines tend to rank websites that respond quickly higher.How can you make your website lightning fast?For users of AnQiCMS, the answer is hidden in its powerful Go language underlying architecture, especially the high concurrency features that Go language boasts.

2025-11-07

How to reference and display external static resources (such as CSS, JS) in the template?

Referencing and displaying external static resources in AnQiCMS templates, such as CSS style sheets and JavaScript scripts, is a key step in building a functional and visually appealing website.Understand the working principle and recommended practices, which can make our website development work more efficient and flexible.

2025-11-07

How to define and use variables for content display in AnQiCMS templates?

AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and flexible template mechanism, making content presentation both powerful and intuitive.In website content operation, we often need to display dynamic information, such as article titles, product prices, contact information, etc., which is inseparable from defining and using variables in templates.Understanding how to flexibly use variables in the AnQiCMS template is the key to building and maintaining an efficient, personalized website.The AnQi CMS template engine has borrowed the syntax from Django, making it very easy to understand the definition and reference of variables. Essentially

2025-11-07