In the daily content operation of AnQi CMS, we often encounter situations where we need to handle HTML content.Whether it is to display a plain text summary in a specific scenario, or to standardize content output and enhance security, removing HTML tags is a common requirement.removetagsandstriptagsThey each have unique uses and application scenarios, let's take a closer look at how they can help us efficiently clean up HTML content.
striptagsFilter: One-click to remove all HTML tags
When our goal is to get a pure text without any formatting,striptagsThe filter is our reliable assistant.The function of this filter is very direct—it will remove all HTML and XML tags from a block of HTML content, leaving only the plain text between the tags.It will also explicitly remove HTML comments to ensure the output content is concise.
Imagine that you have published an article with rich images and HTML tags such as paragraphs, images, links, etc.Now, you need to display a brief summary of this content on the homepage or in the search results of the website, and this summary must be plain text without any HTML formatting to ensure uniform layout.striptagsit can be very useful.
The usage is very simple, just pass the content to be processed through the pipe symbol|Pass tostriptagsthe filter.
Example Usage:
Suppose we have a piece of HTML content:<strong><i>Hello!</i></strong>
{# 移除所有HTML标签,并确保输出为HTML内容 #}
{{ "<strong><i>Hello!</i></strong>"|striptags|safe }}
The output of this code will be:Hello!
In actual application,striptagsCommonly used in the following scenarios:
- Generate plain text summaryGenerate a plain text summary for articles, product details, and other content without formatting, for display on list pages, SEO descriptions (meta description), or in-site search results.
- SMS or Email NotificationConvert HTML content to plain text to send in SMS or emails that do not support HTML.
- Clean up user submitted contentEnsure that users enter plain text in input fields that do not require HTML (such as comment titles, message topics) to prevent accidental formatting errors or potential security issues.
removetagsFilter: Precisely remove specified HTML tags
Withstriptagsis different from the "one-size-fits-all" approach.removetagsThe filter provides finer control.It allows us to specify one or more HTML tags, and then only remove these specified tags from the content while retaining other HTML tags that are not mentioned.This means we can selectively retain some formats or structures according to specific needs.
For example, you might want to keep the text in bold (<strong>) and italic (<em>)Effect, but also want to remove all paragraphs (<p>)and hyperlinks (<a>)tags.removetagsThis requirement can be perfectly realized.
When usingremovetagsWhen, we need to pass the name of the tag to be removed as a parameter, separated by a comma.,The separator is passed to the filter.
Example Usage:
Assuming we have some HTML content:.<strong><i>Hello!</i><span>AnQiCMS</span></strong>
Remove a single tag.:
{# 移除<i>标签 #} {{ "<strong><i>Hello!</i></strong>"|removetags:"i"|safe }}The output will be:
<strong>Hello!</strong>Remove multiple tags.:
{# 移除<i>和<span>标签 #} {{ "<strong><i>Hello!</i><span>AnQiCMS</span></strong>"|removetags:"i,span"|safe }}The output will be:
<strong>Hello!</strong>
removetagsThe practical scenarios include:
- Standardize content structure: When content is imported from different sources and is accompanied by various irregular or redundant tags (such as excessive)
divorspannested), it can beremovetagssimplified. - Local Format ControlWhen displaying content, it may be necessary to retain only the emphasis style of the text (such as)
<strong>/<em>), while removing other layout-related tags (such as)table/figure), to adapt to a specific display area. - Basic Security FilteringAlthough it cannot completely replace professional HTML sanitization libraries,
removetagsit can be used to remove common potentially dangerous tags from user input, such as<script>to reduce the risk of XSS (Cross-Site Scripting attacks). For example:{{ user_input|removetags:"script"|safe }}.
choose the appropriate tool:striptagsWithremovetagsconsiderations for actual application
when choosing to usestriptagsOrremovetagsthe key is your requirement for the final content format:
- Choose
striptagsWhen you need absolute plain text output without any HTML formatting.It is suitable for generating a concise preview of content, SEO meta information, or displaying content in environments that do not support HTML. - Choose
removetagsWhen you want to preserve certain formatting or structure of content while removing other unnecessary tags.It is suitable for fine-tuning, condensing, or targeted security filtering of content.
About|safeImportant notes on the filter:It is worth noting that in the above examples, we usuallystriptagsorremovetagsimmediately use after|safeFilter. This is because the template engine of Anqi CMS automatically escapes all HTML content by default for security reasons, to prevent XSS attacks. When we usestriptagsorremovetagsProcessed content, if the output still contains parts we want to be parsed as HTML (such asremovetagsretained tag), or just plain text but we want to tell the template engine that it is already 'safe' content and does not require additional escaping, we need to add|safeMake it clear to the template engine: This content has been processed, it is safe, please parse it directly as HTML and do not perform additional HTML entity escaping. If not|safeEven if the tag is removed, the output may still contain<and>characters may also be escaped as<and>which may affect the final display.
By skillfully applyingstriptagsandremovetagsThese powerful filters enable us to manage the HTML content on the Aiqi CMS website more flexibly and efficiently. Whether it's for aesthetics, compatibility, or security, we can find the appropriate solutions.
Common Questions (FAQ)
1. Why did I usestriptagsorremovetagsafter, the content inside was<or>was still escaped<and>?This is usually because the template engine of AnQi CMS defaults to automatically escaping all output HTML content for security reasons to prevent cross-site scripting (XSS) attacks.Even if you remove the tags, if the remaining content contains HTML special characters, they may also be escaped.striptagsorremovetagsThe processed content (especially the HTML fragments you want the browser to parse) should display as expected, you need to add it to the end of the filter chain.|safe.|safeThe filter tells the template engine that this content is safe and can be output directly as HTML without the need for further escaping.
2.removetagsCan the JavaScript code be removed? For example,<script>Tags?Yes,removetags:"script"Can content be effectively removed?<script>Tag and the JavaScript code inside it.This can be used as a basic means to prevent XSS attacks to some extent.<script>Tags, and may also be injected through HTML attributes (such as)onerror/onloadetc.). Therefore,removetagsAlthough useful, it cannot fully replace professional, more comprehensive HTML content sanitization libraries or backend security filtering mechanisms.For scenarios involving HTML input from users, it is recommended to combine multiple security strategies.
3.striptagsWill it remove HTML entities (such as )?
striptagsThe main goal is to remove HTML tags (such as)<p>/<a>/<div>), it usually does not remove HTML entities (such as representing)&representing&Symbol,)<representing<Symbol).These entities will be retained because they represent a part of the text content, rather than structural or formatting tags.If you need to remove HTML entities from text, you may need to combine with other custom text processing methods or regular expressions to achieve this.