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:
- 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. Use
removetagsCan help you standardize content, ensure the consistency of the website style. - Improve display neatness:Remove unnecessary or outdated HTML tags (such as early the
fontTags can make the page code more concise, load faster, and also avoid layout confusion caused by redundant tags. - 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>.