How to display the singular or plural form of a word based on the quantity value in the AnQiCMS template?

Calendar 👁️ 70

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 %}

Related articles

How to convert letter combinations like 'PONGO2' into the corresponding numbers on a phone keypad?

How to easily convert letter combinations to phone number keypad digits in AnQiCMS?In daily life, we sometimes encounter some phone numbers composed of letters and numbers, for example, some companies may use memorable numbers like "999-PONGO2" for brand promotion.However, when making a phone call in reality, these letters need to be converted into the corresponding numbers on the phone keypad.For website operators, if they can automatically complete this conversion on the website front end, it will undoubtedly greatly improve the user experience.

2025-11-08

How to dynamically define and use small array variables for loops in AnQiCMS templates?

In AnQiCMS template development, we often need to flexibly display some small data sets according to business logic.These collections may be some custom tags, navigation items, or just some status identifiers.Although AnQiCMS provides rich tags to call background data, sometimes we want to define a small array directly in the template and loop through it to achieve finer control and simpler template code.

2025-11-08

How does the AnQiCMS template automatically convert line breaks in text to HTML tags such as `<p>` or `<br>`?

In website content presentation, text formatting is often the key to determining the user's reading experience.We often encounter such needs: the text content obtained from the background database usually only contains simple newline characters (`\n`), and if it is displayed directly on the web page, the browser will not intelligently convert these newline characters into the HTML paragraph (`<p>`) or line break (`<br>`) tags we expect, causing the content to be cramped together and lose its original structure and readability.AnQiCMS (AnQiCMS) has fully considered this point in template design

2025-11-08

How to get the total length of elements in a string, array, or key-value pair in AnQiCMS template?

In AnQi CMS template design, dynamically obtaining the total length of elements in a string, array, or key-value pair is a very practical feature. It can help us flexibly display information on the page, such as showing the number of products, the number of comments, or adjusting the layout according to the content length.The Anqi CMS template engine provides simple and efficient filters to meet these requirements.### Core Tool: `length` Filter The most direct and universal method to obtain the total number of elements in any string, array (slice), or key-value pair (map/struct) is to use

2025-11-08

How to safely remove all or part of a specified tag from HTML content in AnQiCMS templates?

When building a website with AnQiCMS, we often need to finely control the content displayed on the page.Especially when dealing with user submitted content, importing articles from different sources, or simply to maintain consistency in page style, you may encounter the hassle of HTML tags.These tags may contain unnecessary formatting, styles, and even potential security risks.Fortunately, the AnQiCMS template system provides a flexible mechanism to help us safely and efficiently remove all or part of the specified tags from the HTML content.

2025-11-08

How to ensure that the multi-site content of AnQiCMS can be displayed and managed uniformly on the front-end?

In today's complex digital environment, many businesses and content creators often need to manage multiple websites, whether they have multiple brand sub-sites, different product line portals, or provide services for multilingual users.How can it be ensured that each site operates independently while also achieving unified display and efficient management of content on the front end, which has become a common challenge.AnQiCMS as an enterprise-level content management system, provides a powerful and flexible solution in this regard.

2025-11-08

How to customize the AnQiCMS content model to achieve personalized page content display?

AnQiCMS content management system provides us with powerful content management capabilities with its excellent flexibility and customizability.In the daily operation of websites, we often encounter situations where the standard "article" or "product" model cannot fully meet the needs of specific business scenarios.At this time, the custom content model feature of AnQiCMS is particularly important, as it allows us to build personalized content structures based on the unique needs of the website, thereby achieving more accurate and expressive display of page content.Why do we need a custom content model?

2025-11-08

How does AnQiCMS support the switching of multilingual content and the correct display on the front-end page?

AnQiCMS provides powerful multilingual support capabilities, helping websites easily meet the needs of global content promotion.Understand its working principle and configuration method, which can help you efficiently manage content in different languages and display it correctly on the front-end page. ### The Foundation of Multilingual Content Management: Multi-Site Mode One of the core ideas of AnQiCMS for implementing multilingual content switching is to make use of its powerful multi-site management function.This means, typically, you would create a separate site instance for each target language.

2025-11-08