When using AnQiCMS for website content management, we often need to extract specific information from the document list and combine it in some format, such as connecting a series of document category IDs into a string for frontend dynamic rendering, SEO optimization, or specific data statistics.Although AnQiCMS's template system provides powerful data acquisition capabilities, it requires us to skillfully use its built-in tags and filters to implement such 'extract-combine' logic directly in the template.

Core Challenge: Extract and Concatenate Specific Fields from List Data

Using AnQiCMS template tags:archiveListWithforLoop

Firstly, to get the document list, we will use AnQiCMS'sarchiveListTags. This tag can return a collection of document objects based on the conditions we set, such as module ID, category ID, sorting method, etc.

Assuming we want to get all documents under a certain category and extract their category ID. TypicalarchiveListUsage is as follows:

{% archiveList archives with type="list" categoryId="1" limit="10" %}
    {# 此处将遍历返回的文档列表 #}
{% endarchiveList %}

Here,archivesThe variable will contain a list of document objects. Next, we need to iterate through this list and access the specific fields of each document object. AnQiCMS's template engine supportsforLoop to complete this task:

{% for item in archives %}
    {# 这里的 item 代表列表中的每一个文档对象 #}
    {# 我们可以通过 item.FieldName 的形式访问其属性,例如 item.CategoryId #}
{% endfor %}

A clever combinationsetTags andaddFilter to implement string concatenation

Now, we have been able to obtain the classification ID of each document.The key is how to connect these independent ID values into a string, such as “1,2,3,4,5”.setTags are used to declare and assign variables,addFilter is used for concatenation of strings or numbers, which is the tool we use to achieve our goal.

Our approach is:

  1. First, usesetLabel initializes an empty string variable to store the final concatenated result.
  2. InforIn each iteration of the loop, the current document's category ID is appended to this string variable.
  3. To avoid extra delimiters at the beginning of a string (such as “,”), we can judge in the loop whether it is the first element, and do not add a prefix delimiter to the first element.

The following is the specific implementation code:

{# 步骤1:初始化一个空字符串变量来存储所有分类ID #}
{% set allCategoryIds = "" %}

{% archiveList archives with type="list" categoryId="1" limit="5" %} {# 假设获取分类ID为1的最新5篇文档 #}
    {% for item in archives %}
        {# 步骤2:在循环中,将当前文档的分类ID拼接到 allCategoryIds 变量中 #}
        {# 步骤3:判断是否是第一个元素,以控制分隔符的添加 #}
        {% if forloop.First %}
            {# 如果是第一个元素,直接拼接分类ID,不加逗号 #}
            {% set allCategoryIds = allCategoryIds|add:item.CategoryId %}
        {% else %}
            {# 如果不是第一个元素,先拼接逗号,再拼接分类ID #}
            {% set allCategoryIds = allCategoryIds|add:","|add:item.CategoryId %}
        {% endif %}
    {% endfor %}
{% endarchiveList %}

{# 循环结束后,allCategoryIds 变量就包含了所有分类ID组成的字符串 #}
<p>所选文档的分类ID集合:{{ allCategoryIds }}</p>

Code analysis:

  • {% set allCategoryIds = "" %}: We defined a variable namedallCategoryIdsand initialized it as an empty string.
  • {% for item in archives %}TraversalarchiveListThe list of documents obtained.
  • {% if forloop.First %}: This is a very useful built-in variable in the AnQiCMS template loop, it determines whether the current iterated element is the first in the list.
  • {% set allCategoryIds = allCategoryIds|add:item.CategoryId %}:
    • item.CategoryId: Get the current document's category ID.
    • |add:value: This is the AnQiCMS template.addFilter. Its function is to concatenate the value on the left with the value on the right (if both are strings) or to add them (if both are numbers). Here, it will concatenate theallCategoryIds(string) withitem.CategoryIdConcatenation. (Numbers will be implicitly converted to strings) Join together.
  • {% else %}{% set allCategoryIds = allCategoryIds|add:","|add:item.CategoryId %}: If it is not the first element, we use|add:","Concatenate a comma, and then concatenate the current category ID.

Through this method, we ultimately getallCategoryIdsThe variable will be a comma-separated string of category IDs, for example: "1,5,3,2,4".

Attention Points and **Practice

  • Consideration of data volume:This method of dynamically concatenating strings in the template is efficient and practical for small to medium-sized data lists (for example, tens to hundreds of items).If the document list is very large, such as tens of thousands of items, then performing a large number of string concatenations in the template layer may slightly increase the server's rendering burden.For extreme cases, consider preprocessing data in the AnQiCMS backend plugin or custom module and directly passing the concatenated string to the template.
  • Field Type:Ensure you extract the field (such asitem.CategoryId)can be implicitly converted to a string, or it is of string type itself. Numeric types are usually automatically converted, while other complex objects may require further processing.
  • Separator customization:You can change the code according to your needs:,For any other separator, such as:|(Space) and others.
  • Generality:This method is not only suitable for extracting classification ID, but also can be used to extract document ID (item.Id), Document title (item.Title) or any other field that can beitem.FieldNameaccessed.

Summary

AnQiCMS's template system has some limitations in complex logic processing, but through clever combinationarchiveListlabel to get data,forloop traversal,setlabel assignment andaddThe filter concatenation feature allows us to flexibly extract specific fields from a document list and combine them into the required string.This is very helpful for realizing various dynamic content display and data processing needs, and also reflects the strong customizability of AnQiCMS in the field of content management.


Common Questions (FAQ)

1. Why can't you directly usearchiveListtags to use featuresGROUP_CONCATlike SQL to concatenate strings?

The template system of AnQiCMS is mainly used for data display and rendering, rather than complex data aggregation operations at the database level.archiveListThe purpose of tags is to obtain document list objects, and like `