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

Calendar 👁️ 54

In AnQiCMS template development, we often need to control the displayed content in detail.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 keep and others that you want to remove.At this time, the powerful template filter of AnQiCMS can be put into use, among whichremovetagsThe filter is the ideal tool to meet such needs.

The core feature revelation:removetagsFilter

removetagsThe function of the filter is as its name implies: it can selectively remove the specified HTML tags from a piece of HTML content while completely preserving other unspecified tags and their internal text.This contrasts with some filters that remove all HTML tags in a "one-size-fits-all" manner, giving content output great flexibility.

Its basic usage syntax is very intuitive:

{{ 你的HTML内容 | removetags:"要移除的标签1,要移除的标签2" | safe }}

Let's delve into this structure in detail:

  • 你的HTML内容This is the original HTML string variable you want to process. For example, it may be the content of a document detail.archive.ContentOr the description of a categorycategory.DescriptionOr the content of a single pagepage.Contentetc.
  • removetags:"要移除的标签1,要移除的标签2"This isremovetagsThe filter itself. Within the colon of the filter name, you need to enclose all the HTML tag names you want to remove in English double quotes, with tag names separated by English commas,Separate. It is worth noting that only the label name should be filled in here, without including<or>these angle brackets.
  • | safe: This is used forremovetagsA very critical suffix when filtering. The AnQiCMS template engine, for security reasons, defaults to escaping all output HTML content, which means that like<p>such tags will be converted to&lt;p&gt;Thus, it is displayed in pure text format. However, when we useremovetagswe hope that the preserved HTML tags can be normally parsed and rendered by the browser. Therefore,| safeThe filter tells the template engine that this content has been safely processed and can be safely output as HTML without additional escaping.

Give a simple example, suppose we have a piece of HTML content<strong><i>安企CMS</i></strong>, and we only want to keep the bold tags<strong>, remove the italic tags<i>. We can use it like this:

{{ "<strong><i>安企CMS</i></strong>"|removetags:"i"|safe }}

The output of this code will be:<strong>安企CMS</strong>. Italic tags.<i>Removed, while the bold tag<strong>and its text content is retained.

If you need to remove multiple tags, for example, remove tags at the same timedivandspanthen it can be written like this:

{{ "<div><p>欢迎使用<span>AnQiCMS</span></p></div>"|removetags:"div,span"|safe }}

The output result will be<p>欢迎使用AnQiCMS</p>.

withstriptagsThe difference: precision and comprehensiveness

In AnQiCMS's filter, there is also a filter namedstriptags. It can also remove HTML tags, but it has an essential difference withremovetags: striptagsWill removeallHTML tags, leaving only plain text content.

For example:{{ "<strong><i>安企CMS</i></strong>"|striptags }}The output result is安企CMS.

Therefore, when your goal is to obtain a pure piece of content with no HTML structure,striptagsIt is a more convenient choice. And when you need to retain some HTML tags, only removing specific tags,removetagsit is the more precise scalpel in your hand.

Practical Application Scenario: Make content purer and safer

removetagsThe application scenarios of filters are very extensive, which can effectively improve the safety and display standardization of website content:

  1. Clean up user submitted contentIn forums, comment sections, or user-generated content boards, users may enter malicious scripts (such as<script>tags) or improper styles (such as<font>/<style>The HTML of the label. UseremovetagsThese potentially dangerous or unnecessary labels can be safely removed, for example:{{ user.Comment|removetags:"script,iframe,style,font"|safe }}

  2. Unified content display style: When a website displays an article summary on a list page, it may not want the summary content to show paragraphs<p>or title<h1>etc. tags, only simple text and line breaks are desired<br>. ThroughremovetagsCan quickly standardize content:{{ archive.Description|removetags:"p,h1,h2,div"|safe }}

  3. Extract specific elements from rich textSometimes, the complete rich text content needs to be simplified for different layouts, such as in the mobile view, where you may want to remove all tables<table>and image<img class="full-width">and retain only the core text information.removetagsCan help you easily achieve.

  4. SEO content optimization: Although search engines usually parse HTML well, removing some redundant or irregular tags can help provide a cleaner HTML structure, which may be helpful for the crawling and parsing efficiency of search engines.

In the specific application of AnQiCMS template

In AnQiCMS, you can use any variable that needs to handle HTML contentremovetagsa filter. For example:

  • Article detail page (detail.html)When you need to allow only partial basic formatting in the article content and remove all custom font colors or sizes and other tags:<div>{{ archive.Content|removetags:"span,font,div"|safe }}</div>

  • Category list page (list.html)When you want to display category descriptions in a list in a concise form without complex HTML structures:<span>{{ category.Description|removetags:"p,br,strong"|safe }}</span>

Use suggestions and precautions

  • Always match|safe:Again, if you want to retain the HTML tags and have them rendered normally,|safeThe filter is indispensable.
  • specify the tag names clearly:Make sure the tag names you provide are standard HTML tags and do not contain<and>.
  • Handle with careWhen removing tags, please make sure you understand the important information or styles they may contain to avoid accidentally deleting key content or disrupting the expected layout.
  • Test FirstUse in any production environmentremovetagsBefore that, be sure to thoroughly test in the development or test environment to ensure that the content is displayed as expected and no unexpected side effects occur.

By mastering the use ofremovetagsFilter, AnQiCMS users can more freely control the output of HTML content from templates, whether it is to enhance security, unify the visual style, or optimize content display for different devices, they can find efficient and flexible solutions.


Frequently Asked Questions (FAQ)

  1. Question:removetagsDoes the style in the content remain after the filter removes the tags? Answer: removetagsThe filter only removes the HTML tags themselves, and the text content within the tags is retained. If the removed tag (for example<span style="color:red;">)With inline styles, these styles will disappear when the tag is removed. If the style is applied through a CSS stylesheet to other retained tags or

Related articles

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

In AnQiCMS template design, we often encounter situations where we need to display content but do not want to show the HTML tags contained in it.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 concise and efficient solution, helping us accurately remove HTML tags and retain only plain text information.###

2025-11-08

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 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

How to ensure that the AnQiCMS website content is displayed efficiently to cope with high concurrency access?

When operating a website, whether the content can be presented quickly and stably to users, especially during periods of increased traffic, is a key indicator of the efficiency of a content management system.AnQiCMS is a system developed based on the Go language, which has fully considered performance in high-concurrency scenarios from the beginning of its design.Understand and make good use of its built-in features and recommended strategies, which can help us better cope with traffic challenges.**The foundation of AnQiCMS performance: an efficient and stable architecture** The reason why AnQiCMS can operate efficiently in high-concurrency environments is inseparable from the technical choices at the bottom layer

2025-11-08