In Anqi CMS template development, we often use various filters (filters) to process and handle data to meet the needs of front-end display. Among them,splitThe filter is undoubtedly a very practical tool, it can help us split strings of a specific format into arrays, such as converting comma-separated tag strings into a tag list.HoweverToday, let's delve into this issue in depth.

splitFilter: Convert string to array easily

First, let's reviewsplitThe basic usage of the filter. This filter can split a string into an array of strings according to a specified delimiter. Its syntax is concise and clear: {{ obj|split:"分隔符" }}.

For example, let's assume we have a string variablearticle_tagsIts value is"SEO,CMS,网站优化,SEO,内容营销". If we want to process these tags as an array in a template, we can do it like thissplitFilter:

{% set tags_string = "SEO,CMS,网站优化,SEO,内容营销" %}
{% set all_tags = tags_string|split:"," %}

{# 此时 all_tags 是一个数组:["SEO", "CMS", "网站优化", "SEO", "内容营销"] #}

Now,all_tagsIt is already an array that includes all tags. You may notice that the tag "SEO" appears twice.This is the problem we will be addressing next: how to remove these duplicate elements.

The direct support situation of duplicate removal function in AnQi CMS template

In the AnQiCMS template system, we use a template engine syntax similar to Django. We carefully reviewed the various tags and filters documentation provided by AnQiCMS, includingtag-filters.md/filter-split.mdWait, I found that the official built-in filters do not providea filter nameduniqueordistinctthat can directly act on an array to remove duplicate elements.

This means that we cannot simply call a function on an array like some programming languagesunique()A method can get a new array after deduplication. This is actually a common design concept of many template engines - they are more focused on data display and rendering, rather than complex data transformation and processing.Placing complex business logic (including data deduplication) at the template layer often leads to bulky and difficult-to-maintain templates, and reduces performance.

Recommended practice: process data on the backend, and templates are only responsible for display

Since the template layer does not have a direct deduplication filter, the most recommended, most elegant, and most efficient solution is to place the data deduplication logic on the backend for processing.AnQiCMS is developed based on the Go language, which is very efficient and flexible in handling data.

In the backend logic of Go language, we can easily implement array deduplication.For example, you can first split the string into an array, then traverse it and use the characteristics of Map (hash table) to filter out unique elements, and finally pass the deduplicated array to the template.

`go // Assuming this is a backend Go language code snippet package main

import (

"strings"

)

func deduplicateTags(tagsStr string) []string {

splitTags := strings.Split(tagsStr, ",")
seen := make(map[string]struct{}) // 使用map来追踪已见过的元素
uniqueTags := []string{}

for _, tag := range splitTags {
	trimmedTag := strings.TrimSpace(tag) // 去除可能存在的空格
	if trimmedTag == "" {
		continue // 忽略空字符串
	}
	if _, exists := seen[trimmedTag]; !exists {
		seen[trimmedTag] = struct{}{}
		uniqueTags = append(