In the template development of AnQi CMS, 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, which can help us split strings of specific formats into arrays, such as converting a comma-separated tag string into a list of tags.However, when these sliced arrays contain duplicate elements, many friends will naturally think: 'Does AnQiCMS template have a direct method to remove these duplicate elements?'Today, let's delve deeply into this issue.

splitFilter: Easily convert strings to arrays

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

Give an example, suppose we have a string variablearticle_tagswith the value"SEO,CMS,网站优化,SEO,内容营销"If we want to handle these tags as an array in the template, we can use it like thissplitFilter:

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

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

Now,all_tagsThis is an array that already contains all the tags.You may notice that the label 'SEO' appears twice.This is the problem we will be addressing next: how to remove these duplicate elements.

English CMS template: direct support for duplicate removal function

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

This means we cannot simply call a function on an array like some programming languagesunique()The method can get a new array without duplicates.This is 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) in the template layer often leads to bulky, difficult-to-maintain templates, and reduces performance.

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

Since there is no direct deduplication filter in the template layer, the most recommended, most elegant, and most efficient solution is to process the data deduplication logic on the backend.AnQiCMS is developed based on Go language, which is very efficient and flexible in handling data.

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

`go // Assume this is a back-end 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(