When using AnQiCMS for content operation, we often encounter situations where we need to refine the HTML tags in the article content.For example, during the import or copy and paste process, some unnecessary, format-inconsistent tags may be brought in, which may affect the page layout, SEO performance, and even introduce security risks.For the requirement of "only delete the specified HTML tags from AnQiCMS article content", 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:striptagsandremovetags. They each have their own focus, understanding their differences is the key to correctly choosing the 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 keep plain text,striptagsThis is the ideal choice. For example, after<strong><i>Hello!</i></strong>Afterstriptagsprocessed, onlyHello!.
  • 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>English<a>),while precisely removing unnecessary and distracting tags (such as infrequently used style tags, script tags, etc.).

Therefore, to only delete the specified HTML tags in AnQiCMS article content, it 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":is a string containing the name of the HTML tag you want to delete, multiple tags need to be separated by English commas,. The tag name does not need to include angle brackets (<and>),只需写标签名本身(例如,"div,span,font").

一个重要的注意事项:removetags过滤器通常需要与|safethe filter.

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 original HTML, it will be converted to entity characters (for example,<becomes&lt;),而不是被浏览器解析为HTML。

When usingremovetagsFiltering processed, the specified tag in the content has been removed, but it is still considered a string that may contain HTML. If this is not added.|safeFilter, 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 necessary toremovetagsafter adding|safe.

Let's understand this through some specific examples:

Example 1: Delete a Single HTML Tag

Assuming your article content fieldarchive.ContentContains the following HTML:

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

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

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

The output will be:

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

Example 2: Delete Multiple HTML Tags

Assuming the article content has many<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 output will be:

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

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


Common Questions (FAQ)

Q1: If I want to remove all HTML tags from the article content instead of a few specified ones, 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 usestriptagsfilter. For example:{{ archive.Content|striptags }}This filter does not require specifying the tag name, it will automatically identify and remove all HTML structures.

Q2: Why do I need to addremovetagsorstriptagsfilterer after using|safeFilter?A2:|safeThe role of the filter is to inform the template engine that the content processed by the preceding filter is "safe" and does not need to be further converted to HTML entities. If there is no|safe,even 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;Therefore, they are displayed as plain text code on the page instead of being rendered by the browser. Thus,|safeensured that the content can be correctly parsed as HTML after processing.

Q3:removetagsFilter can be used to remove plain strings from text instead of HTML tags?A3:removetagsThe filter is specifically designed to handle HTML tags. If you want to delete a specific plain string (such as the word "AnQi CMS") from the content of the article, or replace a word, you should usecutFilter (delete) orreplaceFilter (replace). For example:{{ archive.Content|cut:"安企CMS" }}it will delete all 'AnQi CMS' strings, and{{ archive.Content|replace:"旧词,新词" }}it will perform the replacement.