In the development of Anqi CMS templates, handling strings is one of the common requirements.When we need to split a string containing multiple values into an array and then process each element of the array to remove spaces, this requires the clever combination of multiple filters.This article will discuss in detail how to achieve this goal in the Anqi CMS template and provide practical code examples.
String Slicing Basics:splitFilter
First, let's get to knowsplitFilters. In the template engine of Anqie CMS,splitThe filter can split a string into an array of strings (or a list) based on a specified delimiter.
For example, we have a string containing multiple tags, separated by commas and spaces:{% set raw_tags = " 热门, 推荐 , 新品 " %}
If used directly:splitThe filter splits using comma as the delimiter:{% set tag_array = raw_tags|split:"," %}
At this time,tag_arrayThis will be an array containing three elements:[" 热门", " 推荐 ", " 新品 "]. You can see that there may still be extra spaces around each element, which may cause trouble in subsequent display or processing.
The tool for removing spaces:trimFilter
To solve the problem of excessive spaces in elements, the Aiqi CMS template engine provides:trimThe filter family.
trimFilter: Can remove all spaces (or specified characters) from both ends of a string.trimLeftFilter: Specifically removes spaces (or specified characters) from the beginning of a string.trimRightFilter: Specifically removes trailing spaces (or specified characters) from strings.
For example, using a single string:trimFilter:{{ " Hello World "|trim }}it will output:Hello World.
Smart combination:splitWithtrimof collaborative operations
Now, the core issue we face is how to handle `split