In the Anqi CMS template,pluralizeThe filter is a very practical tool that can automatically adjust the display form of words according to the singular and plural changes of numbers.This can avoid manually writing complex conditional judgments when displaying text related to quantity, such as "1 article" or "3 articles", making the template code more concise and semantic.

pluralizeBasic usage of the filter.

When we do notpluralizeprovide any additional parameters to the filter, it will use the default rule: if the associated number is1, the word will retain its original form; if the number is not1For example 0, 2, 3, etc., a vowel is added at the end of the wordsto indicate the plural.

For example, if you want to display “1 customer” or “3 customers:”

{{ count }} customer{{ count|pluralize }}

WhencountWith1the output would be1 customer. WhencountWith0/2/3or other non1number's output will be0 customers/2 customers/3 customers.

This default behavior applies to most words, but in English, there are some plural forms that are not simply added.sIn this case,pluralizethe filter parameters come into play.

pluralizeFilter parameters"es"Meaning

"es"This parameter is used to process those that need to addessuffixes when they become plural. When you passesas a parameter topluralizethe filter, its logic is as follows:

  • if the associated number is1: The filter will display the singular form of the word, which is based on the original word,without adding any suffix. For example, if the original word iswalrus, the number is1, then it will displaywalrus.
  • If the associated number is not1: The filter will be at the end of the wordaddesto form the plural. For example, if the original word iswalrus, the number is0or2, it will displaywalruses.

Let's understand through a specific example:

{{ count }} walrus{{ count|pluralize:"es" }}

IfcountIs1to display1 walrusIfcountIs0or2to display0 walrusesor2 walruses.

This parameter is very suitable for something likebox(boxes),bus(buses),wish(wishes) such words.

pluralizeFilter parameters"y,ies"Meaning

"y,ies"This parameter is used to process those ending withyand willychanges toiesto form plural words. This parameter consists of two parts, separated by a comma,: The first partyIndicates the suffix for singular forms, the second partiesIndicates the suffix for plural forms. The filter will replace according to this rule.

Its logic is:

  • if the associated number is1: The filter will display the singular form of a word, which is thefirst part of the parameter added after the stem (y). For example, if the stem ischerr, the number is1, then it will displaycherry.
  • If the associated number is not1: The filter will be applied to the word'sAdd the second part of the parameter (to the stemies)to form the plural. For example, if the stem ischerr, the number is0or2, it will displaycherries.

See this example:

{{ count }} cherr{{ count|pluralize:"y,ies" }}

IfcountIs1to display1 cherryIfcountIs0or2to display0 cherriesor2 cherries.

This parameter is very suitable forbaby(babies),party(parties)story(stories) words.

Why did you choose this parameter design?

pluralizeThe filter's parameter design allows you to handle common English pluralization rules in a concise way, without manually writing a lot ofif-elseThe statement to judge the end of a word. It improves the readability and maintainability of the template, making the content output more grammatically standard, and also provides a more professional reading experience for the end user.

Frequently Asked Questions (FAQ)

Q1: If my word is an irregular plural form, for exampleman(men),mouse(mice),pluralizeCan the filter handle it?

A1:pluralizeThe filter is designed to handle regular suffix changes (such as addings/esorychanges)iesForman(men) ormouse(mice) such irregular plural forms, `pluralize