In AnQiCMS template design, we often encounter situations where we need to clean up or simplify the HTML code in the content.This is not just for aesthetics, but also to ensure the correct display of content, improve page loading efficiency, and even prevent potential security risks.Secure CMS provides two very practical filters for this:striptagsandremovetagsAlthough they are all related to the removal of HTML tags, each has a clear applicable scenario.
striptagsFilter:Completely remove HTML tags and obtain plain text content.
When we want to completely extract the plain text part from a piece of content that contains HTML format,striptagsThe filter is particularly important. Its function is to strip all HTML, XML, and even PHP tags from the string, leaving only the text content, and it will also automatically clear HTML comments.
Application scenarios:
- Generate page meta description (Meta Description) or summary:Search engines usually only crawl plain text as metadata or content summaries for web pages.If the content source contains a large amount of complex HTML structure, directly extracting it may cause garbled text or unnecessary tag display.At this time, use
striptagsCan ensure that the output abstract is clean, concise pure text, which is beneficial for SEO optimization and user experience. For example, it is often used to display the article introduction on the article list page. - Create plain text content preview:In some cases, such as in pop-ups, tooltips, or minimalist views on mobile devices, we may not need any formatting, just to display the skeleton information of the content.
striptagsProvide this plain text view quickly. - Output content to a non-HTML environment:If you need to export the website content to a text file, use it as the plain text part for generating email notifications, or provide plain text data to external systems via an API interface,
striptagsEffectively remove all formats to ensure data compatibility. - Clean up the 'dirty' content of user input:Although the AnQiCMS backend has a strict content filtering mechanism, in certain specific scenarios, if you want to perform an additional, thorough plain text conversion of user input,
striptagsCan also act as an additional defense.
Example:Assumearticle.ContentContains a segment with<h2>/<p>/<strong>Tagged HTML content.
<meta name="description" content="{{ article.Content|striptags|truncatechars:150 }}">
<div class="article-summary">{{ article.Content|striptags|truncatewords:50 }}...</div>
BystriptagsAfter filtering, whether output tometaThe label, whether as an article summary, will be plain text without any HTML format.
removetagsFilter: Precisely remove the specified HTML tags while retaining the required format.
withstriptagsThe "No All" strategy is different,removetagsThe filter provides more refined control capabilities. It allows you to specify one or more HTML tags, and then only remove these specified tags from the content, while retaining all other HTML tags and content that are not mentioned.
Application scenarios:
- Content security filtering:This is
removetagsOne of the most common applications. To prevent cross-site scripting attacks (XSS), we may need to remove the user submitted content of the<script>/<iframe>/<style>Labels that have potential risks but are allowed to be retained<strong>/<em>/<a>Labels that are safe and commonly used. - Unified content style:In different areas of the website, content may need to follow different style guidelines. For example, in the comment section, you may only allow users to use
<strong>and<a>tags, while other like<img>or<h1>Tags need to be removed to keep the layout clean in the comment area. - Content adapts to different display areas:Sometimes, the same content needs to be displayed in different sizes or styles. For example, the main content of a news article may include images and videos, but in the 'Hot Articles' list in the sidebar, we may want to remove all
<img>and<video>Tags, only keep text and links to save space. - Specific format adjustment:If the website's style design does not want some specific HTML tags (such as outdated tags)
<font>Tags or irregular<span>appear at the front,removetagsCan accurately remove them without affecting the normal HTML structure.
Example:AssumeuserComment.ContentThe variable contains user submitted comments, which may include<strong>/<a>, but also has potential<script>and<img>.
<div class="comment-body">
{{ userComment.Content|removetags:"script,style,iframe,img,video"|safe }}
</div>
Here, we removed<script>,<style>,<iframe>,<img>,<video>tags, but retained<strong>and<a>etc., safe and useful tags.|safeThe filter is necessary because it tells the template engine, after passing throughremovetagsThe remaining HTML code is safe and can be output directly as HTML without the need for default escaping.
How to choose: Ask yourself a question.
When deciding to usestriptagsOrremovetagsyou can ask yourself a simple question:I hope the content includes保留HTML format?
- If the answer isNo, I just want plain text, not any HTML tagsThen
striptagsis your first choice. - If the answer isYes, I want to retain some HTML formatting, but delete specific tags that I do not want to appear.Then
removetagsThat is a more suitable choice.
Understanding the core differences and application scenarios of these two filters can help us manage and display the content of the AnQiCMS website more efficiently and safely, making the template output more in line with expectations.
Frequently Asked Questions (FAQ)
Q1: I want to keep the content in<a>the link tags, but remove all other HTML tags, which filter should be used?
A1:In this case,removetagsThe filter is the better choice. You can explicitly specify to remove only<a>All common HTML tags outside of this, but this may be a very long list. A more direct approach is, if you clearly know except for<a>All tags outside should be removed, then you can first usestriptagsCompletely remove all tags to get plain text, then manually convert the URLs in the text to<a>Label (if your business logic allows it, or use suchurlizeauxiliary tools), but this is usually more complex. Usually, a more practical approach is to useremovetagsto clear those that are explicitly knownDon't wantthe label, and make sure you want to keep the label (such as<a>) is notremovetagsin the removal list. For example:{{ article.Content|removetags:"p,div,h1,h2,img,strong,em,script,style"|safe }}.
Q2:striptagsandremovetagsCan these two filters be used together?
A2:Can be used like a pipeline and connected. For example,{{ article.Content|striptags|removetags:"p" }}. However, due tostriptagsall HTML tags have been removed, and thenremovetagsthe operation is usually