When using AnQiCMS for content operations, we often encounter situations where we need to refine the processing of HTML tags in the article content.For example, during the import or paste process, some unnecessary, format-inconsistent tags may be introduced, which may affect page layout, SEO performance, and even introduce security risks.In order to meet the requirement of only deleting the specified HTML tags from the content of AnQiCMS articles, 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:striptagsandremovetagsThey each have their own focus, understanding the differences is the key to choosing the correct 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 retain plain text, thenstriptagsIt is the ideal choice. For example, it will<strong><i>Hello!</i></strong>afterstriptagsAfter processing, only the following will remainHello!.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>, links<a>), while precisely removing unnecessary and interfering tags (such as uncommon style tags, script tags, etc.).
Therefore, to only delete the specified HTML tags from the content of AnQiCMS articles, you 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"This is a string containing the HTML tag names you want to remove, separated by English commas.,The tag names do not need to include angle brackets (<and>),just write the tag name itself (for example,"div,span,font")
A note worthy of attention:removetagsFilters usually need to be used with|safeFilter combined use.
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 raw HTML, it will be converted to entity characters (such as,<becomes<), rather than being parsed as HTML.
While usingremovetagsAfter the filter has processed, the specified tags in the content have been removed, but they are still considered as strings that may contain HTML. If at this point, you do not add|safeThe filter, 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 essential toremovetagsthen add|safe.
Let's understand through some specific examples:
Example one: Delete a single HTML tag
Suppose your article content fieldarchive.ContentContains the following HTML:
<p>这是一段 <b>重要的</b> 内容,但这里有一个不必要的 <i>斜体</i> 标签。</p>
If you want to delete all of them<i>Tags:
{{ archive.Content|removetags:"i"|safe }}
The result will be:
<p>这是一段 <b>重要的</b> 内容,但这里有一个不必要的 斜体 标签。</p>
Example two: Delete multiple HTML tags
Suppose there are many in the article content<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 result will be:
<p>这是一段内容。</p>不想要的文本。
In this way, you can flexibly control the display of HTML tags in the article content, ensuring that the content is presented in the most consistent with your expectations on the website, thereby enhancing the quality of the content and the user experience.
Frequently Asked Questions (FAQ)
Q1: If I want to remove all HTML tags from the article content instead of just a few, 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 usestriptagsa filter. For example:{{ archive.Content|striptags }}This filter does not require a tag name, it will automatically identify and remove all HTML structures.
Q2: Why is it used inremovetagsorstriptagsPlus after the filter,|safeFilter?A2:|safeThe filter's role is to inform the template engine that the content processed by the previous filter is 'safe' and does not require further HTML entity encoding. If there is no|safeEven if you remove the unnecessary tags, the remaining valid HTML tags (such as<p>/<a>) may also be escaped into<p>/<a>It causes them to be displayed on the page as plain text code instead of being rendered by the browser. Therefore,|safeIt ensures that the content is correctly parsed as HTML after processing.
Q3:removetagsCan the filter be used to remove plain text from the text, rather than HTML tags?A3:removetagsThe filter is specifically designed to handle HTML tags. If you want to delete a certain plain string from the article content (for example, the word "AnQi CMS"), or replace a word, you should usecutFilter (delete) orreplaceFilter (replace). For example:{{ archive.Content|cut:"安企CMS" }}It will delete all "AnQi CMS" strings, but{{ archive.Content|replace:"旧词,新词" }}it will replace them.