Skip to main content

a jinja2 extension to use humanize library inside jinja2 templates

Project description

What is it ?

This is a jinja2 extension to use humanize library inside jinja2 templates.

Syntax

The generic syntax is {{ 'VALUE'|humanize_{humanize_fn}([humanize_fn_args]) }}.

Following humanize functions are currently mapped:

  • naturalsize
  • abs_timedelta
  • date_and_delta
  • naturaldate
  • naturalday
  • naturaldelta
  • naturaltime
  • precisedelta
  • humanize_intword

See humanize documentation for argument details.

To take a more real example, let's take the naturalsize() function. To use it inside a jinja2 template with this extension, you have to use:


The file size is: {{ 30000000|humanize_naturalsize(binary=False, gnu=True) }}

You can use the same logic with all supported functions. If you need other functions, feel free to open a PullRequest.

Installation

pip install jinja2-humanize-extension

Full example

from jinja2 import Template, Environment

# We load the extension in a jinja2 Environment
env = Environment(extensions=["jinja2_humanize_extension.HumanizeExtension"])

template = env.from_string("The file size is : {{ 30000000|humanize_naturalsize() }}")
result = template.render()

# [...]

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

jinja2_humanize_extension-0.2.1.tar.gz (4.1 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page