In AnQiCMS template development, we often need to display a set of data obtained from the database, such as the multiple tags (Tags) of an article, in a user-friendly manner, rather than simply listing them.One of the most common needs is to concatenate these tags into a string with a custom delimiter, such as AnQiCMS offers a powerful template engine that provides a flexible way to achieve this goal.
Understand the data structure of AnQiCMS templates
AnQiCMS's template uses syntax similar to Django, with its core advantage being the ability to easily retrieve data from the backend and render it on the frontend. When we use liketagListSuch a label to get the tags of the document, it usually returns an array or slice containing multiple 'tag objects'. Each 'tag object' itself may containTitle(label name),Link(Tag link) and multiple fields.
For example, by{% tagList tags %}Tags like this, we get.tagsThe variable is not a simple string array, but an array of multiple likes.TagThis structure is composed of an array. If we try to perform certain operations on it directly, we may not get the results we want.
To clearly demonstrate this, we usually use templates infora loop to iterate over these tags, like this:
{# 假设我们正在一个文档详情页,获取当前文档的标签 #}
{% tagList tags with itemId=archive.Id limit="10" %}
{% for item in tags %}
<a href="{{item.Link}}">{{item.Title}}</a>
{% endfor %}
{% endtagList %}
This code will output the links and titles of each tag separately, but they are scattered HTML elements. Our goal is to concatenate{{item.Title}}these strings.