Skip to main content

Collection of utils for jinja2

Project description

jinja2utils

Useful utils for jinja2

Install

pip install jinja2utils

t_factory

t_factory is a small tool which helps prefixed templates easily.

See examples below.

Example:

  • ./templates/en/home/welcome
Welcome, {{username}}!
  • ./templates/en/home/goodbye
Welcome, {{username}}!
  • ./templates/ru/home/welcome
Welcome, {{username}}!
  • ./templates/ru/home/goodbye
Goodbye, {{username}}!
  • ./main.py
from jinja2 import Environment, PrefixLoader, FileSystemLoader
from jinja2utils import t_factory

loader = PrefixLoader({
    'en': FileSystemLoader('./templates/en'),
    'ru': FileSystemLoader('./templates/ru'),
})

jinja = Environment(loader=loader)
get_t = t_factory(jinja)


def print_templates(t, username):
    rendered1 = t('home/welcome', username=username)
    print(rendered1)

    rendered2 = t('home/goodbye', username=username)
    print(rendered2)


print_templates(get_t('en'), 'John Doe')
# Expected output:
# Welcome, John Doe!
# Goodbye, John Doe!

print_templates(get_t('ru'), 'Иван')
# Expected output:
# Привет, Иван!
# Пока, Иван!

plural

plural is jinja2 filter function for easy text pluralization.

Example:

  • ./templates/info/users
System have {{users}} active {{users|plural('en','user','users')}}.
# main.py
from jinja2 import Environment, FileSystemLoader
from jinja2utils import plural

jinja = Environment(loader=FileSystemLoader('./templates'))
jinja.filters['plural'] = plural

template1 = jinja.get_template('info/users')
rendered1 = template1.render(users=1)
print(rendered1)  # System have 1 active user.
rendered2 = template1.render(users=23)
print(rendered2)  # System have 23 active users.

elapsed and remaining

Calculates and formats elapsed time to string like this: 25d 4h 3m 35s. Can be used as jinja2 filter.

Example:

  • ./templates/info
System uptime: {{started|elapsed(show_seconds=True)}}.
  • ./templates/newyear
System uptime: {{started|elapsed(show_seconds=True)}}.
# main.py
from jinja2 import Environment, FileSystemLoader
from jinja2utils import elapsed, remaining
import datetime

jinja = Environment(loader=FileSystemLoader('./templates'))
jinja.filters['elapsed'] = elapsed
jinja.filters['remaining'] = remaining

started = datetime.datetime.now()
newyear = datetime.datetime(2025, 1, 1, 0, 0, 0)

username = 'John Doe'
template1 = jinja.get_template('info/uptime')
rendered1 = template1.render(started=started)
print(rendered1)  # System uptime: 25d 4h 3m 35s.

template2 = jinja.get_template('info/newyear')
rendered2 = template2.render(newyear=newyear)
print(rendered2)  # To next year remaining 295d 10h 13m 10s!

uchar

Simple jinja2 function to insert unicode characters by unicode names. Very useful for inserting emoji.

Example:

  • ./templates/info/users
Welcome, {{username}} {{UN('THUMBS UP SIGN')}}!
# main.py
from jinja2 import Environment, FileSystemLoader
from jinja2utils import uchar

jinja = Environment(loader=FileSystemLoader('./templates'))
jinja.globals['UN'] = uchar

template1 = jinja.get_template('info/users')
rendered1 = template1.render(username='John Doe')
print(rendered1)  # Welcome, John Doe 👍!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2utils-0.1.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

jinja2utils-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file jinja2utils-0.1.0.tar.gz.

File metadata

  • Download URL: jinja2utils-0.1.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.3 CPython/3.12.1

File hashes

Hashes for jinja2utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 75a218ec7a35b07c775266097dc003d54c409d8a267a61d69c46e48c017e2834
MD5 2513dfad538df6a58e958be93a688444
BLAKE2b-256 cd5aeb30d1d2b7e46745889856dd473f211552b7f649adfd14425b5d171834a0

See more details on using hashes here.

File details

Details for the file jinja2utils-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jinja2utils-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.3 CPython/3.12.1

File hashes

Hashes for jinja2utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a42cfa3ad6f2004c7cea01cec6bab8c909c0070cd5db9fa3b04ad90e5c1ac3d2
MD5 126b26d1ac26f765d3d66f5b10220112
BLAKE2b-256 1b75cd56e7a92ca276a4760dd17b8eee5a23e069f12be74633e8d9edb539909d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page