Jinja2 Pluralize Filters

0.3.0 · maintenance · verified Tue Apr 14

Jinja2 Pluralize is a Python library that provides pluralization filters for Jinja2 templates. It offers both a simple filter based on `inflect.py` and a Django-style pluralize filter. Currently at version 0.3.0, released in 2015, the project appears to be in a maintenance state with infrequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to register the `pluralize_dj` filter with a Jinja2 environment and use it in a template to handle singular and plural forms of words, including custom plural suffixes.

from jinja2 import Environment
from jinja2_pluralize import pluralize_dj

env = Environment()
env.filters['pluralize'] = pluralize_dj

template = env.from_string('I have {{ count }} dog{{ count|pluralize }}.')

# Example 1: count = 1
result1 = template.render(count=1)
print(f"Rendered for count=1: {result1}") # Expected: I have 1 dog.

# Example 2: count = 2
result2 = template.render(count=2)
print(f"Rendered for count=2: {result2}") # Expected: I have 2 dogs.

template_args = env.from_string('There {{ num_items|pluralize("is", "are") }} {{ num_items }} item{{ num_items|pluralize }}.')

# Example 3: using custom singular/plural words
result3 = template_args.render(num_items=1)
print(f"Rendered for num_items=1: {result3}") # Expected: There is 1 item.

result4 = template_args.render(num_items=3)
print(f"Rendered for num_items=3: {result4}") # Expected: There are 3 items.

view raw JSON →