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