In the template design of AnQi CMS, 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 on output variables, such as removing specific characters or HTML tags, making the content cleaner and more in line with your display requirements.cutandremovetagsThese practical filters.
Anqi CMS template filter overview
The template system of Anqi CMS borrows the syntax of the Django template engine, allowing you to transform and process variables in a concise manner. The basic syntax of template filters is{{ 变量 | 过滤器名称 : 参数 }}The variable is the data you need to process, the filter name is the operation you want to perform, and the parameters are additional information passed to the filter.
After understanding this basic structure, we can delve deeper intocutandremovetagsthese two special filters for content cleaning.
Remove specific characters:cutFilter
In content management, sometimes we need to remove some unnecessary characters from strings, such as extra spaces, special symbols, or simply a specific segment of the text.cutThe filter is very useful.
cutThe function of the filter is to remove all occurrences of the specified character from any position in a string.It will search for the provided "keyword" parameter and remove all matches from the original string.
Usage:
{{ obj|cut:"关键词" }}
Among themobjis the string variable you want to process,"关键词"is the character or string fragment you want to remove.
Actual Case:
Suppose you have a variableproductNamehas a value of" 安企CMS 内容管理系统 "And you want to remove extra spaces from both ends 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 comes to standardizing text or preparing data for specific display formats (such as removing delimiters for merging display).
Remove HTML tags:removetagsFilter
When displaying content, especially when the content is from a rich text editor or imported externally, you may want to remove certain HTML tags to ensure consistent page styles, improve SEO, or extract plain text content.removetagsThe filter is born for this purpose.
removetagsThe filter can remove the HTML tags you specify explicitly from a string. Unlike removing all HTML tags,striptagsThe filter is different,removetagsProvided finer control, you can selectively 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 angle brackets), separated by English commas,.
Important reminder:Due toremovetagsThe content processed by the filter may still be a HTML fragment (because it only removed the tags you specified, other tags may remain), so when outputting,it 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:
Suppose you have a variablearticleContentContains 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> 获取更多信息。
PassremovetagsYou 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 the AanQi CMS:
- SEO optimizationWhen generating concise Meta Description or Meta Keywords, remove all HTML tags from the content to ensure that search engines capture pure text summaries.
- Content Summary/PreviewWhen displaying article summaries on the article list page or the homepage, it is usually desirable to show plain text to avoid layout chaos or style conflicts caused by HTML tags.
- Data CleaningRemove non-standard characters from user input, external APIs, or collected content to standardize data formats.
- Interface OutputWhen providing content data to other systems via API, it may be necessary to provide a plain text version without any HTML tags.
- safetyAlthough
removetagsCan be used to remove some potentially dangerous HTML tags, but if you need to completely remove all unknown HTML tags to prevent XSS attacks, usestriptagsFilter (removing all HTML tags) will be safer.removetagsMore suitable for known and scenes that require precise control. - UI consistencyForce certain areas of the content to be displayed 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 the Anqi CMS, enhancing the website's user experience and data quality.
Common Questions (FAQ)
Q1:removetagsandstriptagsWhat are the differences? Which one should I choose?
A1: removetagsFilter used to remove youspecifiedone or more HTML tags. For example,{{ content|removetags:"p,div"|safe }}it will only remove<p>and<div>tags, while keeping other like<a>/<strong>etc. tags.
whilestriptagsthe filter will removeallHTML tag, which converts HTML content into plain text completely. For example,.{{ content|striptags }}It will remove all HTML tags (including.<a>/<strong>etc.)
Choose which one depends on your needs. If you need to precisely control which tags to remove, please useremovetags. If you just want to get the plain text content, not