In AnQi CMS template development, string handling is one of the common requirements.When we need to split a string containing multiple values into an array and process each element of the array by removing spaces, it requires skillfully combining multiple filters.This article will discuss in detail how to achieve this goal in Anqi CMS template and provide practical code examples.

The foundation of string splitting:splitFilter

First, let's get to knowsplitFilters. In the Anqi CMS template engine,splitThe filter can split a string into an array of strings (or list) based on the specified delimiter.

For example, we have a string containing multiple tags, separated by commas and spaces:{% set raw_tags = " 热门, 推荐 , 新品 " %}

If used directlysplitThe filter is split using commas as delimiters:{% set tag_array = raw_tags|split:"," %}

at this point,tag_arrayIt will be an array containing three elements:[" 热门", " 推荐 ", " 新品 "]As can be seen, there may still be extra spaces before and after each element, which may cause difficulties in subsequent display or processing.

The tool for removing spaces:trimFilter

To solve the problem of extra spaces in elements, the Anqi 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 for removing trailing spaces (or specified characters) from strings.

For example, using a single stringtrimFilter:{{ " Hello World "|trim }}will outputHello World.

Skillfully combined:splitwithtrimin conjunction with others.

The core issue we are facing now is how to split