In AnQiCMS template, flexible string handling is an important factor in improving the display effect of content and data accuracy. We often encounter the need to remove specific character sets from both ends of a string, for example, when retrieving titles or tags from a database, they may be()/[]even<>Parentheses enclosed, and these parentheses may not be needed when displayed on the front end.Fortunately, AnQiCMS is developed based on the Go language, its template engine adopts the Django-like template syntax, providing powerful filter functions, which can easily handle such string processing tasks.
Get to know the string processing in AnQiCMS templates
The template system of AnQiCMS is designed to be simple and efficient, allowing developers to{{ 变量 }}and control logic by|Symbols combined with filters to process data.These filters are like individual processing stations in a data pipeline, capable of formatting, truncating, replacing, and other operations.To remove the specified character set from both ends of a string, we need to use one of the very practical filters -trim.
Core solution:trimFilter
trimThe filter is 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 you provide. If they match, they are removed, and this process continues until a character not in the character set is encountered.
Basic usage: Remove single type of bracket
Suppose we have a string.(AnQiCMS)We want to remove the parentheses at both ends.trimThe filter can do this easily. You just need to pass the characters to be removed as parameters to it:
{% 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]the operation to remove brackets is also consistent:
{% set my_string = "[AnQiCMS]" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除方括号后:{{ my_string|trim:"[]" }}</p>
The output will be:
原始字符串:[AnQiCMS]
移除方括号后:AnQiCMS
Advanced usage: remove multiple or nested brackets
trimThe power of the filter lies in the ability to specify a 'character set' parameter containing multiple characters.It removes all instances that match these characters from both ends of the string, regardless of their order.This means that even nested parentheses, as long as they are at the outermost layer of the string,trimcan also be processed at one time.
For example, if the string is[(AnQiCMS)]we want to remove brackets and parentheses at the same time:
{% set my_string = "[(AnQiCMS)]" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除方括号和圆括号后:{{ my_string|trim:"()[]" }}</p>
Here()[]is a character set,trimit will remove all from both ends(,),[,]The character, until the start of the valid content of the string. The output is:
原始字符串:[(AnQiCMS)]
移除方括号和圆括号后:AnQiCMS
Consider a continuous nested scenario, such as((AnQiCMS)):
{% set my_string = "((AnQiCMS))" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除连续括号后:{{ my_string|trim:"()" }}</p>
due totrimIt will continue to remove matching characters until it encounters a non-matching character, therefore, even if the brackets are nested in multiple layers, as long as they are both at both ends, they will be completely removed, and the output will be:
原始字符串:((AnQiCMS))
移除连续括号后:AnQiCMS
Other related filters (辅助说明)
AlthoughtrimThe filter mainly processes the strings at both ends, but sometimes you may also need to handle specific characters within the string. In this case,replaceThe filter will be a good helper for you.
replaceFilter: Replace characters inside a string.
replaceThe filter is used to replace all occurrences of a specific substring in a string with another substring. It accepts two parameters, separated by a comma,: the old substring and the new substring.
For example, if the string isAnQi(CMS)系统We want to remove the middle one,():
{% set my_string = "AnQi(CMS)系统" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除内部括号后:{{ my_string|replace:"(,"|replace:")," }}</p>
Here we need to use it twice in a chain,replaceFilter, replace once,(Empty, replace again,)Empty. The output is:
原始字符串:AnQi(CMS)系统
移除内部括号后:AnQiCMS系统
This istrimThe filter only processes strings with contrasting ends, each with its own focus, and can be flexibly combined according to actual needs.
Application scenarios and precautions
trimThe filter is widely used in AnQiCMS templates, for example:
- Display data cleaningWhen the title, description, and other content obtained from the backend may contain unnecessary decorative characters, use
trimIt can be presented beautifully on the front end. - Uniform formatEnsure that all entries in the tag cloud or keyword list are displayed in a uniform format without any extra brackets.
- SEO optimizationSometimes the backend data may contain extra symbols when processing SEO titles or descriptions, the frontend can
trimquickly clear them through the filter.
UsetrimNote on the filter:
trimThe filter only processes the outermost layer of the string. If the brackets 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 the characters in this setanythat appear, rather than as a whole "substring" for matching. For example|trim:"()means to remove all(and all)characters, not just to remove()this sequence.
MasteredtrimThe use of filters and their配合 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.
Frequently Asked 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 the parentheses from a stringmiddleYou should usereplaceFilter. For example, to remove"AnQi(CMS)系统"from()you can use{{ "AnQi(CMS)系统"|replace:"(,"|replace:")," }}.
Q2: If a string is((AnQiCMS)), usingtrim:"()"what will you get?A2: Will getAnQiCMS.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. So,((and))they will all 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测试The AnQiCMS template engine has good support for Unicode characters.