In website content operation, we often encounter such situations: the template variable content retrieved from the database may contain unnecessary characters, extra spaces, or formatting that is not satisfactory.These subtle details, if not handled properly, may affect the aesthetics of the page, user experience, and even cause interference with search engine optimization (SEO).AnQiCMS provides powerful template filtering features, which help us easily clean and format these variables.
AnQiCMS's template syntax is similar to the Django engine, which allows us to process template variables through a mechanism called 'filters'.The filter can perform various operations on variables, such as formatting text, modifying strings, and removing specific content.|Then follow the name of the filter, and if the filter requires parameters, use a colon:Connect the parameters, for example{{ 变量名 | 过滤器名称 : 参数 }}If you need to use multiple filters consecutively, just concatenate them:{{ 变量名 | 过滤器1 | 过滤器2 : 参数 }}.
A tool for removing specific characters:cutandreplaceFilter
When we want to remove certain specific characters from template variables,cutandreplacethese two filters come in handy.
cutFilter
cutThe filter can remove all occurrences of a specific character from a string. Its usage is very simple, just specify the character you want to remove. For example, if there is a variableproductNamehas a value of"安企CMS-免费-高效"[en] But you hope to remove all hyphens in it-[en] You can use it like this:
{{ productName|cut:"-" }}
[en] The result of the output will be"安企CMS免费高效"[en] It should be noted that,cutThe filter can only delete all instances of a single character, if you want to delete a word or a paragraph of text, you need to usereplaceFilter.
replaceFilter
replaceFilter bycutIt is more powerful, it can replace a specific keyword (or substring) in a string with another keyword.This is very useful for more complex character cleaning tasks.{{ obj|replace:"旧词,新词" }}Among them, 'old word' is the content you want to replace, and 'new word' is the content you want to replace it with, separated by an English comma,.
For example, your article titlearticleTitleMay contain"AnQiCMS是一个优秀的CMS"Do you want to put all of them?"CMS"Replace it with"内容管理系统"You can write it like this:
{{ articleTitle|replace:"CMS,内容管理系统" }}
The output will be:"AnQi内容管理系统是一个优秀的內容管理系统".
If your goal is to completely remove a keyword, you can leave the "New Word" field blank. For example, to remove all instances from the title"优秀"of the word:
{{ articleTitle|replace:"优秀," }}
This will"AnQiCMS是一个优秀的CMS"Changes to"AnQiCMS是一个的CMS".
An extremely practical scenario is when you want to remove all internal spaces in a variable.replaceFilters can also be used for this:
{{ " 这是 包含 多余 空格的 字符串 "|replace:" ","" }}
But please note that this method will only removeSingle spaceIf you have multiple consecutive spaces, you may need to call it multiple timesreplaceOr use a more advanced text processing method.
Handle extra spaces:trimSeries of filters
Excess spaces, especially leading and trailing spaces in strings, often affect the alignment and display of content. AnQiCMS providestrim/trimLeftandtrimRighta filter to accurately handle these spaces.
trimFilter
trimThe filter can remove all whitespace (including newline characters) from the beginning and end of a string. It is the most commonly used whitespace cleaning tool.
For example, there is a name obtained from user inputuserNamewith the value" 张三 "To avoid blank spaces when displayed, you can use:
{{ userName|trim }}
The output result will be neat in this way"张三".trimYou can also specify the specific characters at the beginning and end to be deleted, for example{{ "###标题###"|trim:"#" }}It will output"标题".
trimLeftandtrimRightFilter
If your requirement is more refined and you only want to remove whitespace at the beginning or end of a string, thentrimLeftandtrimRightthe filter will be your choice.
trimLeftThe filter only removes all whitespace at the beginning of a string:{{ " Hello World "|trimLeft }}The output result is
"Hello World ".trimRightThe filter only removes all trailing spaces at the end of the string:{{ " Hello World "|trimRight }}The output result is
" Hello World".
andtrimSimilarly, these filters can also specify the removal of specific leading or trailing characters.
Combination use and **practice
In practical applications, you may need to combine these filters to achieve **effect**.For example, you may need to remove the leading and trailing spaces of the string first, then replace some specific characters, and finally replace multiple consecutive spaces with a single space.
An example of a typical case is cleaning text obtained from external data sources, which may have leading and trailing spaces as well as special symbols within:
{% set rawText = " ## AnQiCMS - 内容管理系统 ## " %}
{{ rawText|trim|replace:"##,"|replace:" - ","-" }}
This statement will remove first:rawTextTwo spaces at both ends, then remove all `