How to correctly parse and display HTML content in the rich text editor in the AnQiCMS template?

Calendar 👁️ 69

Managing website content in AnQiCMS, the rich text editor is undoubtedly our powerful assistant for creating rich and beautiful pages.It allows us to easily add styles, images, links, and even tables, making the content bid farewell to the dull plain text.However, ensuring that these meticulously edited rich text contents are accurately displayed in the website front-end template is a key skill that every AnQiCMS user needs to master.

When we edit content through the rich text editor or Markdown editor on the AnQiCMS backend, this content will eventually be stored in the database in the form of HTML code. For example, a piece of text you input may become bold.<strong>加粗文字</strong>, the inserted image will be<img src="图片地址" alt="图片描述">.

AnQiCMS uses a template engine similar to Django, one of its design philosophies being security. To prevent security risks such as cross-site scripting attacks (XSS), the template engine, by default, will filter all data passed through{{ 变量 }}The output content is escaped as HTML. This means that if you directly output a variable containing HTML tags, for example{{ archive.Content }}, the ones you add in the editor<strong>The tag is not recognized by the browser as bold, but is displayed directly as&lt;strong&gt;加粗文字&lt;/strong&gt;such plain text strings. It is obvious that this is not the effect we expect.

Core solution: use|safeFilter

In order for the browser to correctly parse and render the HTML content in the rich text editor, we need to explicitly tell the AnQiCMS template engine that this content is safe and should not be escaped. Please output it directly as HTML code. The key to achieving this is to use|safefilter.

The method of use is very intuitive, just add after the variable that needs to be parsed into HTML|safeThe filter can be used. Typically, the content of a rich text editor is stored in a document (archive) or a single page (page) ofContentin the field.

For example, on your article detail page (usuallyarchive/detail.htmlorarchive_detail.htmlIn the text, when displaying the article content, you can call it like this:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="article-content">
        {%- archiveDetail articleContent with name="Content" %}
        {{ articleContent|safe }}
    </div>
</article>

In this example,{% archiveDetail articleContent with name="Content" %}The tag is used to get the article'sContentfield content and assign it toarticleContentthe variable. Then,{{ articleContent|safe }}This line of code ensuresarticleContentAll HTML tags in the text, such as paragraphs<p>, image<img>, links<a>can be parsed and displayed normally by the browser, rather than as plain text output.

Similarly, if you need to display rich text content in a single page, the calling method is also similar:

<section>
    <h2>{% pageDetail with name="Title" %}</h2>
    <div class="page-content">
        {%- pageDetail pageContent with name="Content" %}
        {{ pageContent|safe }}
    </div>
</section>

Handling the special characteristics of Markdown content

The strength of AnQiCMS lies in the fact that it also supports Markdown editors.Markdown content is different from traditional rich text HTML; it uses a concise markup syntax to write and ultimately needs to be converted to HTML to be displayed on the web.

AnQiCMS'archiveDetailandpageDetailtags are being retrievedContentWhen a field is, if the content settings of the system backend have enabled the Markdown editor, it will intelligently automatically convert Markdown content to HTML.This saves us the trouble of manual conversion.

However, if you need more fine-grained control, such as forcing Markdown to HTML conversion even when the Markdown editor is not enabled, or conversely, preventing it from rendering (possibly because you want to use a JavaScript library for secondary processing), you canarchiveDetailorpageDetailthe tag withrenderParameter.

  • Force rendering Markdown to HTML:

    {%- archiveDetail articleContent with name="Content" render=true %}
    {{ articleContent|safe }}
    

    render=trueWill force the AnQiCMS backend toContentThe Markdown content of the field is converted to HTML, regardless of whether the Markdown editor is enabled.

  • Prevent Markdown rendering:

    {%- archiveDetail articleContent with name="Content" render=false %}
    {{ articleContent|safe }}
    

    render=falseIt will prevent the AnQiCMS backend from converting Markdown content. In this case, if you want the content to be presented in HTML, thenarticleContentThe variable must be pure HTML, or you can use a JavaScript library to parse Markdown on the front end.

It should be noted that if Markdown content includes advanced elements such as mathematical formulas or flowcharts, AnQiCMS may mark them as specific code blocks, but their final visual presentation often still depends on the front-end to load additional JavaScript libraries (such as MathJax and Mermaid) to support, as followshelp-markdown.mdThe configuration method mentioned in the document.

Advanced control and **practice**

except|safeFilter, AnQiCMS also provides{% autoescape off %}and{% autoescape on %}The tag controls the automatic escaping behavior inside code blocks. If you need to disable HTML escaping for an entire block of content, useautoescapetag to avoid adding it after each variable|safe:

{% autoescape off %}
    <div class="some-html-block">
        {{ var1 }}
        {{ var2 }}
        <p>{{ var3 }}</p>
    </div>
{% endautoescape %}

In this code block,var1/var2andvar3HTML escaping will no longer be performed. However, please use it with caution.autoescape offIt can affect the entire block, and if it contains data from unreliable sources, it may introduce security vulnerabilities.

Consideration of security:Though|safeIt brings great convenience, but also please remember to use:|safeIt means you trust that this content is safe and does not contain malicious scripts.This is usually safe for content entirely created by you or trusted editors in the background.But if your website allows users to submit content containing HTML (such as comments or messages), and you do not want to strictly filter or review the content submitted by these users, then directly use|safeMay pose XSS risk. In this case, you may need to consider additional security handling for user submitted content, such as usingstriptagsOr a custom HTML sanitization function.

Content truncation:Sometimes we may need to display an abstract of rich text content rather than the entire content.Directly truncating an HTML string may destroy the HTML structure, causing the page to display abnormally.AnQiCMS providedtruncatechars_htmlandtruncatewords_htmlFilters that can intelligently maintain the integrity of HTML structure while extracting HTML content, are very suitable for generating rich text summaries.

{# 截取HTML内容到指定字符长度,并保持HTML结构 #}
<p>{{ articleContent|truncatechars_html:150|safe }}</p>

Template file encoding:Ensure that your template files use UTF-8 encoding uniformly to avoid issues with Chinese content displaying as garbled.

Summary

In AnQiCMS template, correctly parsing and displaying HTML content in the rich text editor lies in understanding the default HTML escaping mechanism of the template engine and using it well|safeThe filter explicitly declares the security of the content. For Markdown content, the system provides intelligent automatic conversion, and you can also do it byrenderParameters for finer control. While enjoying the flexibility of content display brought by AnQiCMS, always keep in mind the importance of content security, which is an indispensable part of building a robust website.


Frequently Asked Questions (FAQ)

1. Why is the image and bold text I added in the rich text editor displayed as plain text on the front end?<img src="...">and<strong>...</strong>What is this plain text?

This is because the AnQiCMS template engine, for security reasons, defaults to escaping all output variable content. It will escape HTML tags.<and>Symbols are converted separately&lt;and&gt;This causes the browser to be unable to recognize it as an HTML tag for rendering. To solve this problem, you need to add after the variable that outputs rich text content.|safeFilter, for example `{{ archive.Content|

Related articles

How to display the related Tag tag list of articles or products on the AnQiCMS detail page?

How can we make users find the content they are interested in more efficiently while also improving the visibility of the content in website operations, which is a problem we often think about.The detail page of an article or product, in addition to the core content, providing a list of related Tag tags is undoubtedly a good method to achieve this goal.AnQiCMS as a flexible and efficient content management system provides an intuitive and powerful Tag label feature, allowing you to easily display these related information on the detail page.### Learn about AnQiCMS Tag feature In AnQiCMS backend

2025-11-09

What content models does AnQiCMS support, and how can they be flexibly displayed on the front end?

In AnQi CMS, content is not just simple articles or products, it is a dynamic ecosystem that allows users to freely define and display various types of information according to their actual business needs.This high degree of flexibility is due to its powerful content model support and the limitless possibilities brought by the front-end template engine. ### Flexible and diverse content model: the skeleton of information One of the core advantages of Anqi CMS is its highly flexible content model.It is not preset with a few fixed content types to limit your choices, but provides a powerful mechanism that allows users to build like building blocks

2025-11-09

How to customize the display of the publication time and page views of articles using AnQiCMS template tags?

In the display of website content, the publication time and the number of views of the article are important elements in conveying information to readers and enhancing the value of the content.A clear publication time allows readers to understand the timeliness or timeliness of the content, while the number of views can reflect the popularity of the content.AnQiCMS with its flexible template mechanism allows us to easily customize the display of this key information to adapt to the design styles and user needs of different websites.

2025-11-09

How to get and display the record number and copyright information of the website in the AnQiCMS template?

In the operation of the website, the filing number (ICP) and copyright information are essential elements for building user trust and compliance with laws and regulations.It is crucial to clearly display this information for websites built using AnQiCMS, whether it is a corporate website or a personal blog.Luckily, AnQiCMS provides a very intuitive and flexible way to manage and display this content. Next, we will together learn how to obtain and display the website filing number and copyright information in the AnQiCMS template.

2025-11-09

How to use AnQiCMS's pseudo-static function to optimize URL structure for better search engine display effect?

In website operation, a clear and meaningful URL address, like a business card of the website content, not only helps visitors quickly understand the theme of the page, but is also a key factor for search engines to identify, crawl, and rank the website content.A well-designed URL structure can significantly improve a website's performance in search engines and bring more natural traffic.AnQiCMS as an efficient content management system, understands the importance of URL structure for SEO, and therefore provides powerful and flexible pseudo-static functions to help us easily optimize the website's URL.### Static URL Rewriting

2025-11-09

How to implement lazy loading of images in document content in AnQiCMS to improve page loading speed?

In website operation, page loading speed is always one of the key factors affecting user experience and search engine optimization (SEO).Especially for websites with rich content, images are often the main factor that slows down page speed.Therefore, how to efficiently manage image resources has become an indispensable factor in improving website performance.Today, let's talk about how AnQiCMS cleverly implements lazy loading of images in document content, thus significantly improving page access speed.In AnQiCMS's design philosophy, website performance optimization is one of its core advantages.

2025-11-09

How to get and display the extended fields of a custom content model in the AnQiCMS template?

Flexibly obtain and display extended fields of custom content models in the AnQiCMS template, which is the key to achieving personalized content display on the website.AnQiCMS powerful content model customization capabilities, allowing us to add various unique attributes to articles, products, and other content according to actual business needs, and how to present these customized data beautifully in the frontend template is exactly the topic we are going to delve into today.### The Flexibility of AnQiCMS Content Model AnQiCMS is an enterprise-level content management system developed based on the Go language

2025-11-09

How to implement nested display of multi-level category navigation with the `categoryList` tag in AnQiCMS?

When building a website with comprehensive functionality, a clear and easy-to-navigate category system is the key to improving user experience and optimizing content structure.AnQiCMS as an efficient enterprise-level content management system provides flexible template tags to meet diverse content display needs.Among them, the `categoryList` tag is the core tool for achieving nested display of multi-level classification navigation.

2025-11-09