In website content operation, details often significantly enhance user experience.One common but often overlooked detail is how to correctly display the singular and plural forms of words based on quantity.Imagine seeing the display 'You have 1 message' and 'You have 2 messages', isn't it much more natural and fluent than seeing 'You have 1 messages' or 'You have 2 message'?pluralizefilter.

pluralizeFilter: Smart handling of singular and plural words

pluralizeThe filter is a practical feature of Anqi CMS template engine, which can automatically judge and add or remove the suffix indicating the plural of a word according to the value represented by the variable.This makes it possible to display count-related text on the page more in line with language habits, especially in English content display, where the effect is particularly significant.

The core of this filter is that it receives a number and decides whether to add a plural suffix based on this number (usually 1 or other numbers).When the number is 1, the word usually retains the singular form;When the number is 0 or greater than 1, the word will be converted to plural form.

Basic usage: Automatically add 's'

pluralizeThe simplest usage of the filter is without any parameters. In this case, it will automatically decide whether to add the default plural suffix 's' based on the incoming value.

For example, if we want to display the singular and plural forms of the word “item”:

<p>您购物车中有 {{ item_count }} item{{ item_count|pluralize }}。</p>

Whenitem_countThe value is1The output would be:您购物车中有 1 item。Whenitem_countThe value is0/2or other non1When the number is:您购物车中有 0 items。or您购物车中有 2 items。

It can be seen that when the number is 1,pluralizeThe filter returns an empty string to maintain the singular form of the word; when the number is 0 or greater than 1, it returns 's' to make the word plural.

Advanced Usage: Custom Plural Suffix

Not all words' plural forms are simply added 's'.Some words need to add 'es' (such as bus -> buses), and some need to change the ending (such as cherry -> cherries).pluralizeThe filter also takes these cases into account, allowing us to customize the plural suffix through parameters.

1. Only provide the plural suffix (applicable for adding 'es' and similar cases

If the plural form of a word is formed by simply adding 'es' or other fixed suffixes, we can use this suffix aspluralizethe first parameter passed to the filter.

For example, forwalrus(walrus) this word is pluralizedwalruses:

<p>动物园里有 {{ walrus_count }} walrus{{ walrus_count|pluralize:"es" }}。</p>

Whenwalrus_countWith1When it comes to output1 walrus. Whenwalrus_countWith0/2when or other numbers are output0 walrusesor2 walruses.

2. Provides singular and plural suffixes (for word endings that change or irregular plurals)

For words likecherryWords like (cherry) end with 'y' in the singular form, and 'ies' is used in the plural form.At this point, we can provide two parameters, separated by a comma: the first is the singular suffix, and the second is the plural suffix.

<p>篮子里有 {{ cherry_count }} cherr{{ cherry_count|pluralize:"y,ies" }}。</p>

Whencherry_countWith1When it comes to output1 cherry. Whencherry_countWith0/2when or other numbers are output0 cherriesor2 cherries. Note that we input the stem of the wordcherrThen bypluralizeThe filter dynamically concatenates based on the numberyories.

wordcountwithpluralizeApplication of the combination.

The document mentions,pluralizeHow does the filter base onwordcountThe number is used to correctly display the singular and plural forms of a word. This is actually a very practical scenario where these two filters work together.

First, let's understandwordcountA filter. As the name suggests,wordcountThe filter is used to count the number of words in a text and return an integer value.

For example:

{% set article_content = "AnQiCMS provides powerful content management features for various types of websites." %}
{% set total_words = article_content|wordcount %}
<p>您的文章总共有 {{ total_words }} words。</p>

Heretotal_wordsit will be11.

Now, if we want to make the display of the word 'word' also dynamically adapt to the quantity, we canwordcountas the result is used aspluralizeFilter input:

{% set article_content = "AnQiCMS provides powerful content management features for various types of websites." %}
{% set total_words = article_content|wordcount %}
<p>您的文章总共有 {{ total_words }} word{{ total_words|pluralize }}。</p>

No mattertotal_wordsIs it 1 or another number, the 'word' will automatically display as 'word' or 'words'.This can greatly enhance the professionalism and friendliness of the user interface when creating content statistics, search result prompts, and other functions.

Summary

Of Security CMSpluralizeFilter