In website content operation, details often significantly enhance the user experience.One common but often overlooked detail is how to correctly display the singular and plural forms of a word based on the quantity.Imagine the display of 'You have 1 message' and 'You have 2 messages'. Isn't it much more natural and smooth than seeing 'You have 1 messages' or 'You have 2 message'?pluralizeFilter.
pluralizeFilter: Automatically handle singular and plural words
pluralizeThe filter is a practical feature in the Anqi CMS template engine, which can automatically determine and add or remove the plural suffix for words based on the numerical value represented by the variable.This makes it possible to display text related to counting on the page more in line with language habits, especially in English content display, 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 maintains its singular form; when the number is 0 or greater than 1, the word will convert 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_counthas a value of1The output would be:您购物车中有 1 item。Whenitem_counthas a value of0/2or other non-1numbers, the output would be:您购物车中有 0 items。or您购物车中有 2 items。
It can be seen that when the value is 1,pluralizeThe filter will return an empty string to maintain the singular form of the word; while the value is 0 or greater than 1, it will return 's', making the word plural.
Advanced Usage: Customize Plural Suffix
Not all words have plural forms that 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 plural suffixes (for cases like adding 'es' and so on)
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(Sea elephant)这个词,复数是walruses:
<p>动物园里有 {{ walrus_count }} walrus{{ walrus_count|pluralize:"es" }}。</p>
Whenwalrus_countresponse for1at that time, output1 walrus.walrus_countresponse for0/2or other numbers, output0 walrusesor2 walruses.
2. Provide singular and plural suffixes (for word endings that change or irregular plurals)
For likecherry(Cherry)such words, the singular form ends with 'y', while the plural form needs to change 'y' to 'ies'.This is when 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_countresponse for1at that time, output1 cherry.cherry_countresponse for0/2or other numbers, output0 cherriesor2 cherries。
Note that the word stem is passed here.cherrthen bypluralizeThe filter dynamically concatenates according to the number.yories.
wordcountWithpluralizeThe combination of applications
The document mentions that,pluralizehow the filter determineswordcountThe value 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 understandwordcountFilter. 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' dynamic to adapt to the quantity, we canwordcountthe result aspluralizethe input of the filter:
{% 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 number 1 or any other number, here the "word" will automatically display correctly as "word" or "words".This can greatly enhance the professionalism and friendliness of the user interface when creating functions such as content statistics and search result hints.
Summary
Anqi CMS'spluralizeFilter