How does the `pluralize` filter correctly display the singular and plural form of a word based on the `wordcount` value?

Calendar 👁️ 63

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

Related articles

How to dynamically adjust the length of the article abstract based on the number of words in the AnQiCMS template?

In website content operation, the article abstract is like a business card of the article, it can catch the reader's attention at the first time, helping them quickly understand the main theme of the article, thus deciding whether to delve deeper into reading.A well-designed summary can not only improve the user experience, but also be of great benefit to search engine optimization (SEO).However, faced with a massive amount of articles, how to ensure that the summary is both accurate and concise, flexible to adapt to different lengths of articles, and avoid being too long or too short, is a challenge faced by many operators.

2025-11-09

How to use the `wordcount` filter in page titles and descriptions to ensure SEO keyword density?

## How to skillfully use AnQiCMS's `wordcount` filter to accurately control the keyword density of page titles and descriptions The importance of page titles (Title) and descriptions (Description) in website operations and Search Engine Optimization (SEO) practices is self-evident.They are not only the information that users see first in search results, which directly affects the click-through rate, but are also key signals for search engines to understand the core theme of the page content.To make the page stand out among competitors, control the keyword density in the title and description reasonably

2025-11-09

In AnQiCMS multi-site management, can the `wordcount` filter be applied across sites or share the statistical logic?

The performance of AnQiCMS (AnQiCMS) in multi-site management has always been our focus.Especially when faced with content processing filters like `wordcount`, it is natural to think about its application capabilities across sites.Delve into the design philosophy and template mechanism of Anqi CMS, and we can better understand its logic in this aspect.First, let's clarify the function of the `wordcount` filter.

2025-11-09

How to use the `stringformat` filter to format the integer result of `wordcount`?

In AnQiCMS template development, the `wordcount` filter is undoubtedly a very practical tool that can help us quickly count the number of characters in articles, product descriptions, and other content.However, getting just a number is often not enough; we may want to present this number in a more readable or professional format to the visitor.At this time, the `stringformat` filter comes into play, which can finely format the integer result of `wordcount` to make the display of numbers more in line with the overall design and user experience of the website.

2025-11-09

How does the `wordcount` filter affect the performance overhead for long text? Will it affect the page loading speed?

In the daily operation of AnQi CMS, we all pay close attention to the performance of the website and the page loading speed, especially when dealing with a large amount of content.Regarding whether the `wordcount` filter will bring significant performance overhead and its impact on page loading speed is a common question.Today, let's delve deeply into this topic. ### Understanding the `wordcount` filter and its working principle First, let's clarify the role of the `wordcount` filter.

2025-11-09

How to use the `fields` filter to extract all 'words' from a string and further process them?

In AnQiCMS, content operators often need to flexibly handle and display text information.Sometimes, we may need to extract all the 'words' from a long string, whether it is for keyword analysis, making tag clouds, or simply for reorganizing content display.At this time, the AnQiCMS template system provides a very practical tool - the `fields` filter, which can help us easily achieve this goal.

2025-11-09

In AnQiCMS content management, after batch replacing keywords, will the `wordcount` result be automatically updated?

In content management, efficiency and accuracy are the core concerns of operators.AnQiCMS is a feature-rich CMS that provides various tools to simplify content maintenance.Among them, the batch keyword replacement function greatly improves the efficiency of content adjustment, while word count (`wordcount`) is an important indicator of content length.Around these two features, users often wonder: Will the word count automatically update after the content has been replaced with batch keywords?

2025-11-09

What effect will be produced when the `wordcount` filter is combined with the `add` filter?

AnQiCMS with its flexible and powerful template system makes the presentation and data processing of website content very efficient.In the process of template development, skillfully combining various filters often unlocks unexpected practical effects.Today, let's delve deeply into two seemingly basic filters that, when combined, can produce wonderful effects: `wordcount` and `add`.

2025-11-09