Singular and plural modifiers in Statamic 3

Published September 19th, 2021

Who doesn’t love knowing how many results a search returned? It’s a simple question: did you find one result, or many results.

OK, that’s a boring example. What about fries? Do you want one fry or many (OK, all of the) fries? That’s better… we want all the things. Well, at least we want to know whether we have multiple fries left, or if we’re down to that last one – that last one needs to be savoured.

Statamic includes modifiers to help us singular-ify or plural-ify a word: just one caveat, it’s for English words only. Statamic does this by leveraging the Pluralizer which is part of Laravel’s core, which in turn is being helped by Doctrine’s inflector package – and if you’re curious on how it all works under the hood, is really fascinating to check out how it all works… and how borked the English language and its plural rules are.

Singular modifier

The string modifier singular is really straight forward: the basic idea is that you apply the modifier to a variable, and you get the singular representation of the word. Easy, right? 

Maybe you have a Taxonomy in Statamic, and want to output the singular representation of a term. Or even if you wanted to output the Taxonomy’s title in singular format (because you have made your Taxonomy plural, right?):

2 {{ tags }}
3 <li>{{ title | singular }} ({{ title }})</li>
4 {{ /tags }}

Let’s think about fries and nuggets (naturally) and we’ll get:

2 <li>Fry (Fries)</li>
3 <li>Nugget (Nuggets)</li>

Who doesn’t love a good automated singularisation? And given how weird English is, you can see that the under-the-hood-ness is also taking our “Fries” Term and dropping the “ies”and replacing with a “y”. Love me some magic. Actually, love me some code: go under the hood and find out why.

Plural modifier

And while I could keep talking about fries, I’ll get hungry, so let’s go back to search results. 

When we are using pagination within the Collection tag we have access to the paginate variable which includes the total_items variable.

It’d be nice to tell our visitors how many fries, sorry, search results, are found – and there are two obvious ways to do this – with an if statement, or using the plural modifier.

If we want to go the long way, we could do something like this inside our paginate

1{{ if total_items == 1 }}
2 <p>We found 1 fry in the bag.</p>
3{{ else }}
4 <p>We found {{ total_items }} fries in the bag.</p>
5{{ /if }}

And who doesn’t love bag fries? And look at that: we’re back to fries. Oh well.

We can approach this differently with Statamic (through Laravel, through Doctrine) and make use of the plural modifier.

1<p>We found {{ total_items }} {{ "fry" | plural:total_items }} in the bag.</p>

Mmmm bag fries. Plural.

When we use the plural helper, we pass it a count variable - this is the number the modifier uses to determine if it should return "fry" (the singular) or "fries" (the plural).

Depending on the language you need to use, there may be times where you actually need the if statement approach – such as “1 fry were found” doesn’t sound right – but “2 fries were found” does, and conversely “1 fry was found” is fine but “2 fries was found” isn’t. See, English is fun. In these instances, you may need to make your own modifier for was/were, or re-think your wording.

While English does pose some restrictions for using these modifiers, they’re still a handy addition to Statamic (and your Laravel apps, of course). For more information of the singular or plural modifiers, check out Statamic’s documentation. And Laravel’s Str helper documentation too for when you’re in Laravel world.

You may be interested in...