Only delete the specified HTML tags in AnQiCMS article content, which filter should be used?

Calendar 👁️ 73

When using AnQiCMS for content operations, we often encounter situations where we need to refine the processing of HTML tags in the article content.For example, during the import or paste process, some unnecessary, format-inconsistent tags may be introduced, which may affect page layout, SEO performance, and even introduce security risks.In order to meet the requirement of only deleting the specified HTML tags from the content of AnQiCMS articles, we need to use the specific filter provided by the template engine.

In AnQiCMS template system, there are mainly two filters for handling HTML tags:striptagsandremovetagsThey each have their own focus, understanding the differences is the key to choosing the correct filter.

  • striptagsFilter:This filter willstrip all HTML, XML tags from the string. If you want to completely remove all HTML formatting from the content and only retain plain text, thenstriptagsIt is the ideal choice. For example, it will<strong><i>Hello!</i></strong>afterstriptagsAfter processing, only the following will remainHello!.
  • removetagsFilter:This is the answer we are looking for.removetagsThe filter allows youTo specify the HTML tags to be deletedThis means you can retain most of the useful HTML structure (such as paragraphs<p>, image<img>, links<a>), while precisely removing unnecessary and interfering tags (such as uncommon style tags, script tags, etc.).

Therefore, to only delete the specified HTML tags from the content of AnQiCMS articles, you should useremovetagsFilter.

removetagsHow to use the filter?

removetagsThe filter is very intuitive to use. Its basic syntax is:{{ obj|removetags:"标签1,标签2,标签3" }}.

Here:

  • obj: represents the string variable you want to process, usually a field of the article content, for examplearchive.Content.
  • "标签1,标签2,标签3"This is a string containing the HTML tag names you want to remove, separated by English commas.,The tag names do not need to include angle brackets (<and>),just write the tag name itself (for example,"div,span,font")

A note worthy of attention:removetagsFilters usually need to be used with|safeFilter combined use.

AnQiCMS's template engine escapes HTML tags by default when outputting content for security reasons to prevent cross-site scripting (XSS) attacks. This means that if your content contains raw HTML, it will be converted to entity characters (such as,<becomes&lt;), rather than being parsed as HTML.

While usingremovetagsAfter the filter has processed, the specified tags in the content have been removed, but they are still considered as strings that may contain HTML. If at this point, you do not add|safeThe filter, the template engine will escape the remaining content again, causing the browser to display the processed HTML code as plain text instead of rendering it as web content. Therefore, to ensure that the content after removing the tags can be correctly parsed and displayed as HTML by the browser, it is essential toremovetagsthen add|safe.

Let's understand through some specific examples:

Example one: Delete a single HTML tag

Suppose your article content fieldarchive.ContentContains the following HTML:

<p>这是一段 <b>重要的</b> 内容,但这里有一个不必要的 <i>斜体</i> 标签。</p>

If you want to delete all of them<i>Tags:

{{ archive.Content|removetags:"i"|safe }}

The result will be:

<p>这是一段 <b>重要的</b> 内容,但这里有一个不必要的 斜体 标签。</p>

Example two: Delete multiple HTML tags

Suppose there are many in the article content<span>and<div>Tags, do you want to remove them:

<div><p>这是一段内容。</p><span>不想要的文本。</span></div>

You can use it like thisremovetags:

{{ archive.Content|removetags:"div,span"|safe }}

The result will be:

<p>这是一段内容。</p>不想要的文本。

In this way, you can flexibly control the display of HTML tags in the article content, ensuring that the content is presented in the most consistent with your expectations on the website, thereby enhancing the quality of the content and the user experience.


Frequently Asked Questions (FAQ)

Q1: If I want to remove all HTML tags from the article content instead of just a few, which filter should I use?A1: If you need to completely remove all HTML tags from the content and keep only plain text, then you should usestriptagsa filter. For example:{{ archive.Content|striptags }}This filter does not require a tag name, it will automatically identify and remove all HTML structures.

Q2: Why is it used inremovetagsorstriptagsPlus after the filter,|safeFilter?A2:|safeThe filter's role is to inform the template engine that the content processed by the previous filter is 'safe' and does not require further HTML entity encoding. If there is no|safeEven if you remove the unnecessary tags, the remaining valid HTML tags (such as<p>/<a>) may also be escaped into&lt;p&gt;/&lt;a&gt;It causes them to be displayed on the page as plain text code instead of being rendered by the browser. Therefore,|safeIt ensures that the content is correctly parsed as HTML after processing.

Q3:removetagsCan the filter be used to remove plain text from the text, rather than HTML tags?A3:removetagsThe filter is specifically designed to handle HTML tags. If you want to delete a certain plain string from the article content (for example, the word "AnQi CMS"), or replace a word, you should usecutFilter (delete) orreplaceFilter (replace). For example:{{ archive.Content|cut:"安企CMS" }}It will delete all "AnQi CMS" strings, but{{ archive.Content|replace:"旧词,新词" }}it will replace them.

Related articles

How to completely remove all tags from HTML content in the AnQiCMS template?

In website content operation, we often encounter such needs: to extract plain text from richly formatted content, such as displaying abstracts on article list pages, or providing clean text for SEO meta description tags.AnQiCMS provides a powerful and flexible template engine, allowing us to easily remove tags from HTML content with built-in filters.

2025-11-08

How to batch replace keywords or links in the entire site's content and display them immediately?

In website operation, we often encounter situations where we need to adjust a large amount of content, whether it is to adapt to new brand strategies, unify website terminology, fix broken links, or better optimize SEO, these all require batch updating of keywords or links throughout the entire site.Manually modifying one by one not only takes time and effort, but is also prone to errors.The AnqiCMS provides a very practical feature that allows you to easily deal with such challenges: batch replacement of keywords or links in the entire site content, and ensure that changes take effect immediately.This ability is crucial for the daily operation and maintenance of the website

2025-11-08

How to use the pseudo-static function of AnQi CMS to optimize URL display and improve SEO effect?

In website operation, the URL (Uniform Resource Locator) plays a vital role.A clear, meaningful, and easy-to-understand URL not only improves user experience but is also an indispensable part of search engine optimization (SEO).The traditional dynamic URL often contains parameters and numbers that are difficult to understand, while the pseudo-static URL, through rewriting technology, makes it look as simple and standardized as a static HTML page, thus gaining the favor of search engines and users.AnQiCMS (AnQiCMS) knows the impact of URL structure on website SEO

2025-11-08

How to set up document thumbnails and control their display on list pages?

In a content management system, a striking thumbnail can greatly enhance the attractiveness of the content, whether it is in an article list, product display, or other content aggregation pages, it is an important visual element to guide users to click.AnQiCMS (AnQiCMS) is well aware of this and provides flexible thumbnail settings and display control features to help optimize the visual effects and user experience of your website.Next, we will discuss in detail how to set the thumbnail of the document in Anqi CMS and master its various display methods on the list page.### The First Part

2025-11-08

How to safely truncate a long text containing HTML tags in AnQiCMS template?

In AnQiCMS template design, we often encounter scenarios where it is necessary to display long text content, such as the abstract on the article list page, and the brief introduction of product details.If this long text content is displayed directly, it may cause the page to be long, affecting user experience and layout aesthetics.Therefore, truncating long text is a common requirement.However, when long text content contains HTML tags, simple character truncation may cause problems.For example, a content like this `\u003cp\u003eThis is an important\u003cb\u003e text\u003c/b\u003e\u003c/p\u003e`

2025-11-08

How to avoid AnQiCMS from destroying the tag structure when truncating HTML text?

In content management, in order to maintain the neatness and loading efficiency of the website page, we often need to truncate articles, product descriptions, or other long text content, and only display part of the abstract.However, if the content itself contains HTML tags, simple character truncation often breaks the structure of these tags, causing the page display to become chaotic and even affecting the layout and functionality of the website.For example, the content of a `<p>This is an <b>important</b> paragraph</p>`, if it is simply truncated to `<p>This is an <b>important</b`

2025-11-08

How does AnQiCMS render Markdown formatted article content to HTML?

AnQiCMS boasts its efficient content management capabilities, is favored by users, especially in handling text content, providing flexible and diverse options.For users accustomed to using Markdown format, AnQiCMS also provides comprehensive support, able to seamlessly render Markdown formatted article content into user-friendly HTML pages.To understand how AnQiCMS achieves this transformation, we can explore from three aspects: content creation, system configuration, and template rendering.

2025-11-08

Can I control whether Markdown content in AnQiCMS templates is automatically converted to HTML?

In website content management, we often need to balance the writing efficiency of content and the final presentation effect.Markdown with its concise syntax greatly enhances the speed of content creation.But the question that follows is: do we always want the content to be automatically converted to HTML when it enters the template?Or in some specific scenarios, we want to maintain the original Markdown format, or manually control the conversion process?

2025-11-08