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 to prevent potential security risks.striptagsandremovetagsAlthough they are all related to the removal of HTML tags, each has a clear application scenario.

striptagsFilter: Thoroughly remove HTML tags to obtain plain text content

When we want to completely extract the plain text part from a piece of content that includes HTML formatting,striptagsThe filter becomes 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 remove HTML comments.

Applicable scenarios:

  1. Generate page meta description (Meta Description) or summary:Search engines usually only crawl plain text as the meta description or content summary of a web page.If the content source has a large amount of complex HTML structure, directly extracting it may cause garbled text or unnecessary tags to be displayed.striptagsIt can ensure that the output abstract is clean, concise, and pure text, which is beneficial for SEO optimization and user experience. For example, it is often used to display article summaries on article list pages.
  2. Create a plain text content preview:In certain cases, such as in pop-ups, tooltips, or minimalist views on mobile devices, we may not need any formatting, just hoping to display the skeletal information of the content.striptagsCan quickly provide this plain text view.
  3. Content output to non-HTML environment:If you need to export website content to a text file, the plain text part for generating email notifications, or provide plain text data to external systems via an API interface,striptagsCan effectively remove all formats and ensure data compatibility.
  4. Clean up the "dirty" content of user input:Although 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 line of defense.

Example:Assumearticle.ContentThe variable contains a segment of HTML content with<h2>/<p>/<strong>tags.

<meta name="description" content="{{ article.Content|striptags|truncatechars:150 }}">

<div class="article-summary">{{ article.Content|striptags|truncatewords:50 }}...</div>

PassstriptagsAfter filtering, whether output tometaLabels or as article abstracts, they will be plain text without any HTML formatting.

removetagsFilter: Precisely remove specified HTML tags, retaining the desired format.

WithstriptagsThe 'all-don't' 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 were not mentioned.

Applicable scenarios:

  1. Content security filtering:This isremovetagsOne of the most common applications. To prevent cross-site scripting attacks (XSS), we may need to remove the user submitted content that contains<script>/<iframe>/<style>Tags with potential risks, but at the same time allowing retention<strong>/<em>/<a>Tags that are safe and commonly used.
  2. Unified content style:In different areas of the website, content may need to follow different style guidelines. For example, in the comment section, you might only allow users to use<strong>and<a>tags, while other like<img>or<h1>Tags such as this need to be removed to maintain the tidy layout of the comment section.
  3. Content adaptation for 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 contain images and videos, but in the 'Popular Articles' list in the sidebar, we might want to remove all<img>and<video>Tags, keep only text and links to save space.
  4. Specific format adjustment:If the website's style design does not want to display certain specific HTML tags (such as outdated,<font>Label or irregular<span>appears at the front,removetagscan accurately remove them without affecting other normal HTML structures.

Example:AssumeuserComment.ContentComments submitted by users may contain variables, possibly including<strong>/<a>However, there may be potential<script>and<img>.

<div class="comment-body">
    {{ userComment.Content|removetags:"script,style,iframe,img,video"|safe }}
</div>

Here, we have removed<script>,<style>,<iframe>,<img>,<video>tags, but we have retained<strong>and<a>such as safe and useful tags.|safeThe filter is necessary because it tells the template engine, afterremovetagsThe remaining HTML code after processing is safe and can be directly output 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 only want plain text, not any HTML tagsThenstriptagsis your preference.
  • If the answer isYes, I want to retain some HTML formatting, but remove specific tags that I do not want to appearThenremovetagsis the more appropriate choice.

Understanding the core differences and application scenarios of these 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.


Common Questions (FAQ)

Q1: I want to retain the content of the item,<a>remove all other HTML tags, which filter should I use?

A1:In this case,removetagsFilter is the better choice. You can explicitly specify to remove only<a>All common HTML tags, but this may be a very long list. A more direct approach is, if you clearly know except for<a>All other tags should be removed, then you can first usestriptagsCompletely remove all tags to get plain text, and then manually convert the URLs in the text to<a>Label (if your business logic allows it, or use tools likeurlize), but this is usually more complex. Usually, a more practical approach is to useremovetagsto clear those that are explicitly knownDon't wantlabel, and make sure you want to keep the labels (such as<a>) 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 concatenated. For example,{{ article.Content|striptags|removetags:"p" }}However,striptagsafter all HTML tags have been removed,removetagsoperations on it are usually