Can the `removetags` filter remove the specified tags from the HTML content (such as ``)?

Calendar 👁️ 67

In such a flexible content management system as AnQiCMS, dealing with HTML content is a common task in daily operations.Sometimes, we hope to remove certain specific tags from the content without completely stripping all the HTML structure, in order to maintain the consistency of the page display or to comply with design specifications. At this time,removetagsThe filter has become a very practical tool.

UnderstandingremovetagsFilter

removetagsIs an Anqi CMS template engine provided built-in filter, its main function is to remove the specified one or more HTML tags from the HTML content. Unlike the possibility of cutting all HTML tags at once, only leaving plain text.striptagsThe filter is different,removetagsIt provides a more refined and targeted control capability.

When using this filter, the basic syntax follows the common filter format of the Anqi CMS template:{{ 变量名|removetags:"标签列表" }}. It should be noted that when you are dealing with content that includes HTML structures and you want the final output to be parsed as HTML by the browser, it is usually necessary to use in conjunction with|safefilter.|safeTell the template engine that this part of the content is safe, no HTML entity encoding is required, and HTML code can be output directly.

removetagsactual application

The strength of this filter lies in its flexibility. You can specify the removal of a single tag, as well as the removal of multiple tags at the same time.

Remove a single HTML tag

Assuming your article content uses<i>tags to represent italic text, but since the new design style no longer requires italics, or you wish to use CSS to control it. You can use it like thisremovetags:

{# 原始内容: 这段内容<strong>包含<i>斜体</i>和粗体</strong>文字 #}
{{ "这段内容<strong>包含<i>斜体</i>和粗体</strong>文字"|removetags:"i"|safe }}
{# 输出结果: 这段内容<strong>包含斜体和粗体</strong>文字 #}

In this example, only<i>the tags and their corresponding closing tags were removed, while<strong>the tags and their internal text content were retained in full.

multiple specified HTML tags were removed

If you need to remove multiple different HTML tags at the same time,removetagsYou can also handle it easily. Just use English commas to separate the tag names you want to remove in the filter parameters. For example, if you want to remove<i>(italic) and<span>Tags:

{# 原始内容: 这里有<strong><i>斜体文字</i></strong>以及<span style="color:red;">红色文字</span> #}
{{ "这里有<strong><i>斜体文字</i></strong>以及<span style=\"color:red;\">红色文字</span>"|removetags:"i,span"|safe }}
{# 输出结果: 这里有<strong>斜体文字</strong>以及红色文字 #}

Here you can see,<i>and<span>Tags including<span>On the tagstyleThe properties were completely removed, leaving only their internal text and other unspecified HTML tags.

Application in the content field

In Anqi CMS, the article detail page usually displays through{{archive.Content|safe}}to show the article content. If you need to remove some tags during display, you can directly apply theremovetagsfilter to the content field:

{# 移除文章内容中的<i>和<b>标签 #}
{% archiveDetail articleContent with name="Content" %}
{{ articleContent|removetags:"i,b"|safe }}

Similarly, for single-page content, you can operate in this way:

{# 移除单页面内容中的<style>标签 #}
{% pageDetail pageContent with name="Content" %}
{{ pageContent|removetags:"style"|safe }}

This processing method is very flexible, you can trim the content at the template level according to the display requirements of the specific page, without modifying the original database content, which greatly simplifies the work of content maintenance and adjustment of display strategies.

WhyremovetagsSo practical?

In content operation,removetagsFilters have multiple values:

  1. Maintain the unity of content structure:When you collect or import content from different sources, different editors may generate various redundant or irregular HTML tags. UseremovetagsCan help you standardize content, ensure the consistency of the website style.
  2. Improve display neatness:Remove unnecessary or outdated HTML tags (such as early thefontTags can make the page code more concise, load faster, and also avoid layout confusion caused by redundant tags.
  3. Optimize the content management process:Operation personnel do not need to delve into the HTML source code, they can control the final presentation of content by configuring filters in the template, which improves work efficiency.

In short,removetagsThe filter provides Anqi CMS users with a powerful and precise HTML content processing capability.It allows you to cut out the 'excess parts' in the HTML content like a surgeon, while retaining the core structure, thus better serving the design, user experience, and content management goals of the website.


Frequently Asked Questions (FAQ)

Question:removetagsCan the filter remove attributes from HTML tags, such as<p style="color:red;">ofstyle?

Answer: removetagsWhat the filter removes isthe entire HTML tag elementIncluding the tag itself, its closing tag, and all the attributes within the tag. For example, if you useremovetags:"p", then tags like<p style="color:red;">我的文本</p>will be included along withstyleThe attributes are removed together, leaving only "My text". It will not remove the attributes while retaining the tags.

Ask: If I only want to remove all HTML tags and keep plain text, which filter should I use?

Answer:In this case, you should usestriptagsfilter.striptagsThe filter will strip all HTML, XML, and PHP tags from the content, leaving only plain text. For example,{{ "<strong><u>AnQiCMS</u></strong>"|striptags }}the output will be "AnQiCMS".

Question:removetagsCan the filter handle nested HTML tags? For example, if I remove<i>a tag that is contained within another<strong>a tag, what will be the result?

Answer: removetagsThe filter will remove all specified tags from the content, regardless of whether they are nested within other tags. For example, for content<strong>这段<i>非常</i>重要的文本</strong>if used{{ "<strong>这段<i>非常</i>重要的文本</strong>"|removetags:"i"|safe }}the output will be<strong>这段非常重要的文本</strong>.<i>The tag and its content will be removed, only the external<strong>.

Related articles

How to remove all HTML tags from dynamically generated HTML content?

In website content management, we often encounter a common requirement: to extract pure text information from dynamically formatted content.The reasons behind this are diverse, such as the need to generate concise and clear meta descriptions (Meta Description) for search engines, to display unformatted summaries on list pages, or simply to obtain clean plain text content for data analysis.AnQi CMS as a flexible and efficient content management system, fully considering these scenarios, through its powerful template engine and built-in filters

2025-11-08

How does the `yesno` filter handle boolean or null values and customize the display of 'Yes/No/Unknown'?

In AnQi CMS template development, how to display boolean (true/false) states or handle unknown (empty) values in an intuitive and concise manner is an important aspect for improving user experience and code readability.The `yesno` filter is designed for this purpose, it can simplify complex logical judgments into a single line of code, and allows you to customize the output results, such as displaying as "yes/no/unknown".### `yesno` filter: Smart converter for boolean and null values In a content management system, we often encounter situations where we need to display whether a project is enabled or a feature is turned on

2025-11-08

What is the use of the `addslashes` filter in JavaScript or JSON data output?

In website content management, especially when we want to insert dynamic data into JavaScript code or construct JSON formatted output, handling special characters is a non-negligible aspect.The AnQiCMS template engine provides a rich set of filters to help us elegantly handle such issues, with the `addslashes` filter being a practical tool specifically designed for this kind of scenario.The purpose of the `addslashes` filter explained

2025-11-08

How to ensure that single quotes, double quotes, and backslashes are correctly escaped in HTML output?

During website operation and template creation, we often need to output dynamic content to the HTML page.This is a common but often overlooked question: How to ensure that special characters such as single quotes, double quotes, and backslashes in the content do not破坏 the page structure or cause security issues when output to HTML?Don't worry, AnQiCMS provides very friendly built-in mechanisms and flexible tools in this aspect, which help us handle it easily.### AnQiCMS's default security mechanism: automatic escaping AnQiCMS has taken full consideration of content security in its design

2025-11-08

How to get the first or last element of a list in AnQiCMS template?

When building a website, we often encounter the need to pick out the most special one from a pile of content, such as displaying the latest article as the headline, or highlighting a popular product.In the AnQiCMS template, it is crucial to flexibly obtain the first or last element of the list to meet these requirements.Fortunately, AnQiCMS provides a variety of intuitive and efficient methods to handle these scenarios, making content display more vivid.AnQiCMS's template system uses syntax similar to the Django template engine

2025-11-08

Does the `first` and `last` filter return a single Chinese character when processing Chinese string?

In Anqi CMS template development, we often use various filters (filters) to format or extract data.The `first` and `last` filters are among the most common ones, used to get the first or last element from a string or array.Many friends who use AnQi CMS may be curious, when we process data containing Chinese characters, such as article titles or content snippets, will these two filters return single Chinese characters?The answer is: **Yes, the `first` and

2025-11-08

How to encode URL parameters to ensure the correctness and security of the link?

In the daily operation of Anqi CMS, we often need to deal with various website links, which not only need to be beautiful and SEO-friendly, but more importantly, they must work correctly and safely.Among these, encoding URL parameters is a seemingly trivial but crucial link, which is directly related to the integrity and user experience of our website links.Why is URL parameter encoding so important?Imagine if your website had a search function, and users entered keywords containing spaces, special characters, or even Chinese characters, such as "Anqi CMS"}

2025-11-08

What is the difference between the `urlencode` and `iriencode` filters in URL escaping?

In the daily content operation and website development of Anqi CMS, we often need to handle URL links to ensure that they are both secure and effective, and can be correctly parsed by browsers and search engines.This is an indispensable part of URL escaping (also known as encoding).Our Anqi CMS provides us with two very practical filters: `urlencode` and `iriencode`, both of which can help us with URL escaping, but their purposes and processing methods are different in actual application.###

2025-11-08