In AnQi CMS template design, in order to better control the display of content, the system provides a variety of flexible filters (Filter).These filters can help you perform various string processing operations when outputting variables, such as removing specific characters or HTML tags, making the content neater and conforming to your display requirements.Today, we will focus oncutandremovetagsThese practical filters.

Anqi CMS template filter overview

The Anqi CMS template system adopts the syntax of the Django template engine, allowing you to convert and process variables in a concise way. The basic syntax of the template filter is{{ 变量 | 过滤器名称 : 参数 }}. A variable is the data you want to process, the filter name is the operation you want to perform, and the parameters are the additional information passed to the filter.

Understand this basic structure and then we can delve deepercutandremovetagsThese are specifically used for content cleaning filters.

Remove specific characters:cutFilter

In content management, sometimes we need to remove some unnecessary characters from strings, such as extra spaces, special symbols, or even specific segments of text. At this point,cutThe filter is very useful.

cutThe filter's role is to remove all occurrences of the specified characters from a string at any position.It will search for the "keyword" parameter you provided and remove all matching items from the original string.

Usage:

{{ obj|cut:"关键词" }}

Among themobjThis is the string variable you need to process,"关键词"This is the character or string fragment you want to remove.

Actual case:

Assuming you have a variableproductNameThe value is" 安企CMS 内容管理系统 "And you want to remove extra spaces at the beginning, end, and in the middle of the string:

{# 移除字符串中的所有空格 #}
{% set productName = " 安企CMS 内容管理系统 " %}
<p>原始字符串:{{ productName }}</p>
<p>移除所有空格后:{{ productName|cut:" " }}</p>

{# 移除字符串中的特定字符 #}
{% set serialNumber = "SN-2023-A1B2-C3D4" %}
<p>原始序列号:{{ serialNumber }}</p>
<p>移除横线后:{{ serialNumber|cut:"-" }}</p>

{# 移除数字中的特定数字 #}
{% set price = 12500 %}
<p>原始价格:{{ price }}</p>
<p>移除数字 "5" 后:{{ price|cut:"5" }}</p>

Output Result:

原始字符串: 安企CMS 内容管理系统 
移除所有空格后:安企CMS内容管理系统

原始序列号:SN-2023-A1B2-C3D4
移除横线后:SN2023A1B2C3D4

原始价格:12500
移除数字 "5" 后:1200

cutThe filter is very efficient when it needs to standardize text or prepare data for a specific display format (such as removing delimiters to merge for display).

Remove HTML tags:removetagsFilter

When displaying content, especially when the content comes from a rich text editor or external import, you may want to remove certain HTML tags to ensure consistency in page style, improve SEO, or extract plain text content.removetagsThe filter is exactly for this purpose.

removetagsThe filter can remove the HTML tags you specify explicitly from the string. And it is with removing all HTML tags.striptagsThe filter is different,removetagsProvided finer control, you can optionally retain some tags while removing others.

Usage:

{{ obj|removetags:"标签1,标签2,标签3"|safe }}

Among themobjIs a string variable containing HTML content,"标签1,标签2,标签3"Is one or more HTML tag names you want to remove (without brackets), separated by English commas,Separated.

Important reminder:due toremovetagsThe content after the filter may still be an HTML fragment (because it only removed the tags you specified, and other tags may remain), so it should be output inIt is strongly recommended to use in conjunction with|safeFilter.|safeThe filter tells the template engine that this string is safe HTML content and does not need to be automatically escaped.This ensures that your HTML tags are correctly parsed and displayed by the browser, rather than being output as plain text.

Actual case:

Assuming you have a variablearticleContentContaining the following HTML content:

{% set articleContent = "<h3>这是一个**粗体**标题</h3><p>安企CMS是一个<i>高效</i>的内容管理系统,它提供了<b>强大的功能</b>。</p>" %}

<p>原始内容:{{ articleContent|safe }}</p>

{# 移除 h3 标签 #}
<p>移除 h3 标签后:{{ articleContent|removetags:"h3"|safe }}</p>

{# 移除 i 和 b 标签 #}
<p>移除 i 和 b 标签后:{{ articleContent|removetags:"i,b"|safe }}</p>

{# 移除所有段落和粗体标签 #}
{% set richText = "<p>请<b>访问</b> <a href='#'>安企CMS官网</a> 获取更多信息。</p>" %}
<p>原始内容:{{ richText|safe }}</p>
<p>移除 p 和 b 标签后:{{ richText|removetags:"p,b"|safe }}</p>

Output Result:

原始内容:<h3>这是一个**粗体**标题</h3><p>安企CMS是一个<i>高效</i>的内容管理系统,它提供了<b>强大的功能</b>。</p>

移除 h3 标签后:这是一个**粗体**标题<p>安企CMS是一个<i>高效</i>的内容管理系统,它提供了<b>强大的功能</b>。</p>

移除 i 和 b 标签后:<h3>这是一个**粗体**标题</h3><p>安企CMS是一个高效的内容管理系统,它提供了强大的功能。</p>

原始内容:<p>请<b>访问</b> <a href="#">安企CMS官网</a> 获取更多信息。</p>
移除 p 和 b 标签后:请访问 <a href="#">安企CMS官网</a> 获取更多信息。

ByremovetagsYou can flexibly control which HTML elements should be displayed in a specific context, and which should be cleaned up.

When do you need to use these filters?

These filters have a wide range of application scenarios in the content operation and template design of Anqi CMS:

  1. SEO optimizationRemove all HTML tags when generating a concise Meta Description or Meta Keywords to ensure that search engines capture a pure text summary.
  2. Content Summary/PreviewWhen displaying article summaries on article list pages or homepages, it is usually desired to show plain text to avoid layout chaos or style conflicts caused by HTML tags.
  3. Data cleaningRemove any non-standard characters from user input, external APIs, or collected content to standardize data formats.
  4. Interface outputWhen providing content data to other systems via API, it may be necessary to provide a plain text version without any HTML tags.
  5. Security: AlthoughremovetagsCan be used to remove some potentially dangerous HTML tags, but if you need to completely remove all unknown HTML tags to prevent XSS attacks, usestriptagsThe filter (removing all HTML tags) will be safer.removetagsMore suitable for known and need precise control scenarios.
  6. UI consistencyMandatory to display the content of certain areas as plain text to ensure consistency and aesthetics of the user interface.

Proficient in usingcutandremovetagsThis filter allows you to manage and display content more efficiently in Anqi CMS, enhancing the website's user experience and data quality.


Frequently Asked Questions (FAQ)

Q1:removetagsandstriptagsWhat is the difference? Which one should I choose?

A1: removetagsThe filter is used to remove youspecifiedone or more HTML tags. For example,{{ content|removetags:"p,div"|safe }}it will only remove<p>and<div>tags, while retaining other such as<a>/<strong>Tags

Andstriptagsfilter will removeallHTML tags, converting HTML content to plain text completely. For example,{{ content|striptags }}Will remove all HTML tags (including<a>/<strong>etc.)

which one depends on your needs. If you need to control exactly which tags to remove, please useremovetagsIf you just want to get plain text content, do not