In AnQiCMS templates, flexible string processing is an important aspect for enhancing content display effects and data accuracy. We often encounter the need to remove specific character sets from both ends of a string, for example, when obtaining titles or tags from a database, they may be()/[]even<>Enclosed in brackets, and these brackets may not be needed when displayed on the front end.It is fortunate that AnQiCMS is developed in Go language, and its template engine adopts the Django-like template syntax, providing powerful filter functions that can easily handle such string processing tasks.
Recognize the string processing in AnQiCMS templates
AnQiCMS's template system is designed to be simple and efficient, allowing developers to{{ 变量 }}to output data, and through|Symbols are used in conjunction with filters to process data.These filters are like individual processing stations in the data pipeline, capable of formatting, truncating, replacing, and other operations.trim.
Core Solution:trimFilter
trimThe filter is specifically used to remove the specified characters from the beginning and end of a string.The principle of its operation is to detect whether the characters at both ends of the string are included in the "character set" parameter provided. If a match is found, it removes it and continues this process until it encounters a character not in the character set.
Basic Usage: Remove single brackets
Suppose we have a string(AnQiCMS), we want to remove the parentheses at both ends.trimThe filter can do this easily. You just pass the characters you want to remove as an argument:
{% set my_string = "(AnQiCMS)" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除圆括号后:{{ my_string|trim:"()" }}</p>
The output will be:
原始字符串:(AnQiCMS)
移除圆括号后:AnQiCMS
Similarly, if the string is[AnQiCMS]Remove brackets, the operation method is completely the same:
{% set my_string = "[AnQiCMS]" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除方括号后:{{ my_string|trim:"[]" }}</p>
The output will be:
原始字符串:[AnQiCMS]
移除方括号后:AnQiCMS
Advanced usage: Remove multiple types of brackets or nested brackets
trimThe power of the filter lies in the ability to specify a 'character set' parameter containing multiple characters.It will remove all instances that match these characters from both ends of the string, regardless of the order of the characters.trimand process them all at once.
For example, if the string is[(AnQiCMS)]We hope to remove both square brackets and parentheses at the same time.
{% set my_string = "[(AnQiCMS)]" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除方括号和圆括号后:{{ my_string|trim:"()[]" }}</p>
Here are the()[]It is a character set.trimIt will remove all characters from both ends.(,),[,]Until the valid content of the string begins. The output is:
原始字符串:[(AnQiCMS)]
移除方括号和圆括号后:AnQiCMS
Consider a scenario of consecutive nesting, for example,((AnQiCMS)):
{% set my_string = "((AnQiCMS))" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除连续括号后:{{ my_string|trim:"()" }}</p>
Due totrimContinuously removes matching characters until an unmatched character is encountered, so even nested brackets at both ends will be completely removed, the output being:
原始字符串:((AnQiCMS))
移除连续括号后:AnQiCMS
Other related filters (auxiliary description)
AlthoughtrimThe filter mainly handles characters at the beginning and end of a string, but sometimes you may also need to handle specific characters within the string.replaceThe filter will be a good helper for you.
replaceFilter: Replace characters inside strings
replaceThe filter is used to replace all occurrences of a specific substring in a string with another substring. It takes two parameters, separated by a comma:,old substring and new substring.
For example, if the string isAnQi(CMS)系统We want to remove the middle,():
{% set my_string = "AnQi(CMS)系统" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除内部括号后:{{ my_string|replace:"(,"|replace:")," }}</p>
Need to use the chain twicereplaceFilter, replace once(Empty, replace again)Empty. The output is:
原始字符串:AnQi(CMS)系统
移除内部括号后:AnQiCMS系统
this istrimThe filter only processes strings that have contrasting ends, each with its focus, and can be flexibly combined according to actual needs.
Application scenarios and precautions
trimThe filter is widely used in AnQiCMS templates, for example:
- Cleaning data displayWhen the title, description, and other content obtained from the background may contain unnecessary decorative characters, use
trimto present beautifully on the front end. - Uniform formatEnsure that all entries are displayed in a uniform format without any extra brackets when showing tag clouds or keyword lists.
- SEO optimizationSometimes backend data may contain extra symbols when processing SEO titles or descriptions, the front end handles it through...
trimThe filter can quickly clear them.
UsetrimPoints to note about the filter:
trimThe filter will only process the outermost layer of the string. If parentheses are in the middle of the string, they will not be removed. For exampleAnQi(CMS)系统Use|trim:"()"It will still be obtainedAnQi(CMS)系统.trimThe parameter is a "character set", which means it will match and remove characters from this character setanythat appear, rather than as a whole "substring" for matching. For example|trim:"()means removing all(and all)characters, not just removing()this sequence.
MasteredtrimThe use of filters and their coordination with other string processing tools will allow you to manage and display content more efficiently and flexibly in the AnQiCMS template, ensuring the neatness and beauty of the website data.
Common Questions (FAQ)
Q1:trimCan the filter remove parentheses from the middle of a string?A1: No.trimThe filter is designed to remove stringsbeginning and endThe specified character. If you need to remove a stringfrom the middleThe brackets should be used.replaceFor example, to filter."AnQi(CMS)系统"Remove.(),"{{ "AnQi(CMS)系统"|replace:"(,"|replace:")," }}.
Q2: If the string is.((AnQiCMS))Usetrim:"()"What will be obtained?A2: Will be obtainedAnQiCMS.trimThe filter will continue to remove characters from both ends of the string that match the specified character set until it encounters a non-matching character. Therefore,((and))all of them will be removed consecutively.
Q3:trimHow does the filter handle Chinese characters?A3:trimThe filter handles Chinese characters in the same way as English characters because it operates on a character set. If you aretrimThe parameter contains Chinese characters, it will try to remove these Chinese characters from both ends of the string. For example,{{ "【测试】"|trim:"【】" }}it will output测试AnQiCMS's template engine has good support for Unicode characters.