In the template design of AnQi CMS, we often need to make accurate statistics of the content on the page, whether it is to dynamically display the number of data, or to make conditional judgments based on the number, mastering how to calculate the number of elements in a string or an array is particularly important.AnQiCMS powerful template engine provides a simple and efficient method to handle these requirements.
The acquisition of string length
When we need to know the length of a string, for example, the number of characters in an article title, AnQiCMS template provides|lengthFilter. This filter helps us easily obtain the character count of any string variable.
The character length of the article title displayed on the page can be displayed as follows:
<p>文章标题:{{ archive.Title }}</p>
<p>标题长度:{{ archive.Title|length }} 个字符</p>
Here,archive.TitleRepresents the article title of the current page. By adding|lengthThe template engine will then calculate the total number of characters contained in the title.It should be noted that the string length calculation of AnQiCMS is based on UTF-8 encoding, which means that a Chinese character is also correctly counted as a unit, which is very friendly for operators of multilingual websites.
In addition, if you are more focused on counting the number of words in English titles rather than the number of characters, AnQiCMS also provides|wordcountfilter. For example:
<p>文章标题:{{ archive.Title }}</p>
<p>标题中的单词数量:{{ archive.Title|wordcount }} 个</p>
This filter calculates the number of words in a string by using spaces as delimiters, which is very practical for a general overview of English content statistics.
Statistics of the number of elements in an array or list
For array or list type data, such as the tag list of articles, sub-items in the navigation menu, or image galleries, we can also use|lengthFilter to count the total number of elements it contains.This is very convenient for implementing some dynamic display logic, such as "show tag cloud only if there are tags" or "show carousel only if there are more than 1 image" and other scenarios.
For example, on a document detail page, we may need to display all the tags associated with the article, and also want to know how many tags there are in total:
{% tagList tags with limit="10" %}
{% if tags|length > 0 %}
<p>本文共有 {{ tags|length }} 个相关标签:</p>
<ul>
{% for item in tags %}
<li><a href="{{ item.Link }}">{{ item.Title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>本文暂无相关标签。</p>
{% endif %}
{% endtagList %}
In the above code, we first usetagListGet article tags and assign them totagsthe variable. Next, bytags|lengthCheck if the label list is empty and display the corresponding text and list content. This method is also applicable to other array type variables, such as fromarchiveDetailobtained fromImages(Image list of the article) orContentTitles(Title list in the article content).
For example, judge whether the article has images:
{% archiveDetail archiveImages with name="Images" %}
{% if archiveImages|length > 0 %}
<p>本文共包含 {{ archiveImages|length }} 张配图:</p>
{% for img in archiveImages %}
<img src="{{ img }}" alt="配图" />
{% endfor %}
{% else %}
<p>本文没有配图。</p>
{% endif %}
{% endarchiveDetail %}
Count the number of occurrences of a specific element in an array or string.
除了获取总长度,有时我们还需要知道某个特定的值在一个字符串或数组中出现了多少次。AnQiCMS为此提供了English|countFilter. This filter can calculate the frequency of a specified keyword in a string or the number of occurrences of a specific element in an array.
For example, count the number of occurrences of a specific keyword in the content of an article:
{% archiveDetail articleContent with name="Content" %}
<p>关键词“AnQiCMS”在文章中出现了 {{ articleContent|count:"AnQiCMS" }} 次。</p>
{% endarchiveDetail %}
Or, count the number of occurrences of a specific color in a custom color list:
{% set colors = '["red", "blue", "red", "green", "red"]'|list %}
<p>在颜色列表中,“red”出现了 {{ colors|count:"red" }} 次。</p>
Through these flexible filters, AnQiCMS makes it easy for website operators to count and dynamically display the number of contents in templates. Whether it's to optimize user experience or improve the website's SEO performance, these features provide a solid foundation.
Common Questions (FAQ)
1.|lengthand|countWhat are the main differences between the filters?
|lengthThe filter is used to obtain the total number of characters in a string or the total number of elements in an array/list. It counts the "how many".|countThe filter is used to count how many times a specific substring or element appears in a larger string or array. It counts the frequency of 'a specific value'.
2. In the loop (forHow to get the current element is the nth, or how many elements are left in the loop?In AnQiCMSforIn the loop, you can use special loop variables to get this information.{{ forloop.Counter }}It will display the current loop index (starting from 1), while{{ forloop.Revcounter }}it will display the number of remaining elements. These variables are automatically available within the loop and do not require additional definition.
3. When calculating the length of a Chinese string, how many characters is one Chinese character counted as?AnQiCMS's template engine counts the length of strings according to the actual number of characters in UTF-8 encoding.This means that regardless of how many bytes a Chinese character occupies internally, it will always be counted as 1 character externally.This ensures consistency between length calculation and user visual perception.