In the template development of AnQi CMS, we often need to perform detailed control and adjustment of the content displayed on the page.The filter is a set of powerful tools provided by the Anqi CMS template engine, which help us easily modify the output format and content of variables.trimLeftandtrimRightThese two very practical filters play a crucial role in handling extra content at the beginning and end of strings. To better understand them, we will also briefly mention their 'brother' filters.trim.
UnderstandingtrimFilter: Clean strings at both ends
Before getting a deeper understandingtrimLeftandtrimRightBefore that, let's take a look attrimFilter.trimThe function is to remove theAll leading and trailing spaces or specific characters.
If you just simply use{{ 你的变量|trim }}It will automatically clean up all extra spaces, tabs, and newline characters at the beginning and end of the string. This is very convenient for standardizing input data or removing unintended blank spaces caused by layout.
And when you want to remove specific characters, you can pass these characters as parameters totrimFor example,{{ "欢迎使用安企CMS"|trim:'欢迎CMS' }}. It's important to note that it checks for the presence of parameters at the string's endsany characterRemove it until a character not in the set is encountered. For example, in the above case, it would remove the '欢' and '迎' at the beginning, as well as the 'C', 'M', and 'S' at the end, resulting in使用安企.
trimLeft:Precisely delete the left part of the string
As the name suggests,trimLeftThe filter focuses on processing stringsThe left part (i.e., the beginning). Its behavior is similar totrim, but the scope of action is limited to the beginning of the string.
If you have a string欢迎使用安企CMS(Note that there are spaces at the beginning and end), use{{ " 欢迎使用安企CMS "|trimLeft }}, the result will be欢迎使用安企CMS. As you can see, the space on the right side of the string is still retained.
Similarly, you can specify the character set to be deleted. For example,{{ "欢迎使用安企CMS"|trimLeft:"欢迎" }}The characters '欢' and '迎' at the beginning of the string will be removed (if they exist), leaving使用安企CMS.This filter is very useful when we need to remove fixed prefixes or clean up unnecessary leading characters from user input, for example, when processing CSV data, it may be necessary to clear the specific delimiters at the beginning of the lines.
trimRight[en] Precisely delete the right part of a string
trimRightThe filter does the opposite, focusing on clearing theright side (i.e., the end)It will remove all trailing spaces or the specified characters from the end of the string.
For example,{{ " 欢迎使用安企CMS "|trimRight }}The result is欢迎使用安企CMSAs you can see, the leading spaces at the beginning of the string are preserved.
If you want to remove a specific character at the end of a string, like{{ "欢迎使用安企CMS(AnQiCMS) "|trimRight:") " }}it will remove all the ')' and spaces at the end, resulting in `Welcome to AnQiCMS