How to remove HTML tags in AnQiCMS template and display only plain text content?

Calendar 👁️ 74

In AnQiCMS content management practice, we sometimes encounter such needs: to extract pure text information from document content containing rich formats (such as bold, italic, images, links, etc.).This may sound contradictory, content management systems are committed to the diverse display of content, why do we still 'strip' these formats?However, in many specific scenarios, displaying only plain text content can play a crucial role, such as generating concise summaries for articles, optimizing search engine meta descriptions (Meta Description), providing unified and fresh content previews on list pages, or importing content into platforms that do not support HTML formats.

So, how can we efficiently achieve this goal in the flexible template system of AnQiCMS? AnQiCMS provides powerful filters, among whichstriptagsandremovetagsIt is the tool to solve this problem.

Get to know the core tools:striptagsFilter

striptagsThe filter is a very practical feature in AnQiCMS templates, its function is exactly as the name suggests——“Tag stripping”. When you want to remove all HTML or XML tags from a piece of content containing HTML tags all at once, leaving only the text, striptagsit can be put to use.

Its usage is very intuitive, just append the pipe symbol to the variable you want to process|连接striptagsFor example, if you have a variable namedarchive.ContentThe variable stores the article body with HTML formatting, you can retrieve its plain text content in this way:

{{ archive.Content | striptags }}

This simple code will traversearchive.ContentRemove all content<div>/<p>/<a>/<img>And HTML tags, finally only output the visible text.

Scenes for processing Markdown content

It is noteworthy that the AnQiCMS backend may have enabled a Markdown editor when editing document content. In this case,archive.ContentThe variable may contain Markdown formatted text, rather than direct HTML. If Markdown text is used directlystriptagsThe result may not be satisfactory because it cannot recognize Markdown syntax and convert it to the corresponding plain text.

At this point, we need to first userenderA filter that renders Markdown text into HTML and then uses itstriptagsRemove HTML tags.renderThe filter can correctly convert Markdown syntax into HTML structures recognizable by browsers. Therefore, the complete processing process will be as follows:

{# 假设 archive.Content 变量中存储的是 Markdown 格式的内容 #}
{{ archive.Content | render | striptags }}

ByrenderFilter, Markdown content is converted to HTML, thenstriptagsRemove these HTML tags to ensure the final output is plain text. It should be noted that,renderThe filter outputs HTML, if displayed directly on the page, in order to avoid the browser escaping HTML tags and causing HTML tags to be displayed directly, you need torenderAfter the filter is addedsafeThe filter. But withstriptagsWhen used together, due tostriptagsUltimately, it will remove all HTML, sosafeIt is not necessary, because the final result it processes is plain text.

Flexible control:removetagsFilter

Sometimes, our needs may be more refined: we do not want to remove all HTML tags, but only want to remove specific ones, while keeping other tags (such as, we want to keep<a>Label so that users can click on the link but remove all images<img>Or paragraph<p>Label). At this point,removetagsThe filter is particularly powerful.

removetagsThe filter allows you to specify one or more HTML tags to remove.You just need to provide a comma-separated list of tag names after the filter.For example, if you want to remove<i>and<span>Label, but keep all other content, and you can write it like this:

{# 移除 <i> 和 <span> 标签,保留其他所有标签 #}
{{ "<strong><i>Hello!</i><span>AnQiCMS</span></strong>" | removetags:"i,span" }}

This code will output<strong>Hello!AnQiCMS</strong>It can be seen<i>and<span>the tags have been removed, and<strong>Labels are retained. This fine control provides great flexibility in specific scenarios of content display.

Combined with the excerpt function, it generates a plain text summary.

When generating article abstracts or summaries, we need not only plain text but also control its length. AnQiCMS providestruncatecharsandtruncatewordsFilters that can automatically add ellipses while truncating strings (...) when used in conjunction withstriptagscan easily generate pure text summaries that meet the required specifications:

{# 获取纯文本内容,并截取前100个字符作为摘要 #}
<p>{{ archive.Content | render | striptags | truncatechars:100 }}</p>

{# 或者,按单词数量截取 #}
<p>{{ archive.Content | render | striptags | truncatewords:30 }}</p>

Please note,truncatecharsIt will truncate by character count (including one Chinese character), andtruncatewordsIt will truncate by word count. Choose the appropriate truncation method based on your specific needs and content characteristics.

Practical suggestions

Remove HTML tags in the AnQiCMS template and display only plain text content, mainly aroundstriptagsandremovetagsTwo filters expanded. In practical applications, you need:

  1. Determine the source of the content: Judgearchive.ContentThe content stored in the variables is pure HTML or Markdown. If it is Markdown, be sure to use it first.renderthe filter to convert.
  2. Choose the appropriate filterRemove all tags if necessarystriptagsOr remove only some tagsremovetagsChoose the most suitable filter.
  3. Consider the length of the summaryIf used to generate a summary, combinetruncatecharsortruncatewordsEnsure the output content is concise.
  4. SEO OptimizationIn<meta name="description" content="...">Used in tagsstriptagsEnsure the output is plain text, which is friendly to search engines.

These filters provided by AnQiCMS allow template designers to flexibly control the way content is presented, meeting various needs from full rich text display to concise plain text output, thereby building websites with more expressive and functional features.


Frequently Asked Questions (FAQ)

1.striptagsandremovetagsWhat are the main differences of the filter?

striptagsThe filter will remove all detected HTML and XML tags, leaving no room, and directly output plain text. AndremovetagsThe filter provides finer control, allowing you to specify one or more specific HTML tags to remove (such as<img>/<p>),while other unspecified HTML tags will be retained in the content.Choose which filter to use depends on whether you want to remove all formats completely or selectively retain some formats.

2. How to ensure the appropriate length of plain text content when generating an article summary?

After obtaining the plain text content, it can be combined withtruncatecharsortruncatewordsa filter to control the length. First, userender(If the content is Markdown) andstriptagsRemove HTML tags and then apply the truncation filter. For example,{{ archive.Content | render | striptags | truncatechars:150 }}The content will be converted to plain text, then the first 150 characters will be truncated and an ellipsis will be added.truncatewordsThen it will be truncated by word count.

3. UsestriptagsWill filtering output plain text content affect the website's search engine optimization (SEO)?

This depends on where you use plain text. In some cases, using plain text is beneficial for SEO. For example, websites<meta name="description">The tag should only contain plain text because search engines usually only crawl and display plain text descriptions.When displaying article summaries on the list page, plain text also helps search engines understand the content faster.Please note that you should not convert all the main content into plain text, as search engines also need to parse HTML structure to understand the page layout and content focus.**Practice is to use plain text in areas that require concise and unformatted display, while maintaining rich HTML formatting in the main content area.

Related articles

How does AnQiCMS's `urlizetrunc` filter truncate long URLs and display an ellipsis in the link?

The AnQiCMS `urlizetrunc` filter: Allows long URLs to elegantly transform into ellipses In daily website operations, we often need to reference external links in articles, comments, or product descriptions.These links may be intended to provide references, guide users to access related pages, or display product videos, etc.However, sometimes these URLs can be very long, not only occupying a large amount of page space, affecting the overall layout aesthetics, but also reducing the reading experience of the content.

2025-11-09

How to implement custom JSON-LD structured data in AnQiCMS templates to optimize search results display?

In today's digital marketing environment, having high-quality content is not enough. How to make search engines better understand and display this content is becoming increasingly important.Structured data, especially JSON-LD, is a powerful tool at our disposal that can help our website present more rich and attractive forms in search results, which is what we often call 'rich snippets'.

2025-11-09

How to format a `time.Time` type variable into a readable date string in GoLang in AnQiCMS?

In AnQiCMS, managing website content often involves the need to display dates and times, such as the publication time of articles, update time, or the last login time of users, etc.These time data are typically stored as `time.Time` type in the GoLang backend.When they are passed to the front-end template, if not properly formatted, they may be displayed as a string of hard-to-understand timestamps or the default Go language format, which is obviously not in line with the needs of the visitors we show.

2025-11-09

How to judge whether a string contains a certain keyword in AnQiCMS template and display the result?

In website operation, we often encounter such needs: dynamically adjusting the display mode of content based on specific information of web page content.For example, if an article title contains keywords such as 'Promotion' or 'Activity', we may want it to display an eye-catching label automatically; or, in order to review content and improve user experience, we need to check if there are certain sensitive words in the article and take appropriate actions accordingly.

2025-11-09

How to automatically identify URLs in text and convert them to clickable `<a>` tags in AnQiCMS?

In content management and website operations, we often need to convert URLs in text into clickable hyperlinks so that users can directly navigate to access.Manual operations are not only inefficient but also prone to omissions, especially on websites with large amounts of content.AnQi CMS understands this pain point and provides an elegant and efficient solution that allows us to easily identify and convert URLs in text.To make Anq CMS automatically identify URLs in text and convert them into clickable `<a>` tags, we mainly use its powerful template filter function.Specifically

2025-11-09

What is the core function of the `urlize` filter in the AnQiCMS template?

In AnQiCMS template development, handling links in content is a common and important link.The `urlize` filter was born to meet this need, it can greatly simplify the recognition and formatting of links in templates, while also taking into account user experience and search engine optimization.### `urlize` filter is the core function in AnQiCMS templates The core function of the `urlize` filter is to intelligently identify URLs and email addresses contained in a piece of plain text content

2025-11-09

How to parse a URL string into a clickable link in the article detail content of AnQiCMS?

In daily website content operations, we often need to display text content containing URL addresses on article detail pages, such as reference sources, recommended links, and so on.If these URL strings cannot be automatically parsed into clickable hyperlinks, users will not be able to jump directly, which not only affects the user experience but also reduces the readability and convenience of the content.

2025-11-09

Does the `urlize` filter support converting email addresses (such as `[email protected]`) into `mailto:` links?

When managing website content, we often need to convert URLs or email addresses in text to clickable links.Manually adding HTML tags one by one is time-consuming and prone to errors, especially when dealing with large amounts of content.Therefore, whether an efficient content management system can provide intelligent link parsing functionality is crucial for content operation efficiency.AnQiCMS as a content management system designed specifically for small and medium-sized enterprises and content operation teams, understands the importance of such needs.It has built-in many utility tools in the template engine to simplify this work, including `urlize`

2025-11-09