When building and maintaining websites, we often need to generate dynamic content, one common requirement is to adjust the singular and plural forms of text based on the change in quantity.This is not only about the accuracy of the content, but also about improving the user experience, especially in a multilingual environment.The template engine of AnqiCMS (AnqiCMS) provides a namedpluralizeThe practical filter, which can help us easily solve this problem.
UnderstandpluralizeBasic usage of the filter.
pluralizeThe filter aims to automatically add or choose the correct plural form of a word based on its numerical value.Its basic principle is to check the associated number, if the number is 1, it is considered a single number;Otherwise considered plural.
The simplest usage is to add directly after the variable.|pluralizeAt this point, the filter will attempt to process according to the English plural rules (usually by adding "s"):
{# 假设 count 的值为 1 #}
您有 {{ count }} item{{ count|pluralize }} 在购物车中。
{# 输出:您有 1 item 在购物车中。 #}
{# 假设 count 的值为 3 #}
您有 {{ count }} item{{ count|pluralize }} 在购物车中。
{# 输出:您有 3 items 在购物车中。 #}
However, not all words follow the simple rule of adding an 's'.Some words require special plural forms, such as words ending in 'y' changing to 'ies', or adding 'es'.pluralizeThe filter also takes this into account, allowing us to pass two parameters, separated by a comma, the first parameter is the singular form of the additional word, and the second parameter is the plural form of the additional word:
{# 假设 count 的值为 1 #}
您有 {{ count }} cherr{{ count|pluralize:"y,ies" }}。
{# 输出:您有 1 cherry。 #}
{# 假设 count 的值为 3 #}
您有 {{ count }} cherr{{ count|pluralize:"y,ies" }}。
{# 输出:您有 3 cherries。 #}
{# 假设 count 的值为 1 #}
您有 {{ count }} box{{ count|pluralize:"es" }}。
{# 输出:您有 1 box。 #}
{# 假设 count 的值为 3 #}
您有 {{ count }} box{{ count|pluralize:"es" }}。
{# 输出:您有 3 boxes。 #}
topluralizeFilter combined with variable usage
pluralizeThe true power of the filter lies in its ability to be combined with dynamic variables. In AnQi CMS templates, we can obtain numbers from content models, statistics, or custom logic and then pass them directly topluralizefilter.
We can usesetLabel to simulate a dynamic variable to better understand its working mode:
{% set user_count = 0 %}
网站有 {{ user_count }} 位用户{{ user_count|pluralize }} 注册。
{# 输出:网站有 0 位用户 注册。 (这里的"用户"是中文,所以pluralize不会改变它,但如果英文,就会变users) #}
{% set user_count = 1 %}
网站有 {{ user_count }} 位用户{{ user_count|pluralize }} 注册。
{# 输出:网站有 1 位用户 注册。 #}
{% set user_count = 5 %}
网站有 {{ user_count }} 位用户{{ user_count|pluralize }} 注册。
{# 输出:网站有 5 位用户 注册。 #}
As can be seen whenuser_countis 0 or greater than 1,pluralizeThe filter tries to add an 's' to indicate plural. For Chinese words, due to their linguistic characteristics, there is no change in plural form.This filter is mainly designed for English words.
used in actual contentpluralizeFilter
In Anqi CMS, documents (archive)、Category(categoryData models usually contain some numeric fields, such as views (Views) or comment count (CommentCount). We can directly associate these variables withpluralizeFiltering combined, dynamically generating more natural text.
Assuming we are iterating through a list of articles and want to display the number of comments for each article:
{% archiveList archives with type="list" limit="3" %}
{% for article in archives %}
<article>
<h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
<p>
这篇文章有 {{ article.CommentCount }} 条评论{{ article.CommentCount|pluralize }}。
已被浏览 {{ article.Views }} 次{{ article.Views|pluralize }}。
</p>
</article>
{% endfor %}
{% endarchiveList %}
In this code block:
- If
article.CommentCountIs 1, the text will display as '1 comment.' - If
article.CommentCountIs 0 or greater than 1, the text will display as '0 comments' or 'X comments.'
It can be seen that for Chinese words like “comment” or “time”, althoughpluralizeThe filter will still execute, but its default behavior will not cause any visible change to Chinese text, because Chinese nouns do not have plural forms. However, if your website is in English content, or you have used English words in the template, thenpluralizeThe filter's role is very significant.
For example, on an English content website, the same logic will produce the following effect:
{# 假设 article.CommentCount = 1, article.Views = 1 #}
This article has 1 comment{{ 1|pluralize }}. It has been viewed 1 time{{ 1|pluralize:"e,s" }}.
{# 输出:This article has 1 comment. It has been viewed 1 time. #}
{# 假设 article.CommentCount = 5, article.Views = 10 #}
This article has 5 comment{{ 5|pluralize }}. It has been viewed 10 time{{ 10|pluralize:"e,s" }}.
{# 输出:This article has 5 comments. It has been viewed 10 times. #}
Here, we have used the word "time".time{{ count|pluralize:"e,s" }}Process, ensure that 'time' is displayed when the quantity is 1, and 'times' when there are more than 1, this is a very typical special plural handling scenario.
By the above method, we can make the text content of the Anqi CMS template more dynamic and linguistically accurate, whether it is the simple addition of 's' rule, or the need to specify the singular and plural forms of words, pluralizeFilters can provide elegant solutions. This not only improves the efficiency of template writing but also makes the website content presented to users more delicate and professional.
Frequently Asked Questions (FAQ)
1.pluralizeDoes the filter support the plural form of Chinese nouns?
pluralizeThe filter mainly follows the English grammatical rules for singular and plural forms, such as adding 's' or 'es' at the end of a word, or converting according to rules like 'y,ies', etc. Since Chinese nouns do not have grammatical singular and plural variations, thereforepluralizeThe filter does not produce actual plural effects for Chinese words. If you want to display different Chinese expressions based on quantity, you may need to combineifcondition statements manually.
2. If my English words have very irregular plural forms (such as "man" becoming "men"),pluralizeCan the filter handle it?
pluralizeThe filter is mainly used to handle regular plural forms (adding 's') and a few plural forms that are achieved by replacing suffix words (such as 'y' becomes 'ies'). For completely irregular plural forms like 'man' (singular) -> 'men' (plural) or 'child' -> 'children',pluralizeThe filter cannot handle it directly. You need to manually specify the singular and plural text for these special cases through the template.{% if %}Logic judgment.
How can I ensure that the variable I use to determine singular/plural has a value to avoid template rendering errors?Before passing the variable topluralizethe filter, it is usually recommended to useifCheck if a variable exists or is a valid number, or usedefaulta filter to provide a default value. For example,{{ item.CommentCount|default:0|pluralize }}you canitem.CommentCountconsider it as 0 before proceeding if it does not existpluralizeHandle, thus avoiding potential template rendering issues.