In website content operation, we often encounter such situations: the template variable content retrieved from the database may contain some unnecessary characters, extra spaces, or unsatisfactory formatting.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 to help us easily clean and format these variables.
The template syntax of AnQiCMS 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, removing specific content, and so on.The filter is very intuitive, you just need to add a "pipe" symbol at the end of the variable name|Then follow the name of the filter, if the filter requires parameters, use a colon afterwards:Just connect the parameters, for example{{ 变量名 | 过滤器名称 : 参数 }}If you need to use multiple filters in a row, just concatenate them:{{ 变量名 | 过滤器1 | 过滤器2 : 参数 }}.
The tool for removing specific characters:cutandreplaceFilter
When we want to remove some specific characters from template variables,cutandreplacethese filters come into play.
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 variableproductNameThe value is"安企CMS-免费-高效"but you hope to remove all the hyphens-You can use it like this:
{{ productName|cut:"-" }}
The result of the output will be"安企CMS免费高效"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 section of text, you need to usereplacefilter.
replaceFilter
replacefilterer thancutVery 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. Its usage is{{ obj|replace:"旧词,新词" }}Where "old word" is the content you want to replace, and "new word" is the content you hope to replace, separated by an English comma,Separated.
For example, your article titlearticleTitleMay include"AnQiCMS是一个优秀的CMS", would you like to remove all"CMS"with"内容管理系统"This can be written as:
{{ articleTitle|replace:"CMS,内容管理系统" }}
The output will become"AnQi内容管理系统是一个优秀的內容管理系统".
If your goal is to completely remove a keyword, you can leave the "new word" part blank. For example, to remove all occurrences of a word from the title:"优秀"style:
{{ articleTitle|replace:"优秀," }}
This will"AnQiCMS是一个优秀的CMS"changes to"AnQiCMS是一个的CMS".
A very practical scenario is when you want to remove all internal spaces in a variable,replaceFilters can also be used:
{{ " 这是 包含 多余 空格的 字符串 "|replace:" ","" }}
But please note that this method will only removesingle space. If you have multiple consecutive spaces, you may need to call it multiple timesreplace, or use more advanced text processing methods.
Process extra spaces:trimSeries filter
Excess spaces, especially spaces at the beginning and end of strings, often affect the alignment and display of content. AnQiCMS providestrim/trimLeftandtrimRighta filter to accurately handle these spaces.
trimFilter
trimThe filter can remove all leading and trailing spaces (including newline characters). This is the most commonly used space cleaning tool.
For example, there is a name obtained from user inputuserNameIts value is" 张三 "In order to avoid blank spaces when displayed, you can use:
{{ userName|trim }}
The result of this output is neat"张三".trimYou can also specify the leading and trailing characters to be removed, such as{{ "###标题###"|trim:"#" }}It will output."标题".
trimLeftandtrimRightFilter
If your requirement is more refined, and you only want to remove the leading and trailing spaces from a string, thentrimLeftandtrimRightthe filter will be your choice.
trimLeftThe filter only removes all leading spaces from a string:{{ " Hello World "|trimLeft }}The output is
"Hello World ".trimRightThe filter only removes all trailing spaces at the end of the string:{{ " Hello World "|trimRight }}The output is
" Hello World".
andtrimSimilarly, these two filters can also specify the removal of specific leading or trailing characters.
Combined application and **practice
In practice, you may need to combine these filters to achieve **effect.**For example, you may need to remove the spaces at the beginning and end of the string, then replace some specific characters, and finally replace multiple consecutive spaces with a single space.
A typical example is cleaning text obtained from external data sources, which may have leading and trailing spaces and special symbols inside:
{% set rawText = " ## AnQiCMS - 内容管理系统 ## " %}
{{ rawText|trim|replace:"##,"|replace:" - ","-" }}
This statement will be removed first:rawTextRemove all spaces at both ends and then delete all the `