In website operation, we often need to handle various data, one common requirement being to count the number of times a specific value appears in a data set.For example, you may want to know how many times a certain keyword appears in an article, or how many colors of a product are marked as 'hot'.For users of AnQiCMS, the powerful template engine of the system provides very convenient tools to complete this task.

The template engine of AnQi CMS is powerful and flexible, it built-in a variety of filters (Filters) that can help us easily handle and format data. To count the total number of occurrences of a specific value in an array, we need to use one of the filters namedcountfilter.

UnderstandcountFilter

countThe filter is a feature specifically used for counting in the Anqi CMS template engine. Whether you want to count the frequency of a keyword in a line of string or to calculate the number of times a specific element appears in an array (slice),countThe filter is always useful. It returns an integer representing how many times the value you are looking for appears in the target data.

How to use in templatecountFilter

UsecountThe filter is very intuitive. The basic syntax is:

{{ 您的数组或字符串 | count: "要查找的值" }}

Let us look at several practical examples to see how it works in detail.

Example scenario one: Count the number of occurrences of keywords in a string

The assumption is that you have some text content and you want to know how many times a certain keyword is mentioned, which is very useful for SEO content optimization or content quality checks.

For example, we have a description about an 'AnQi CMS', and we want to count how many times the word 'CMS' appears:

{% set description = "安企CMS是一个高效、可定制的内容管理系统,它支持多站点管理,灵活的内容模型,以及强大的SEO工具。安企CMS致力于为中小企业提供优质CMS服务。" %}
<div>
    “CMS”在这个描述中出现了:{{ description | count: "CMS" }} 次。
</div>

After running this template code, the following will be displayed on the page:

“CMS”在这个描述中出现了:3 次。

As you can see,countThe filter accurately identifies and counts all occurrences of 'CMS' in the string.

Example scenario two: Count the number of times a specific value appears in an array.

In the AnQi CMS, many data exist in the form of arrays, such asarchiveDetailthe document image list obtained by tagsImages, or checkbox fields defined in the custom content model.

Assume your content model has a field namedfeatureswith multiple options, which contains several tags, such as["多语言","SEO优化","响应式","多语言","可定制"]. You want to know how many times the 'multi-language' tag has been selected.

First, we need to get this array. Iffeaturesis a custom field, you can access it like this:

{% archiveDetail myFeatures with name="features" %}
{% set featureList = myFeatures | list %} {# 假设myFeatures是字符串形式的JSON数组,用|list过滤器转成实际的数组 #}
{# 如果myFeatures已经是Go原生的slice/array类型,则无需|list转换 #}

<div>
    在功能列表中,“多语言”标签出现了:{{ featureList | count: "多语言" }} 次。
</div>

For a more intuitive demonstration, we can also define an array directly in the template:

{% set tags = '["多语言","SEO优化","响应式","多语言","可定制"]' | list %}
<div>
    在标签列表中,“多语言”标签出现了:{{ tags | count: "多语言" }} 次。
</div>

Run this code, and the page will display:

在标签列表中,“多语言”标签出现了:2 次。

This indicatescountThe filter can also accurately count the number of occurrences of a specific element in the array.

Important notes

When usingcountThere are several details to note when using the filter:

  • Perfect Match Principle:countThe filter will strictly require that the value to be searched for matches the elements or substrings in the target data exactly during the statistics.This means, if an element in the array is 'Multilingual (English)', and you are searching for 'Multilingual', it will not be counted.
  • case-sensitive: Usually, countFilter is case sensitive.For example, when searching for "cmsEnsure that the value you are looking for matches the actual value in the target data, case-sensitive.

Summary

Anqi CMS'scountThe filter is a simple and powerful tool that can help you easily count the number of occurrences of specific values in strings and arrays within website templates.This not only simplifies the logic of data analysis, but also allows content operation personnel to handle and display data more flexibly, thereby better optimizing website content and user experience.Through these practical template tags and filters, Anqi CMS indeed provides us with an efficient and customizable content management solution.


Common Questions (FAQ)

Q1:countDoes the filter distinguish between uppercase and lowercase?

A1: Yes,countThe filter is usually case-sensitive.This means that if you are searching for 'Apple', it will not count 'apple' or 'APPLE'.lowerorupper), and then count.

Q2:除了countFilter, what other filters does the Safe CMS have for array operations?

A2:The template engine of AnQi CMS provides rich array operation filters. In addition tocount, commonly used ones include:

  • length: Get the length of the array (number of elements).
  • contain:Check if an array contains a specific value and return.trueorfalse.
  • join:Join array elements into a string with a specified separator.
  • split:Split a string into an array with a specified separator.
  • firstandlast:Get the first and last elements of the array respectively.
  • slice:Slice the elements in the specified range of the array.

Q3:If the data I want to find is in a multi-dimensional array,countCan the filter still be used?

A3:countThe filter currently mainly counts one-dimensional strings or arrays directly. If your data is a multi-dimensional array (for example, each element of an array is another array),countFilter cannot be directly searched at all nested levels. You may need to combineforLoop and conditional judgment, traverse the array layer by layer, or preprocess it on the backend before the data is sent into the template, flatten it into a one-dimensional array and then count it.