How to Display Singular or Plural of String Based on Number in Shopify
Drive 20-40% of your revenue with Avada
Would you want to create an automatic system of adding plural form on characters? Liquid in Shopify has a way to do that by using string filter. The tutorial today focuses on string filter, especially pluralise and guides you to display singular or plural of string based on number.
Table of content
- Brief introduction to Liquid and filters
- What is string filter and pluralise
- How to display singular or plural of string based on number
Brief introduction to Liquid and filters
Since 2006, Liquid has been the backbone language of Shopify and has been trusted by a lot of web developers. Liquid is one of the open-source template languages in Shopify. The template has been sold since 2006 and now become a good source for a lot of hosted website application such as Jekyll, salesforce desk, and zendesk.
Filter is one of the four Liquid references including filter, objects, basics and tags. Filters are simple methods that modify the output of numbers, strings, variables, and objects. Filters are placed between an output tag and are denoted by a pipe character.
What is string filter and pluralise
String filters are sub-categories of filters. String filters are used to generate output automatically and variables of string types. String filters are divided into a lot of sub-types. Today we focus on pluralise.
Pluralise outputs the singular or plural version of a string based on the value of number.
How to display singular or plural of string based on number
Displaying singular or plural form of a string is not difficult by utilizing pluralise
. Let’s look at the example below. The first parameter is singular string and the second one is plural string.
Input
{{ cart.item_count }}
{{ cart.item_count | pluralize: 'item', 'items' }}
Output
3 items
Conclusion
In a nutshell, string filters are useful for you to create automatic systems on your website. Of all string filters, you use pluralise to show the singular or plural version of a string based on the number. We hope that the tutorial is helpful and let us know your difficulties, we will address them.