In the daily content operation of Anqi CMS, we often encounter situations where we need to handle HTML content.In order to display plain text summaries in specific scenarios, or to standardize content output and enhance security, removing HTML tags is a common requirement.Aq CMS provides two very practical template filters for this:removetagsandstriptags. They each have unique uses and application scenarios, let's take a closer look at how they help us efficiently clean up HTML content.
striptagsFilter: One-click to clear all HTML tags
When our goal is to obtain a pure text without any formatting,striptagsThe filter is our powerful assistant. The function of this filter is very direct - it removes all HTML and XML tags from a piece of HTML content, leaving only the plain text between the tags.It will explicitly remove HTML comments, ensuring the output content is concise.
Imagine that you have published an article with a lot of paragraphs, images, links, and other HTML tags.Now, you need to display a brief summary of this content on the homepage or in search results, and this summary must be plain text without any HTML formatting to ensure uniform formatting.striptagsIt can be put to use.
Its usage is very simple, just pass the content to be processed through the pipe symbol|pass tostriptagsthe filter.
Usage example:
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 practical applications,striptagsCommonly used in the following scenarios:
- Generate plain text summaryGenerate a brief, unformatted summary for articles, product details, and content, for use on list pages, SEO descriptions (meta description), or in-site search results.
- Text or email notification: Convert HTML content to plain text so that it can be sent as a text message or in an email that does not support HTML.
- Clean up user submitted contentIn some input fields that do not require HTML (such as comment titles, message subjects), make sure that the user enters plain text to prevent unexpected format errors or potential security issues.
removetagsFilter: Precisely remove specified HTML tags
withstriptagsDifferent from the 'one-size-fits-all' approach,removetagsThe filter provides more refined 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 part of the format or structure according to specific needs.
For example, you may want to retain the bold<strong>and italic<em>effects, but at the same time want to remove all paragraphs<p>and hyperlinks<a>) tags.removetagsCan perfectly meet this requirement.
While usingremovetagsWhen, we need to pass the name of the tag to be removed as a parameter, separated by a comma,Separated, and passed to the filter.
Usage example:
Assuming we have a piece of 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>
removetagsPractical scenarios include:
- Standardize content structure: When content is imported from different sources, it may contain various irregular or redundant tags (such as too many
divorspanNested), it can be used toremovetagsSimplify it. - Local format control: When displaying content, it may be necessary to retain only the emphasis style of text (such as
<strong>/<em>), and remove other layout-related tags (such astable/figure),to accommodate a specific display area. - Basic security filtering:Although it cannot completely replace professional HTML sanitization libraries,
removetagsCan 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 right tool:striptagswithremovetagsConsiderations for practical application
When choosing to usestriptagsOrremovetagsIt's all about your requirements for the final content format:
- Select
striptagsWhen you need absolute plain text output without retaining any HTML formatting.It is suitable for generating a concise preview of content, SEO metadata, or displaying content in environments that do not support HTML. - Select
removetags: When you want to retain some specific format or structure of the content while removing other unnecessary tags.It is suitable for fine-tuning, trimming, or targeted security filtering of content.
about|safeImportant notes on the filter:It's worth noting that in the above examples, we usually do it after:striptagsorremovetagsthen use it immediately afterwards:|safeThe filter. This is because the Anqi CMS template engine, for security reasons, defaults to automatically escaping all HTML content to prevent XSS attacks. When we usestriptagsorremovetagsAfter processing the content, if the output still contains parts that we want to be parsed as HTML (such asremovetagsThe label is retained), or it is simply plain text, but we want to tell the template engine that it is already 'safe' content and does not need additional escaping, then we need to add|safeMake sure to explicitly tell 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 tags are removed,<and>characters may also be escaped into<and>affecting the final display.
By mastering the use ofstriptagsandremovetagsThese powerful filters allow us to manage the HTML content on the Anqi CMS website more flexibly and efficiently, whether for aesthetics, compatibility, or security, we can find appropriate solutions.
Frequently Asked Questions (FAQ)
1. Why did I usestriptagsorremovetagsAfter, the content in<or>was escaped into<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.To ensure that after passing throughstriptagsorremovetagsThe processed content (especially those HTML fragments you want to be displayed as intended by the browser) should be displayed as expected, you need to add it at the end of the filter chain|safe.|safeThe filter tells the template engine that this content is safe and can be directly output as HTML without escaping.
2.removetagsCan you remove the JavaScript code? For example<script>Tag?Yes,removetags:"script"Can you effectively remove the content within<script>Tag and the JavaScript code inside it.This can be used as a primary means to prevent XSS attacks to some extent.However, it is important to note that XSS attacks come in many forms, not limited to<script>Tags, and may also be injected through HTML attributes (such asonerror/onloadetc). Therefore,removetagsAlthough useful, it cannot completely replace professional, more comprehensive HTML content purification libraries or backend security filtering mechanisms.For scenarios involving HTML in user input, it is recommended to combine multi-layer 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 non-breaking spaces,&Indicates&Symbol,<Indicates<Symbols). These entities are retained because they represent part of the text content, rather than structure or format tags.If you need to remove HTML entities from text, you may need to combine other custom text processing methods or regular expressions to achieve this.