Manage content in Anqi CMS, we often encounter scenarios where we need to handle HTML tags.Whether it is for the purity of content, display requirements, or to ensure safety, flexibly controlling HTML tags is an important ability.striptagsandremovetagsThese two very useful filters help us easily achieve the removal of all HTML tags or only remove specified tags.

Next, we will delve deeper into how these two filters work and how to maximize their value in practical applications.

Remove all HTML tags:striptagsFilter

Imagine that you have a piece of content copied and pasted from an external source, which includes various complex HTML tags, such as<div>/<span>/<p>/<strong>/<i>Even some style tags that should not appear. If these contents are displayed directly, it may destroy the overall layout and style of your website. At this time,striptagsThe filter comes into play.

striptagsThe work of the filter is very direct and thorough: it strips all HTML, XML, and even PHP tags from a string cleanly, including HTML comments.Its goal is to provide the purest text content.

When to usestriptags?

  • Generate content summary or abstract:In the article list page, you may only want to display the plain text summary of the article without any images or complex layouts. UsestriptagsThis can ensure that the generated summary is clean and tidy.
  • Create plain text export:If you need to export content as plain text format, or for RSS feed,striptagsit is an ideal choice.
  • Handle untrusted external content:Although the template engine of AnQi CMS defaults to escaping output to prevent XSS attacks, if your content source is very complex and untrusted,striptagsIt can provide an additional layer of security to ensure that no potential malicious script tags are rendered.

How to usestriptags?

Its usage is very simple, you just need to pass the variables to be processed through the pipe character.|connected tostriptagsthe filter.

Suppose we have a variablearticleContentwith the value<strong><i>Hello 安企CMS!</i></strong><p>这是一个测试段落。</p>.

If you want to remove all HTML tags and keep only plain text, you can write it like this:

{# 假设 articleContent 变量包含 HTML 内容 #}
<div>原文:{{ articleContent|safe }}</div>
<div>清除所有标签后:{{ articleContent|striptags|safe }}</div>

In this example,articleContentAfterstriptagsThe output will be after processingHello 安企CMS!这是一个测试段落。It is worth noting that in order to see the actual effect of the filter removing tags in the browser, we have added both the original output and the processed output.|safeFilter to avoid the default HTML escaping mechanism of the security CMS template engine displaying tags themselves as entity characters.

Precisely remove the specified tags:removetagsFilter

Sometimes, we do not want to remove all HTML tags, but rather keep most of the formatting, and only target some specific tags that we do not want to appear. For example, you may want to keep paragraphs in Englishp和列表ul/li标签,但要移除所有font标签和内联style属性,以确保内容样式受你网站 CSS 的控制。这时,removetags过滤器就能提供这种精细的控制。

removetagsThe filter allows you to specify one or more HTML tag names, which will precisely remove these tags and their content from the string (if the tag is self-closing, like<img>If it is a standalone tag, then only delete the tag itself; if it is a paired tag, such as<strong>, then delete the start and end tags, but keep the text content inside).

When to useremovetags?

  • Standardize the content format:When you import content from different sources and find that some specific HTML tags (such as<span>/<b>/<font>) are misused, leading to inconsistent styles,removetagsit can help you clean them up in bulk.
  • Optimize content semantics:Some outdated HTML tags, such as<i>,May no longer be recommended in terms of semantics. If you want the content to be more in line with modern Web standards, you can remove them while retaining the enclosed text.
  • Clean up external references:If your content often includes references from other websites, it may contain some unnecessary or problematic tags, you can useremovetagsSelective cleaning.

How to useremovetags?

UseremovetagsWhen you need to remove a tag, you should pass the name of the tag to be removed as a parameter to the filter. If there are multiple tags, they can be separated by commas.,Separate them.

Continue to use.articleContentVariable:<strong><i>Hello 安企CMS!</i></strong><p>这是一个测试段落。</p>

  1. Remove a single tag:Suppose we only want to remove.<i>tag, but keep<strong>andpTags:

    <div>移除<i>标签后:{{ articleContent|removetags:"i"|safe }}</div>
    

    The output will be<strong>Hello 安企CMS!</strong><p>这是一个测试段落。</p>

  2. Remove multiple tags:Suppose we want to remove at the same time<i>Tags and<strong>Tags:

    <div>移除<i>和<strong>标签后:{{ articleContent|removetags:"i,strong"|safe }}</div>
    

    The output will beHello 安企CMS! <p>这是一个测试段落。</p>

In this way,removetagsThe filter gives us great flexibility, allowing us to accurately remove unnecessary HTML elements while retaining the required format.

Summary and application scenarios

striptagsandremovetagsIt is a powerful tool for handling HTML tags in the AnQi CMS template engine, but they have different application scenarios.

  • when you needcompletely strip all HTML formattingWhen only plain text is retained, such as when generating article summaries, creating plain text export content, or performing the most stringent purification of content,striptags.
  • when you needSelectively remove specific HTML tags, while preserving other structures and styles, such as standardizing the format of externally imported content, removing old tags that do not conform to semantics, or cleaning up specific distracting elements, chooseremovetags.

In daily content operations, understanding and effectively using these two filters will enable you to manage and display content more efficiently, ensuring the website's tidiness, consistency, and security.


Common Questions (FAQ)

1. Why is it sometimes that when I usestriptagsorremovetagsthe filter, but the browser still displays HTML tags, such as&lt;p&gt;instead of an actual paragraph?

This is usually because the template engine of AnQi CMS defaults to escaping all output content to prevent XSS attacks. When your variable is already an escaped HTML entity (for example, the content is already&lt;p&gt;),or you did not explicitly declare the content to be safe after using the filter (|safe), the template engine will escape it again. In order tostriptagsorremovetagsYou need to ensure that the content you provide to them is the original HTML string, and when displaying in the final output, if you want the browser to render HTML tags instead of displaying entity characters, you need to add extra.|safefilter. For example:{{ archive.Content|removetags:"p"|safe }}.

2. Besides these two filters, what built-in features does AnQi CMS have in content security to prevent malicious HTML code (such as XSS attacks)?

Security is a top priority for AnQi CMS. BesidesstriptagsandremovetagsCan perform content cleaning at the front-end template level, and the system also has multiple built-in security mechanisms.The most basic and important thing is that the template engine of Anqi CMS defaults to automatically escape all HTML tags output from the backend to the frontend.<script>and other malicious tags, they are usually also converted to&lt;script&gt;Unable to execute due to the presence of non-character entities.Additionally, the Anqi CMS backend also provides content security management, sensitive word filtering, and other functions, which can audit and clean content from the source to further reduce security risks.

3. If I want to remove most of the HTML tags but also keep the images<img>and links<a>, what should I do?

In this case,removetagsFilter is your **choice. You can list all the tags you want to remove, but刻意 excludeimgandatags. For example, if you want to removep/strong/iTags, but retain images and links, can be written like this:{{ article.Content|removetags:"p,strong,i,span,div"|safe }}In this way, all tags except those you explicitly specify will be removed,<img>and<a>Labels and their attributes will be preserved. If you need further style or behavior control for images and links,