How to accurately remove all HTML tags from AnQiCMS template HTML content, leaving only plain text information?

Calendar 👁️ 50

In AnQiCMS template design, we often encounter situations where we need to display content but do not want to show the included HTML tags.For example, we may need to extract the plain text summary of an article, or display unformatted category descriptions on a list page.Directly outputting content that includes HTML may disrupt the page layout and even pose security risks.AnQiCMS's powerful template engine provides a simple and efficient solution, helping us accurately remove HTML tags and retain only plain text information.

Know the "behind-the-scenes hero" of text processing

AnQiCMS's template system borrowed the grammar features of Django template engine, which includes a rich set of filters (Filters) for various variable processing, including text formatting and cleansing. When it comes to removing HTML tags, two very practical filters are mainly used:striptagsandremovetags.

1.striptagsFilter: One-click purification of all HTML tags

When our goal is to completely remove all HTML, XML, and even PHP tags from the content, leaving only pure text information, striptagsThe filter is our first choice. It is like a professional cleaner, able to remove all the tag structures from the content, leaving no trace.

The usage method is very intuitive:Assuming we have a variablearchive.ContentIt includes the complete HTML content of the article. If you want to display only the plain text part of this content in the template, you can use it like this:

{{ archive.Content|striptags }}

Through this simple operation,archive.Contentall of the<p>,<div>,<strong>,<img>All HTML tags will be removed, leaving only the text inside them.

Why should it cooperate?|safeWhy use the filter?In the AnQiCMS template, to prevent cross-site scripting (XSS) and other security issues, the template engine defaults to escaping all output content to HTML entities. This means that if your original content contains something like<or>Special characters (even if they are not part of the tag but are text content) may also be escaped into&lt;or&gt;.

striptagsAlthough the filter removes the tags, if the original text already contains HTML entities (such as&amp;Indicates&symbols),striptagsit will not convert them back to the original characters. More importantly, ifstriptagsThe text contains after processed<or>Characters such as default escaping mechanisms will re-escape them. To ensure that the final output is the pure text we expect and to avoid unnecessary double escaping, we usually escape them instriptagsAdd one later|safefilter.|safeThe filter tells the template engine that this part of the content is safe and does not require additional HTML entity encoding processing.

Therefore, the recommended usage is:

{{ archive.Content|striptags|safe }}

This removes the HTML tags and ensures that the final output text content is not re-escaped by the template engine.

2.removetagsFilter: accurately remove specified tags

Sometimes, we may not need to remove all HTML tags, but rather want to retain certain specific tags while removing all other tags. In this case,removetagsThe filter can be put to good use. It allows us to specify one or more HTML tags to be removed.

Usage:InremovetagsAfter the filter, list the names of the tags to be removed in the form of comma-separated values. For example, if you want to remove<b>and<i>tags, you can write it like this:

{{ archive.Description|removetags:"b,i"|safe }}

This operation will removearchive.Descriptionall of the<b>and<i>tags, but if the content contains<p>or<span>Other tags will be retained. Similarly, to avoid unnecessary escaping, it is recommended thatremovetagsafter that|safefilter.

Example of practical application scenarios

These filters are widely used in the daily content operation of AnQiCMS.

Display a concise summary on the list page:When we need to display the summary of each article on the article list page without any formatting:

<div class="article-summary">
    <p>{{ item.Description|striptags|safe }}</p>
</div>

Extract plain text introduction on the category page:If category profilecategory.ContentMay contain rich text and images, but we only want to display the plain text part at a specific location:

<h2 class="category-title">{{ category.Title }}</h2>
<div class="category-plain-text-intro">
    <p>{{ category.Content|striptags|safe }}</p>
</div>

No matter whether it is article content, category description, single page content, or any other custom field that may contain HTML, as long as you want to extract its plain text information, these two filters can provide strong support.

By flexible applicationstriptagsandremovetagsFilter, we can easily control the display form of page content, ensuring clarity and tidiness of information, while maintaining the overall beauty and functionality of the website.


Frequently Asked Questions (FAQ)

Q1:striptagsandremovetagsWhat are the main differences between these two filters? A1: striptagsThe filter removes all detected HTML, XML, and PHP tags from the text content, the purpose is to obtain pure text information without retaining any formatting. AndremovetagsThe filter provides finer control, it only removes the HTML tags you explicitly specify, and other unspecified tags will be retained. If your goal is to thoroughly clean the content and remove all formatting, please usestriptags; If you want to retain part of the tags, for example, only remove the script tags while retaining the paragraph tags, you should useremovetags.

Q2: I used|striptagsfilter, but why can the text like&amp;or&lt;What does this character represent? A2: striptagsThe filter is responsible for removing HTML tag structures (such as<p>/<div>), it does not handle HTML entities (such as&amp;/&lt;). If the original content you have already contains these HTML entities,striptagsHuaiyuan

Related articles

In AnQiCMS template design, in which cases do you need to explicitly use the `safe` filter to ensure that rich text content is rendered correctly as HTML?

During the template design process of AnQiCMS, it is crucial to understand when to explicitly use the `safe` filter to ensure that rich text content is rendered correctly while maintaining website security.AnQiCMS's template engine, similar to many modern CMSs, in order to prevent security vulnerabilities such as cross-site scripting attacks (XSS), it defaults to escaping all content output through the `{{ variable }}` method.This means, if you directly output a text containing HTML tags, for example `{{ article content }}`

2025-11-08

How to safely display a string that may contain HTML tags in AnQiCMS template and prevent XSS injection attack?

In website operation, ensuring the security of content is a crucial link, especially when your website allows users to submit content or display data from different sources.Cross-site scripting (XSS) attacks are one of the common threats that can lead to data leakage of website users, session hijacking, or even website tampering.For those of us who use AnQiCMS to manage content, understanding how to safely display strings that may contain HTML tags is the foundation for preventing such attacks.The AnQiCMS template engine handles variable output when

2025-11-08

How to control the display length of the link text and automatically add an ellipsis when the `urlizetrunc` filter converts URLs in the AnQiCMS template to links?

In website content management, we often need to display various links on the page, whether it is the cited URL in the article or the external links submitted by users.However, these links are sometimes very long, not only affecting the aesthetics of the page, but also possibly destroying the original layout, making the page look disorganized.AnQiCMS provides a very practical template filter——`urlizetrunc`, which can help us elegantly solve this problem, making long links clickable while presenting them in a concise and beautiful way.`urlizetrunc`

2025-11-08

How to automatically scan and convert ordinary text content in the AnQiCMS template into clickable URL links or email addresses?

In website content operation, we often need to display some URLs or email addresses in articles or pages. If these addresses are only in plain text, users cannot directly click to jump, which not only affects the user experience but may also make search engines difficult to recognize these valuable link information.Fortunately, AnQiCMS provides a very convenient set of built-in features that can help us automatically convert ordinary text content into clickable hyperlinks or email links, making the website content more interactive and professional.To implement this feature, we mainly use AnQiCMS

2025-11-08

How does the `removetags` filter selectively remove specified HTML tags from the AnQiCMS template HTML content while retaining other tags?

In AnQiCMS template development, we often need to finely control the displayed content.Especially when dealing with user input, extracting content from rich text editors, or adapting content to different layouts, you may encounter some HTML tags that you want to retain and others that you want to remove.At this time, the powerful template filter of AnQiCMS can be put to use, among which the `removetags` filter is the ideal tool to meet such needs.### Core Feature Revelation: `removetags`

2025-11-08

How to convert the first letter, all letters, or the first letter of each word of an English string to uppercase in AnQiCMS template to meet the typesetting standards?

In website content operation, maintaining consistent text formatting is the key to enhancing user reading experience and professional image.Especially for English strings, sometimes we need to capitalize the first letter, all letters, or the first letter of each word according to design or convention.AnQiCMS (AnQiCMS) template system provides us with a flexible and efficient way to meet these layout requirements.The template engine of AnQi CMS is designed to borrow the simplicity and power of Django templates, allowing us to display data and control the page structure through intuitive syntax. Among them,

2025-11-08

How to extract a specified element from a string or array at a specified start and end position in the AnQiCMS template to achieve content snippet extraction?

In AnQiCMS template design, sometimes we need to accurately extract a specific part from a long text or a data list.Whether it is to extract the summary of an article, display several images in a carousel, or process part of an array of data, the ability to flexibly operate on string and array fragments is crucial.A powerful template engine for AnQiCMS, drawing on the syntax features of Django templates, provides us with concise and efficient 'filters' (Filters) to easily meet these needs.Understanding Core: `slice`

2025-11-08

The `repeat` filter in AnQiCMS template design, in addition to text repetition, what are some creative or practical uses?

The template system of AnQiCMS (AnQiCMS) provides a wide space for content display with its flexibility and powerful functions.Among many built-in filters, the `repeat` filter appears to be just a simple text copy at first glance, but as long as we step out of the conventional thinking, it can bring unexpected creativity and practical value to template design and content operation.It is not just a tool to repeat a text multiple times, but also a tool that can cleverly integrate into design, enhance user experience, assist development, and even realize lightweight data visualization.First, we can turn

2025-11-08