How to remove HTML tags in AnQiCMS template and display only plain text content?

Calendar 👁️ 56

When using AnQiCMS for content creation and display, we often need to flexibly control the presentation of content on the front end.Sometimes, an article or product description may contain rich HTML tags, which are used to provide exquisite layout on the detail page.However, in some other scenarios, such as displaying content summaries on list pages, as SEO descriptions (meta description) output, or simply to avoid style conflicts in some specific UI elements, we may only need the plain text form of the content and not any HTML tags.

The AnQiCMS template system is based on Django template engine syntax, which provides powerful filter functions to help us easily achieve this goal.It is built-in with various filters, which can process variable content, including a tool specifically used to remove HTML tags.

To make the content in the template display only plain text, the core is to use appropriate filters to 'strip' HTML tags. Here, two very practical filters will be mainly used:striptagsandremovetags.

Completely strip all HTML tags: usestriptagsFilter

striptagsThe filter is your preferred choice if you want to completely remove all HTML tags from the content and only retain pure text information.The way it is used is very simple and intuitive, like peeling off all the formatting from a piece of text.

For example, your article detail variablearchive.ContentOr description variablearchive.DescriptionMay include<p>/<strong>/<a>And tags, if you output directly in the template{{ archive.Description }},AnQiCMS for security reasons will default to escaping HTML tags, and will<p>displayed as&lt;p&gt;, The user will see the original HTML code instead of the rendered effect. And if you use{{ archive.Description|safe }}, tags will be parsed and rendered normally by the browser.

But when your goal is plain text, you can use it like thisstriptags:

{# 假设archive.Description内容为:<p>这是一段<b>带有</b>HTML标签的描述。</p> #}
<p>文章摘要:{{ archive.Description|striptags }}</p>

{# 假设archive.Content内容包含大量HTML排版 #}
<div class="summary-text">
    {{ archive.Content|striptags }}
</div>

after|striptagsAfter the filter, no matterarchive.Descriptionorarchive.ContentIt originally contained a very complex HTML structure, the output will be something like 'This is a description with HTML tags.'This plain text, all HTML tags will be cleanly removed.

Selective removal of HTML tags: useremovetagsFilter

In certain more refined scenarios, you may not want to remove all HTML tags, but rather keep some specific tags, such as only keeping<strong>Labels are used to emphasize keywords while removing all other labels. At this time,removetagsThe filter comes into play.

removetagsAllow you to specify one or more HTML tag names (separated by commas), it will precisely remove the specified tags while retaining other tags that are not specified.

How to use:

{# 假设articleContent内容为:<p>欢迎<b>使用</b><i>AnQiCMS</i>的<strong>强大功能</strong>。</p> #}
{# 我们只希望移除 <p>、<i> 标签,保留 <b> 和 <strong> #}
<div class="partially-formatted-text">
    {{ articleContent|removetags:"p,i"|safe }}
</div>

In the above example, the output will be "Welcome"UseAnQiCMS'Powerful features. As you can see,<i>the tags have been removed, and<b>and<strong>The tag is preserved. It should be noted that if you want the tag to be rendered normally by the browser instead of displaying the original HTML code, then inremovetagsAfter that, it is usually necessary to add|safefilter.

Actual application suggestions

  • List page summary and SEO description:This is the most common pure text requirement scenario. When displaying an article list or product list, a brief summary is usually displayed. At this time, use|striptagsMake sure the output is plain text to avoid HTML tags from breaking the page layout or affecting the crawling of SEO tools.
  • Front-end JS operation content:If you need to get the innerText or textContent of a DOM element through JavaScript, then provide plain text content on the backend template level, which can simplify the writing of frontend scripts.
  • Rich text and plain text coexist:You can still use it on the article detail page,{{ archive.Content|safe }}To render complete HTML content; while in the sidebar or Meta information area of the same page, you can use{{ archive.Description|striptags }}to get the plain text summary.

These filters provided by AnQiCMS offer great flexibility for content display, allowing us to precisely control the presentation form of content according to different needs, thereby creating a more professional and user-friendly website user experience.


Frequently Asked Questions (FAQ)

  1. |striptagsand|safeWhat are the differences between filters? |striptagsThe filter's function is to remove all HTML tags from the content, leaving only plain text. And|safeThe filter's role is to inform the AnQiCMS template engine that this content is safe, does not require HTML entity encoding, and can be rendered directly as HTML code on the page. In simple terms,striptagsStrip HTML,safeAllow HTML to be rendered. If you want to display the HTML in the original content, use|safe; If you want to remove all HTML and leave only plain text, use|striptags.

  2. |striptagsWill it remove all formats, including line breaks? |striptagsIt is mainly aimed at removing HTML tags, it usually will not remove the natural line breaks in the text content (\nIf you want to convert these line breaks into HTML's<br>tags, you can use them first|striptagsRemove the HTML tags first, then use them|linebreaksbror|linebreaksa filter. For example:{{ archive.Content|striptags|linebreaksbr }}.

  3. If I only want to keep specific HTML tags but also make sure the final result is plain text, how should I operate?If you want to keep, for example,<strong>or<em>Such emphasis tags, but do not want them to be displayed in HTML form, but rather represented by special characters in plain text (such as asterisks or underscores), thenremovetagsMaybe not **choose.removetagsIt retains the specified HTML tags. To convert these tags to plain text semantic expressions, it is usually necessary to do so on the backend (during content editing) or through more complex template logic (such as regular expression matching and replacement), rather than relying solely on these tag stripping filters.If the ultimate goal is plain text, even if some tags are retained, it should be usedstriptagsTo ensure that the final output is pure text without any labels.

Related articles

How to automatically add watermarks to images in article content in AnQiCMS to protect copyright?

In today's era of increasingly rich digital content, the copyright protection of original images is particularly important.Whether it is a carefully photographed product image or an illustration drawn for an article, they all embody the creator's efforts and the brand image of the website.However, the situation where pictures are downloaded and used without permission is common, which not only violates copyright but may also dilute brand influence.Luckyly, AnQiCMS provides a convenient and efficient solution that helps us automatically add watermarks to images on the website, thereby effectively protecting original content.

2025-11-07

How to format numbers in AnQiCMS templates, such as retaining decimal places?

In AnQiCMS template development, formatting numbers for display, especially controlling decimal places, is a very common requirement.No matter whether you want to display the price of products, the percentage in statistical data, or other values that need to be presented accurately, AnQiCMS's powerful template engine provides flexible filters (filters) to help you achieve these goals.

2025-11-07

How to implement custom template file calling for category or article detail pages in AnQiCMS?

During the process of building a website with AnQiCMS, we often encounter such needs: we hope that a certain category list page or a unique article detail page has a layout and style that is different from the global unified default template.AnQiCMS is a content management system focused on flexibility and customization, providing a very convenient and powerful solution in this regard, allowing us to easily implement this fine-grained template calling.Implement custom template calling for category or article detail pages, AnQiCMS mainly provides two main methods

2025-11-07

How to judge if a variable is empty in the template and display a default value?

During the presentation of web content, we often encounter such situations: a variable may be empty due to data loss, user failure to fill in, or other reasons.If you directly output these empty variables in the template, it may result in blank spaces on the page, affecting the appearance, or even cause layout errors, and may even expose unnecessary debugging information.It is particularly important to set a suitable default value for these variables that may be empty to ensure the completeness of website content display and the smoothness of user experience.The AnqiCMS uses a template engine syntax similar to Django

2025-11-07

How to display the current model name or URL alias in AnQiCMS?

In website content management, we often need to make the various parts of the website adapt and display information intelligently, one common requirement being how to dynamically display the name or URL alias of the current content model on the front-end page.This is crucial for improving user experience, optimizing search engine performance, and flexible management of website structure.AnQiCMS as an enterprise-level content management system provides a very flexible and powerful template tag function, making it intuitive and efficient to achieve this goal.

2025-11-07

How to display the list of internal titles (H1, H2, etc.) of the article content on the article detail page?

In Anqi CMS, in order to enhance the reading experience of long articles and the page SEO effect, we usually hope to display a list of internal titles (H1, H2, etc.) on the side or above the article content on the article detail page, which is what we usually call the article catalog or navigation.This makes it convenient for readers to quickly jump to the chapters of interest, and also helps search engines better understand the structure of the article. The AnQi CMS provides a very convenient way to implement this feature, the core lies in its powerful template tags and content field extraction capabilities.###

2025-11-07

How to implement keyword batch replacement in AnQiCMS and affect the display of front-end content?

It is crucial to maintain the accuracy, timeliness, and brand consistency of content in website operations.Especially when the amount of website content is large, if you need to make global changes to specific keywords or phrases, manual operation will be a huge project that is time-consuming and labor-intensive.AnQiCMS (AnQi CMS) exactly discerned this need, providing a powerful feature named 'Full Website Content Replacement' to help users efficiently manage and update website content, thereby affecting the display effect of front-end content.Why do we need to batch replace keywords? With the development of the enterprise, product updates, or market strategy adjustments

2025-11-07

How to ensure that custom parameters set in the AnQiCMS backend can be displayed in the front-end template?

The AnQi CMS endows content management with high flexibility, among which the customizable parameter function is the key to meeting the needs of personalized content display.How to accurately display the unique parameters set for content models such as articles, products, or categories on the website front-end template is a concern for many users.This article will detail this process, helping you fully utilize the powerful customization capabilities of Anqi CMS.

2025-11-07