How to completely remove all tags from HTML content in the AnQiCMS template?

Calendar 👁️ 76

In website content operation, we often encounter such needs: to extract plain text from richly formatted content, such as displaying abstracts on article list pages, or providing clean text for SEO meta description tags.AnQiCMS (AnQiCMS) provides a powerful and flexible template engine, through built-in filters (filters), we can easily remove tags from HTML content completely.

Understanding the content processing in AnQiCMS template

AnQiCMS templates use a syntax similar to Django, content variables are passed through double curly braces{{ 变量 }}Output. When we use a rich text editor to publish articles or pages in the background, the content is usually stored and presented in HTML format. This means that it is output directly{{ archive.Content }}May appear in expected plain text scenarios<p>/<strong>/<img>and HTML tags, thereby destroying page layout or SEO effects.

幸运的是,AnQiCMS's template engine has an automatic escaping feature, which to some extent prevents the direct execution of malicious HTML code. But for the need to completely remove HTML tags to obtain plain text, we need to use specific filters.

Core Tool:striptagsFilter

To completely remove all tags from the HTML content, the most direct and effective method is to usestriptagsa filter. This filter will remove all HTML tags from the input string (including<p>/<a>/<img>Remove all (including) and leave only plain text content.

Usage:

In the template, you can pass the content variables to be processed through the pipe symbol|pass tostriptagsfilters. For example, if your article content is stored inarchive.ContentIn:

{{ archive.Content|striptags|safe }}

Here|safeFilters are usually used afterstriptagsto ensure that the cleaned-up plain text content is not escaped again when output (even though the content is already plain text, adding|safeIt can explicitly tell the template engine that this is a safe output that does not require additional escaping).

Example:

Assumearchive.Contentis:

<p>这是一段包含<strong>加粗</strong>和<a href="#">链接</a>的文本。</p>

Use{{ archive.Content|striptags|safe }}Then, the output will be:

这是一段包含加粗和链接的文本。

Precise control:removetagsFilter

Sometimes, we may not want to remove all HTML tags, but only want to remove specific ones, such as only removing image tags<img>but keeping paragraphs<p>and links<a>. At this point,removetagsThe filter comes into play.

Usage:

removetagsThe filter requires a parameter, which is the name of the HTML tag you want to remove. If you need to remove multiple tags, the tag names should be separated by English commas.,Separated.

{{ archive.Content|removetags:"img,script"|safe }}

Example:

Assumearchive.Contentis:

<p>这是一段包含<strong>加粗</strong>、<img src="image.jpg" alt="图片">和<a href="#">链接</a>的文本。</p>
  • If you only want to remove<i>Tags:

    {{ "<strong><i>Hello!</i></strong>"|removetags:"i"|safe }}
    

    The output will be:

    <strong>Hello!</strong>
    
  • If you only want to removeimgTags:

    {{ archive.Content|removetags:"img"|safe }}
    

    The output will be:

    <p>这是一段包含<strong>加粗</strong>、和<a href="#">链接</a>的文本。</p>
    
  • If you want to removeimgandaTags:

    {{ archive.Content|removetags:"img,a"|safe }}
    

    The output will be:

    <p>这是一段包含<strong>加粗</strong>、和链接的文本。</p>
    

Application scenarios in practice

  • List page summary display:On the article list or product list page, we usually only display the summary of the content. In order to avoid chaotic HTML tags in the summary, we canitem.Descriptionoritem.ContentapplystriptagsFilter. CombinetruncatecharsortruncatewordsFilter, can generate a suitable plain text summary.

    {# 结合截断功能,生成100个字符的纯文本摘要 #}
    {{ item.Description|striptags|truncatechars:100|safe }}
    
  • SEO Meta Description:Search Engine Description Tag<meta name="description" content="...">Should only contain plain text. You can use the description field of the page tostriptagsensure this, avoiding unnecessary HTML code from being indexed by search engines.

    <meta name="description" content="{% tdk seoDescription with name="Description" %}{{ seoDescription|striptags|safe }}">
    
  • specific layout requirements:When you need to embed some content as plain text in JS scripts, CSS styles, or other strict plain text environments,striptagsit is also an indispensable tool.

Summary

AnQiCMS'striptagsandremovetagsThe filter provides a concise and powerful solution for handling HTML tags in templates.Whether it is necessary to thoroughly clean all tags to obtain plain text, or only selectively remove some tags, these tools can help you better control the presentation of content, optimize the display effect of the page, and improve SEO performance.Proficiently using these filters can make your AnQiCMS website content management more efficient and flexible.


Frequently Asked Questions (FAQ)

  1. striptagsandremovetagsWhat are the main differences of the filter? striptagsThe filter will remove HTML tags from the contentalland retain only plain text. On the other handremovetagsThe filter allows you to specify one or more HTML tag names, such as"img,a,script"It will only remove the specified tags while retaining the other unspecified tags. The choice depends on how much of the original format you want to keep.

  2. I used it on the list page.striptagsFilter, but the summary looks very long and is not automatically truncated, why is that? striptagsThe filter only removes HTML tags and does not change the actual character length of the content. If you want the summary content to be the specified length after removing the tags, you need to use in combinationtruncatecharsCharacter truncate ortruncatewordsWord truncate filter. For example:{{ item.Description|striptags|truncatechars:100|safe }}First remove HTML tags, then truncate the plain text content to a maximum of 100 characters.

  3. If my content is in Markdown format in the background, will it be rendered into HTML first before being processed by the filter?Yes, if your AnQiCMS backend has enabled the Markdown editor and the content field (such asarchive.Content)Configure to support Markdown rendering, then the template engine will first convert the Markdown content to HTML. Then,striptags

Related articles

How to batch replace keywords or links in the entire site's content and display them immediately?

In website operation, we often encounter situations where we need to adjust a large amount of content, whether it is to adapt to new brand strategies, unify website terminology, fix broken links, or better optimize SEO, these all require batch updating of keywords or links throughout the entire site.Manually modifying one by one not only takes time and effort, but is also prone to errors.The AnqiCMS provides a very practical feature that allows you to easily deal with such challenges: batch replacement of keywords or links in the entire site content, and ensure that changes take effect immediately.This ability is crucial for the daily operation and maintenance of the website

2025-11-08

How to use the pseudo-static function of AnQi CMS to optimize URL display and improve SEO effect?

In website operation, the URL (Uniform Resource Locator) plays a vital role.A clear, meaningful, and easy-to-understand URL not only improves user experience but is also an indispensable part of search engine optimization (SEO).The traditional dynamic URL often contains parameters and numbers that are difficult to understand, while the pseudo-static URL, through rewriting technology, makes it look as simple and standardized as a static HTML page, thus gaining the favor of search engines and users.AnQiCMS (AnQiCMS) knows the impact of URL structure on website SEO

2025-11-08

How to set up document thumbnails and control their display on list pages?

In a content management system, a striking thumbnail can greatly enhance the attractiveness of the content, whether it is in an article list, product display, or other content aggregation pages, it is an important visual element to guide users to click.AnQiCMS (AnQiCMS) is well aware of this and provides flexible thumbnail settings and display control features to help optimize the visual effects and user experience of your website.Next, we will discuss in detail how to set the thumbnail of the document in Anqi CMS and master its various display methods on the list page.### The First Part

2025-11-08

How to configure a custom display template for the document detail page?

Manage website content in AnQi CMS, flexibility is one of its core advantages.When you need to present a unique content layout or style for a specific document detail page, the system provides powerful custom template functionality.This means that you can configure a dedicated display template for the document based on its type, content, and even specific document requirements, thus breaking away from the one-size-fits-all approach of traditional CMS and creating pages with greater appeal and functionality.The reason why AnQi CMS can achieve this is due to its 'flexible content model' design and the continuous enhancement of template customization capabilities in system iterations

2025-11-08

Only delete the specified HTML tags in AnQiCMS article content, which filter should be used?

When using AnQiCMS for content operations, we often encounter situations where we need to refine the processing of HTML tags in article content.For example, during the import or copy and paste process, some unnecessary, format-inconsistent tags may be brought in, which may affect the 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

2025-11-08

How to safely truncate a long text containing HTML tags in AnQiCMS template?

In AnQiCMS template design, we often encounter scenarios where it is necessary to display long text content, such as the abstract on the article list page, and the brief introduction of product details.If this long text content is displayed directly, it may cause the page to be long, affecting user experience and layout aesthetics.Therefore, truncating long text is a common requirement.However, when long text content contains HTML tags, simple character truncation may cause problems.For example, a content like this `\u003cp\u003eThis is an important\u003cb\u003e text\u003c/b\u003e\u003c/p\u003e`

2025-11-08

How to avoid AnQiCMS from destroying the tag structure when truncating HTML text?

In content management, in order to maintain the neatness and loading efficiency of the website page, we often need to truncate articles, product descriptions, or other long text content, and only display part of the abstract.However, if the content itself contains HTML tags, simple character truncation often breaks the structure of these tags, causing the page display to become chaotic and even affecting the layout and functionality of the website.For example, the content of a `<p>This is an <b>important</b> paragraph</p>`, if it is simply truncated to `<p>This is an <b>important</b`

2025-11-08

How does AnQiCMS render Markdown formatted article content to HTML?

AnQiCMS boasts its efficient content management capabilities, is favored by users, especially in handling text content, providing flexible and diverse options.For users accustomed to using Markdown format, AnQiCMS also provides comprehensive support, able to seamlessly render Markdown formatted article content into user-friendly HTML pages.To understand how AnQiCMS achieves this transformation, we can explore from three aspects: content creation, system configuration, and template rendering.

2025-11-08