How to combine the `pluralize` filter with variables to dynamically generate text with the correct plural form?

Calendar 👁️ 79

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:

  • Ifarticle.CommentCountIs 1, the text will display as '1 comment.'
  • Ifarticle.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.

Related articles

In Anqi CMS template, what does the parameter `"y,ies"` or `"es"` in the `pluralize` filter specifically represent?

In Anqi CMS template, the `pluralize` filter is a very practical tool that can automatically adjust the display form of words according to the singular and plural changes of numbers.This can avoid manually writing complex conditional judgments when displaying text related to quantity, such as "1 article" or "3 articles", making the template code more concise and semantic.

2025-11-08

How does the `pluralize` filter determine whether to apply plural form based on the input number value?

In website content operation, we often need to display different text information according to specific data values.For example, when your website has "1 new messageThe need to dynamically adjust the form of words based on numbers is particularly important in multilingual environments, for improving user experience and content professionalism.AnQiCMS (AnQiCMS) provides a very practical tool - the `pluralize` filter, which can help us easily solve this problem.

2025-11-08

How to use the `pluralize` filter to handle words like `walrus` that require a specific suffix (such as `es`) to become plural?

In website content operations, ensuring the accuracy and professionalism of the text is crucial, especially when dealing with multilingual content.Many times, we need to show the singular or plural form of a word based on the change in quantity, which not only concerns grammatical correctness but also directly affects the user's reading experience.The `pluralize` filter provided by AnQiCMS is the tool to solve this problem, it can intelligently handle the conversion of singular and plural English words, even for words like `walrus` that require special suffixes.

2025-11-08

What is the default pluralization rule for the `pluralize` filter when no plural suffix is provided?

In Anqi CMS template development, the `pluralize` filter is a very practical tool that can automatically adjust the singular and plural forms of words according to the number.This provides great convenience in the scenario of making multilingual websites or when dynamic display of corresponding words based on quantity is required.Many times, we may wonder, when only a number is provided to the `pluralize` filter and no explicit plural suffix is specified, what default rules will it follow for plural processing?### Understand `pluralize`

2025-11-08

The `pluralize` filter affects its output when handling different quantities, such as 0, 1, or multiple items?

When building a website, the way content is presented often determines the subtlety of user experience.A small detail, such as correctly displaying the singular and plural forms of words according to the number, can make the website content appear more professional and natural.AnQiCMS provides a very practical tool to handle such cases, that is the `pluralize` filter.### Flexible Handling of Quantity Changes: AnQiCMS `pluralize` Filter Usage Details In the dynamic content display of the website, such as displaying product inventory

2025-11-08

In AnQi CMS template, how does the `add` filter implement the functionality of adding numbers or concatenating strings?

In AnQi CMS template development, we often need to perform some basic data processing, such as simple addition operations on numbers, or combining different text fragments to form new strings.To meet these common and practical needs, AnQi CMS provides a very convenient template filter——`add`.This is a compact yet powerful tool that can help us easily perform addition and string concatenation, making template logic more flexible.

2025-11-08

How to use `capfirst`, `lower`, `upper`, `title` filters to perform different case conversions on English strings?

When using AnQiCMS to build and manage website content, we often need to refine the text control, especially in the presentation style of the content.English string case conversion is a common requirement, whether it is to unify style, optimize title display, or meet specific layout requirements, AnQiCMS's powerful template filter can provide a flexible solution.

2025-11-08

How do the `center`, `ljust`, and `rjust` filters align strings to the center, left, or right at a specified length?

In the presentation of website content, the format and alignment of text often directly affect the reader's reading experience.Especially when creating AnQiCMS templates, we often encounter the need to center, left-align, or right-align titles, phrases, or data lists according to a specific length.Although most layouts rely on CSS styles, in some cases, using built-in string filters in templates can be more flexible and efficient to achieve these delicate text layouts.

2025-11-08