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; whereas when there are '2 comments', it should be displayed in plural form.This detail handling not only enhances the user experience, but also demonstrates the professionalism of the website content.If manual quantity judgment and if/else logic are used, the template code will become 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 solution:pluralizeFilter
pluralizeThe filter is a powerful tool in the AnQiCMS template engine, mainly used to automatically add the correct singular or plural suffix to the word stem based on the number value passed in.This filter will intelligently judge whether the quantity is 1 (singular) or non-1 (plural) and select the appropriate suffix according to your configuration.
pluralizeThe basic usage format of the filter is:{{ 数量值|pluralize:"单数后缀,复数后缀" }}. Among them:
数量值: This is a dynamic number, which determines whether the final output is singular or plural."单数后缀,复数后缀"This is an optional parameter. You can provide different suffixes according to the plural rules.
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 's' to the root of the word.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:Show 'X item' or 'X items'
Assuming you have a variableitemCountStores the number of items.
{# 假设 itemCount = 1 #}
您有 {{ itemCount }} item{{ itemCount|pluralize }}。
{# 输出:您有 1 item。 #}
{# 假设 itemCount = 5 #}
您有 {{ itemCount }} item{{ itemCount|pluralize }}。
{# 输出:您有 5 items。 #}
2. Custom plural suffixes (e.g. 'es')
Some words do not form their plural simply by adding 's', such as words ending in 's', 'x', 'ch', or 'sh', which usually need to add 'es'. At this point, you can bypluralizeThe filter provides a parameter to specify the plural suffix.
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 (e.g.,"es"), AnQiCMS will treat it asplural suffix. No suffix will be added when the quantity is 1.
3. Customize singular and plural suffixes (e.g., 'y,ies')
For words ending in 'y', the plural form is usually formed by changing 'y' to 'ies'. In this case, you can bepluralizeThe 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 variablecherryCountIt stores 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 applypluralizea filter to a template?
In AnQiCMS templates, you usually need to obtain the number value from data tags and then combine it withpluralizethe filter.
1. Get the number of comments and display:Assuming you need to display the number of comments for the current article on the article detail page. You can usearchiveDetailtags to getCommentCount:
{% archiveDetail archiveInfo with name="CommentCount" %}
<p>
文章共有 {{ archiveInfo }} 条评论{{ archiveInfo|pluralize:"y,ies" }}。
</p>
Here we assume that the root word of 'comment' is 'comment', the singular form is 'commenty', and the plural form is 'commenties'.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 text for other languages that support pluralization.
2. Display the number of items in the loop:Inforin the 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 usecategoryListtags to getArchiveCount.
{% 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 AnQiCMS templates, which helps us dynamically adjust the singular and plural forms of text based on quantity with a concise syntax, thereby enhancing the accuracy of website content and user reading experience.By flexibly using its default, single-parameter, and double-parameter usage, you can easily meet various singular and plural display needs, making your website content more authentic and professional.
Common Questions (FAQ)
1.pluralizeDoes the filter support the singular and plural forms of Chinese words?Answer:pluralizeThe filter is mainly designed for languages with singular/plural concepts (such as English). For languages like Chinese, which do not have singular/plural variations, it is used directly.pluralizeIt may not have actual meaning, because in Chinese, '1 comment' and '5 comments', the word 'comment' itself does not have a plural form. In this case, you may choose not to use it.pluralizeDirectly display the quantity and the Chinese vocabulary. If you are using Chinese vocabulary in an English template and wish to simulate singular and plural forms for them, you need to manually configure according to the specific English context rules.pluralizesuffix.
2. If I have a special plural form of a word, such as 'child' is 'children',pluralizecan the filter handle?Answer:pluralizeThe filter generates plural forms by adding a suffix. For irregular plural forms like 'child' -> 'children', it cannot be processed directly by the suffix rule.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.
{% set count = 3 %}
{% if count == 1 %}
{{ count }} child
{% else %}
{{ count }} children
{% endif %}