Skip to content

Commit 4cef982

Browse files
committed
Add pluralize filter
1 parent f8796c8 commit 4cef982

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

plain/plain/templates/jinja/filters.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ def localtime_filter(value, timezone=None):
1515
return localtime(value, timezone)
1616

1717

18+
def pluralize_filter(value, singular="", plural="s"):
19+
"""Returns plural suffix based on the value count.
20+
21+
Usage:
22+
{{ count }} item{{ count|pluralize }}
23+
{{ count }} ox{{ count|pluralize("en") }}
24+
{{ count }} cact{{ count|pluralize("us","i") }}
25+
"""
26+
try:
27+
count = int(value)
28+
except (ValueError, TypeError):
29+
return singular
30+
31+
if count == 1:
32+
return singular
33+
34+
return plural
35+
36+
1837
default_filters = {
1938
# The standard Python ones
2039
"strftime": datetime.datetime.strftime,
@@ -27,4 +46,5 @@ def localtime_filter(value, timezone=None):
2746
"timesince": timesince,
2847
"json_script": json_script,
2948
"islice": islice, # slice for dict.items()
49+
"pluralize": pluralize_filter,
3050
}

0 commit comments

Comments
 (0)