In website content operation, we often need to display different text information based on specific data values.For example, when your website has '1 new message', you want to display the singular form, and when there are '5 new messages', you need to display the plural form.This demand for dynamically adjusting the form of words based on numbers is crucial, especially in multilingual environments, for improving user experience and content professionalism.pluralizeA filter that helps us easily solve this problem.
What ispluralizeFilter?
pluralizeThe filter is a built-in feature of the Anqi CMS template engine that allows us to automatically determine and output the singular or plural form of a word based on a numerical value.This means you don't need to write complex conditional judgment logic, just call this filter, and the system can intelligently adjust the form of words according to the number of contexts, making your website content more accurate and natural in terms of quantity expression.
pluralizeThe principle of the filter.
pluralizeThe core logic of the filter is to determine which word form to use based on the input number. The basic rules are as follows:
- When the number is
1hour: The filter will output the word insingular form. - When the number is
0Or any greater1when the value is: The filter will output the word inin plural form.
To achieve this flexible conversion,pluralizeThe filter allows us to provide different "plural affix" parameters to adapt to the plural rules of various words, including regular and irregular plurals.
How to usepluralizeFilter?
pluralizeThe usage of the filter is very concise, mainly by connecting the numeric variables with the filter in the template and providing plural suffixes as needed. The basic syntax is: {{ 数字变量|pluralize:"单数附加词,复数附加词" }}.
Next, we delve into it through several common usages:
Basic usage: by default, 's' is added as a plural suffix:When you do not
pluralizeThe filter provides any parameter, it will default to adding a 's' at the end of the word when0or greater1. This method is suitable for most regular plural rules, such ascustomerchanges tocustomers.For example, if you have a variable representing the number of customers
simple.number:customer{{ simple.number|pluralize }}- If
simple.numberIs0to displaycustomers - If
simple.numberIs1to displaycustomer - If
simple.numberIs2to displaycustomers
- If
Custom plural suffix: only give one suffixSome words do not form their plural simply by adding 's', but by adding 'es' or other suffixes (but the word root remains unchanged). In this case, you can
pluralizeThe filter provides a parameter, which is the suffix to be added when it is plural.For example, the word
walrusis in its plural form.walruses:walrus{{ simple.number|pluralize:"es" }}- If
simple.numberIs0to displaywalruses - If
simple.numberIs1to displaywalrus - If
simple.numberIs2to displaywalruses
- If
Handle irregular plural forms: Provide two suffixes (singular suffix, plural suffix)For those words with significant differences between singular and plural forms, or with special endings, you can
pluralizeThe filter provides two parameters separated by a comma: the first parameter is the suffix in singular form, and the second parameter is the suffix in plural form.For example, the word
cherry(Singular) andcherries(Plural):cherr{{ simple.number|pluralize:"y,ies" }}- If
simple.numberIs0to displaycherries - If
simple.numberIs1to displaycherry - If
simple.numberIs2to displaycherries
- If
Practical usage scenarios
pluralizeThe filter is widely used in the actual content operation of AnQi CMS, especially in places where dynamic display of quantity information is needed, which can significantly improve user experience:
- Website notificationFor example, in the user message center, display 'You have 1 new message' or 'You have 3 new messages' based on the number of unread messages.
- Product or article countOn the search results page or category list page, display 'Found 1 item' or 'Found 15 items', or 'This category has 20 articles'.
- Number of commentsBelow the article details page, display "1 comment" or "5 comments".
- Cart/Favorites: Dynamically display "There is 1 item in the shopping cart" or "There are 2 items in the shopping cart.".
Code example
The followingpluralizeThe specific application example and display result of the filter in the Anqi CMS template:
Template code:
{# 假设 simple.number 的值会在运行时动态变化 #}
<p>customer{{ 0|pluralize }}</p>
<p>customer{{ 1|pluralize }}</p>
<p>customer{{ 2|pluralize }}</p>
<p>cherr{{ 0|pluralize:"y,ies" }}</p>
<p>cherr{{ 1|pluralize:"y,ies" }}</p>
<p>cherr{{ 2|pluralize:"y,ies" }}</p>
<p>walrus{{ 0|pluralize:"es" }}</p>
<p>walrus{{ 1|pluralize:"es" }}</p>
<p>walrus{{ simple.number|pluralize:"es" }}</p>
Display result:
customer
customer
customers
cherries
cherry
cherries
walruses
walrus
walruses
(Please note that the specific value in the examplesimple.numberwill affectwalrusThe final display, here it is assumed that the value is a number that triggers plural form.)
Summary
pluralizeThe filter is a small but powerful tool in the Anqi CMS template language.It simplifies the syntax, allowing your website to dynamically adjust the singular and plural forms of text based on numbers, which not only enhances the accuracy and professionalism of the content but also greatly optimizes the user reading experience.pluralizeFilters can enliven the content of your website.
Frequently Asked Questions (FAQ)
Question:
pluralizeCan the filter be used to handle the plural form of non-English words? Answer:Yes.pluralizeThe filter works by using the "singular suffix" and "plural suffix" parameters you provided.If you are clear about the singular and plural change rules of the target language and provide the correct suffix (such as the common plural change in French or German), it can be used to display the dynamic plural form of non-English words.Question: If I pass a value to
pluralizeWhat will happen if the value passed to the filter is not a number? Answer:pluralizeThe filter expects a numeric value to determine singular or plural.If the input is not a valid number, the system usually ignores the effect of the filter, or according to the specific implementation of the template engine, it may result in an empty output, displaying the original string unchanged, or even cause template rendering errors in some cases.Therefore, make sure to pass numeric data type before using it.**Question:**