In such a flexible content management system as AnQiCMS, handling HTML content is a common task in daily operations.Sometimes, we want to remove certain tags from the content precisely without completely stripping all HTML structure, in order to maintain the consistency of the page display or meet design specifications.removetagsThe filter has become a very practical tool.
UnderstandingremovetagsFilter
removetagsIt is a built-in filter provided by the AnQin CMS template engine, which mainly serves to remove one or more specified HTML tags from the HTML content. Instead of simply removing all HTML tags and leaving only plain text,striptagsThe filter is different,removetagsThe provided is a more refined and targeted control capability.
When using this filter, the basic syntax follows the general filter format of the Anqi CMS template:{{ 变量名|removetags:"标签列表" }}English needs to be particularly noted when you are manipulating content that includes HTML structure and you wish the output to still be parsed as HTML by the browser.|safeFilter.|safeTell the template engine that this part of the content is safe and does not need to be escaped as HTML entities. It can be output directly as HTML code.
removetagsPractical application of:
The strength of this filter lies in its flexibility. You can specify removing a single tag, or you can specify removing multiple tags at the same time.
Remove a single HTML tag
Suppose your article content uses<i>tags to indicate italics, but the new design style no longer requires italics, or you want to use CSS to control them. You can do it like thisremovetags:
{# 原始内容: 这段内容<strong>包含<i>斜体</i>和粗体</strong>文字 #}
{{ "这段内容<strong>包含<i>斜体</i>和粗体</strong>文字"|removetags:"i"|safe }}
{# 输出结果: 这段内容<strong>包含斜体和粗体</strong>文字 #}
In this example, only<i>the tag and its corresponding closing tag were removed, but<strong>the tag and its internal text content were retained in full.
Remove multiple specified HTML tags
If you need to remove multiple different HTML tags at the same time,removetags也能够轻松应对。您只需在过滤器参数中,使用英文逗号将需要移除的标签名隔开即可。例如,您想移除内容中的<i>(斜体)和<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 label,stylethe properties, have been completely removed, only their internal text and other unspecified HTML tags are left.
application in the content field
In the Anqi CMS, the article detail page is usually displayed through{{archive.Content|safe}}to display 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 do it like this:
{# 移除单页面内容中的<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 specific pages, without modifying the original database content. This greatly simplifies the work of content maintenance and strategy adjustment for display.
WhyremovetagsIs it so practical?
In content operation,removetagsThe filter has multi-dimensional value:
- Maintain the uniformity 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, ensuring the consistency of the website style. - Enhance display tidiness:Remove unnecessary or outdated HTML tags (such as early the}
fontLabel, which can make the page code more concise, load faster, and also avoid layout chaos that may be caused by redundant tags. - Optimize the content management process:The operation personnel does not need to deeply modify the HTML source code, by configuring filters in the template, they can control the final presentation of the content, which improves work efficiency.
In short,removetagsThe filter provides an powerful and precise HTML content processing capability for AutoCMS users.It allows you to cut out the 'unnecessary parts' from the HTML content as precisely as a surgeon, retaining the core structure to better serve the design, user experience, and content management goals of the website.
Common Questions (FAQ)
Q:removetagsFilter can remove attributes from HTML tags, such as<p style="color:red;">ofstyleproperties?
Answer: removetagsWhat attributes are removed by the filterThe entire HTML tag elementIncluding the tag itself, its closing tag, and all attributes within the tag. For example, if you useremovetags:"p", then something like<p style="color:red;">我的文本</p>such a tag will be included withstyleProperties are removed together, leaving only "My Text". It will not remove properties while retaining the tags.
问: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'.
Q:removetagsCan the filter process nested HTML tags? For example, if I remove<i>tags, and it is also contained in another<strong>tag, what will be the result?
Answer: removetagsThe filter will remove all matched specified tags from the content, regardless of whether they are nested within other tags. For example, for the content<strong>这段<i>非常</i>重要的文本</strong>, if used{{ "<strong>这段<i>非常</i>重要的文本</strong>"|removetags:"i"|safe }}Then the output will be:<strong>这段非常重要的文本</strong>.<i>Label and its content will be removed, leaving only the external<strong>Label.