splitThe filter comes into play.
The starting point for flexible content operation: backend custom delimiter string
AutoCMS allows users to customize content models and fields, which brings great convenience to content operation. You may have added a custom field named "Recommend Tag" to the article model, and when entering the back-end, you chose to use a special character (such as a vertical line) to store multiple tags in a single field for easy parsing.|and comma,or semicolon;)to separate different tags. Thus, in the database, the value of this field may be something like “Brand Story|Marketing Strategy|User Experience”.
This storage method is concise and efficient, but when displaying on the front end, we usually do not want to directly display this original string.We may need to render each tag as a clickable badge individually, or display it independently on the list page.Now, directly operating on the original string feels inadequate.
splitFilter: The bridge between strings and arrays
splitThe filter is exactly the tool to solve this problem.Its core function is to split a string into an array (or list) according to the delimiter you specify.forLoops come into play, allowing you to easily traverse each element, achieving refined display and processing.
to usesplitFilter, its basic syntax is very intuitive:
{{ 原始字符串变量 | split:"分隔符" }}
For example, if your backend custom fieldarchive.CustomKeywordsis returned"品牌故事|营销策略|用户体验"You can use it like thissplit:
{{ archive.CustomKeywords | split:"|" }}
This statement's execution result will be an array containing three elements:["品牌故事", "营销策略", "用户体验"].
CombinesetTags andforLoop: Bring the array to life
This is not enough to convert a string into an array, we need to store this result for subsequent iteration. In the safe CMS template engine,setThe tag is very suitable for completing this task.setThe tag allows you to define a temporary variable to store data in the template.
tosplitFilter is related tosetThe tag can be combined in the following way:
{% set keyword_array = archive.CustomKeywords | split:"|" %}
Now,keyword_arrayThe variable contains the array of keywords you split. Next, we can usefora loop to traverse this array and process each keyword individually.
An example may look like this:)
{# 假设 archive.CustomKeywords 的值为 "品牌故事|营销策略|用户体验" #}
{% set custom_keywords_string = archive.CustomKeywords %}
{# 使用 split 过滤器将字符串按 "|" 分隔符转换为数组 #}
{% set keyword_array = custom_keywords_string | split:"|" %}
{% if keyword_array %} {# 判断数组是否为空,避免空数组时渲染无意义的HTML #}
<div class="tags-container">
{% for keyword in keyword_array %} {# 遍历数组中的每一个关键词 #}
{# 对每个关键词进行 trim 操作,去除可能存在的首尾空白字符 #}
<a href="/search?q={{ keyword | trim }}" class="tag-item">
{{ keyword | trim }}
</a>
{% endfor %}
</div>
{% else %}
<p>暂无相关标签。</p>
{% endif %}
In this example, we first assign the content of the custom fieldcustom_keywords_stringand then throughsplit:"|"Split it intokeyword_array. Then, usingifLabel judgment array whether empty, if not empty, then passforlooping through eachkeyword. In the process of traversing, we also use extra,trimA filter to remove any leading and trailing whitespace characters that may exist for each keyword, ensuring a tidy display and wrapping it in<a>Label in, points to a search page, so each keyword becomes a clickable search link.
UsesplitSome notes on the filter.
Exact match of delimiters.:
splitThe filter will split strictly according to the delimiter you provide. If your string is"关键词1, 关键词2"(followed by a space), and you onlysplit:","use it to separate, then the elements of the array you get will be["关键词1", " 关键词2"],其中第二个元素会包含前导空格。为了避免这种情况,你需要确保分隔符参数与实际字符串中的分隔符完全一致,或者在遍历时对每个元素使用EnglishtrimFilter removes blank. For example, usingsplit:", "to handle comma-separated values with spaces.when the delimiter does not exist: If the original string does not contain the specified delimiter,
splitThe filter returns an array containing only the original string itself. This usually conforms to expectations because you can still process this single element through aforloop.The special behavior of empty separatorsIf you set the separator to an empty string (
"")splitThe filter will split each UTF8 character in the original string into a separate array element. For example,"安企CMS" | split:""You will get["安", "企", "C", "M", "S"]. If you do indeed need to split by character, this usage will be convenient.make_listEnglish substitute: For the scenario of splitting strings purely by individual characters, Anqi CMS also providesmake_lista filter, which can achieve this purpose more concisely, for example"你好世界" | make_listYou will get["你", "好", "世", "界"].
Passsplit
Common Questions (FAQ)
Q1:If my keywords are separated by commas,)splitCan the filter still be used?
A1:Of course you can.splitFilter supports any string as a delimiter. If your keywords are separated by "comma+space", just change the delimiter parameter to", ", for example{% set keyword_array = archive.CustomKeywords | split:", " %}It is important that the delimiter matches the string you are actually storing. When iterating, it is still recommended to use each element to ensure that each keyword does not contain unexpected spaces.| trimFilter.
Q2:splitHow do I link to them if I want to perform a jump to the array elements obtained?
A2:splitThe filter is only responsible for converting a string to an array.If you want each keyword to be clickable for jumping, you need to build the links manually.