When building a website, we often encounter situations where we need to dynamically display text based on a certain quantity value.For example, when there is '1 comment', we want to display it in singular form;When there are “2 comments”, it will be displayed in plural form.This detail handling can not only improve user experience but also demonstrate the professionalism of the website content.If manually judge the quantity and write if/else logic, it will make the template code long and difficult to maintain.

AnQiCMS provides a very practical built-in filter for template developers——pluralizeIt can help us elegantly handle the singular and plural forms of words, making your website content more natural and accurate.

AnQiCMS's solution:pluralizeFilter

pluralizeThe filter is a powerful tool in the AnQiCMS template engine, which mainly serves to automatically add the correct singular or plural suffix to the root of a word based on the quantity value passed in.This filter will intelligently judge whether the quantity is 1 (singular) or not 1 (plural) and choose the appropriate suffix according to your configuration.

pluralizeThe basic usage format of the filter is:{{ 数量值|pluralize:"单数后缀,复数后缀" }}. Among them:

  • 数量值This is a dynamic number that determines whether the final output is singular or plural.
  • "单数后缀,复数后缀"This is an optional parameter. You can provide different suffixes according to the plural rules of the word.

Next, we will understand its various usages through specific examples.

1. Default usage (automatically adds 's')

For most English words, the plural form is usually formed by adding an apostrophe s to the root.pluralizeThe filter automatically follows this rule when no suffix parameter is provided: it does not add any suffix when the quantity is 1; it adds 's' when the quantity is not 1.

Example:Display "X item" or "X items" Assuming you have a variableitemCountContains the number of items.

{# 假设 itemCount = 1 #}
您有 {{ itemCount }} item{{ itemCount|pluralize }}。
{# 输出:您有 1 item。 #}

{# 假设 itemCount = 5 #}
您有 {{ itemCount }} item{{ itemCount|pluralize }}。
{# 输出:您有 5 items。 #}

2. Customize plural suffixes (e.g. ‘es’)

Some words do not have a simple plural form by adding 's', such as words ending in 's', 'x', 'ch', 'sh', which usually need to add 'es'. At this point, you canpluralizeThe filter provides a parameter to specify plural suffixes.

Example:Display 'X walrus' or 'X walruses' Assuming you have a variablewalrusCountStores the number of walruses.

{# 假设 walrusCount = 1 #}
页面中有 {{ walrusCount }} walrus{{ walrusCount|pluralize:"es" }}。
{# 输出:页面中有 1 walrus。 #}

{# 假设 walrusCount = 3 #}
页面中有 {{ walrusCount }} walrus{{ walrusCount|pluralize:"es" }}。
{# 输出:页面中有 3 walruses。 #}

Please note that when only a suffix parameter is provided (for example"es"), AnQiCMS will treat it asplural suffix. When the quantity is 1, no suffix will be added.

3. Custom singular and plural suffixes (e.g., 'y,ies')

For words ending in 'y', the plural form usually changes 'y' to 'ies'. In this case, you canpluralizeThe filter provides two comma-separated parameters: the first is the singular suffix, and the second is the plural suffix.

Example:Display 'X cherry' or 'X cherries' Assuming you have a variablecherryCountStores the number of cherries.

{# 假设 cherryCount = 1 #}
库存有 {{ cherryCount }} cherr{{ cherryCount|pluralize:"y,ies" }}。
{# 输出:库存有 1 cherry。 #}

{# 假设 cherryCount = 4 #}
库存有 {{ cherryCount }} cherr{{ cherryCount|pluralize:"y,ies" }}。
{# 输出:库存有 4 cherries。 #}

how to convertpluralizeFilter applied to the template?

In the AnQiCMS template, you usually need to obtain the quantity value from the data tags and then compare it withpluralizeFilter combined use.

1. Get the comment count and display:Assuming you need to display the number of comments for the current article on the article detail page. You can usearchiveDetailtag to obtainCommentCount:

{% archiveDetail archiveInfo with name="CommentCount" %}
<p>
    文章共有 {{ archiveInfo }} 条评论{{ archiveInfo|pluralize:"y,ies" }}。
</p>

Here we assume that the root of the word 'comment' is '评论', the singular is '评论y', and the plural is '评论ies'.If your language is Chinese and the word itself does not have a singular or plural concept, then just show the number, or use it in other languages that support pluralization changes.

2. Display the number of items in the loop:Inforthe loop,forloop.Counterorforloop.RevcounterCan provide the current loop index, which can also be used as a quantity value.

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <p>这是第 {{ forloop.Counter }} 篇文章{{ forloop.Counter|pluralize }}。</p>
        {# 假设 forloop.Counter 是 1,输出:这是第 1 篇文章。 #}
        {# 假设 forloop.Counter 是 2,输出:这是第 2 篇文章s。 #}
    {% endfor %}
{% endarchiveList %}

3. Display the number of documents under the category:If you want to display the total number of documents under each category in the category list, you can usecategoryListtag to obtainArchiveCount.

{% categoryList categories with moduleId="1" parentId="0" %}
    {% for category in categories %}
        <p>分类“{{ category.Title }}”有 {{ category.ArchiveCount }} 篇文档{{ category.ArchiveCount|pluralize }}。</p>
        {# 假设 category.ArchiveCount 是 1,输出:分类“AnQiCMS”有 1 篇文档。 #}
        {# 假设 category.ArchiveCount 是 10,输出:分类“Go语言”有 10 篇文档s。 #}
    {% endfor %}
{% endcategoryList %}

Summary

pluralizeThe filter is a small but powerful feature in the AnQiCMS template, which helps us dynamically adjust the singular and plural forms of text based on the number, thus enhancing the accuracy and reading experience of the website content.By flexibly using its default, single-parameter, and double-parameter usage, you can easily meet various needs for singular and plural display, making your website content more authentic and professional.


Frequently Asked Questions (FAQ)

1.pluralizeDoes the filter support the singular and plural forms of Chinese words?Answer:pluralizeThe filter is designed for languages that have singular and plural concepts (such as English). For languages like Chinese that do not have singular and plural variations, it is used directly.pluralizeIt may not have actual meaning because the Chinese word 'comment' in '1 comment' and '5 comments' does not have a plural form. In this case, you can choose not to use itpluralizeDirectly display the quantity and Chinese vocabulary. If you are using Chinese vocabulary in an English template and want to simulate singular and plural, you need to manually configure according to the specific English context rules.pluralizesuffix.

2. If I have a special plural form of a word, for example, the plural of 'child' is 'children',pluralizeCan the filter handle it?Answer:pluralizeThe filter generates plural forms by adding a suffix, but it cannot directly process irregular plural forms like 'child' to 'children'.In this case, you need to manually write conditional judgments in the template, or preprocess the data, or use other more complex logic to display irregular singular and plural forms. For example:

{% set count = 3 %}
{% if count == 1 %}
    {{ count }} child
{% else %}
    {{ count }} children
{% endif %}