Skip to main content

Useful tools for jinja2 template processor.

Project description

# j2tools
Useful tools for jinja2

## Install

```bash
pip install j2tools
```

## Testing

_coming soon_

## YamlLoader

YamlLoader is a template loader for jinja2 template framework.
It loads templates from yaml-files. Useful when you need to
store many small templates in one file.

#### Example:

```yaml
# templates.yaml
home:
welcome: |
Welcome, {{username}}!
goodbye: |
Goodbye, {{username}}!
```

```python
# main.py
from jinja2 import Environment
from j2tools import YamlLoader

jinja = Environment(loader=YamlLoader('templates.yaml'))

username = 'John Doe'
template1 = jinja.get_template('home/welcome')
rendered1 = template1.render(username=username)
print(rendered1) # Welcome, John Doe!

template2 = jinja.get_template('home/goodbye')
rendered2 = template2.render(username=username)
print(rendered2) # Goodbye, John Doe!
```

## t_factory

t_facotory is a small tool which helps prefixed templates easily.

See examples below.

#### Example:

```yaml
# templates_en.yaml
home:
welcome: |
Welcome, {{username}}!
goodbye: |
Goodbye, {{username}}!
```

```yaml
# templates_ru.yaml
home:
welcome: |
Привет, {{username}}!
goodbye: |
Пока, {{username}}!
```

```python
# main.py
from jinja2 import Environment, PrefixLoader
from j2tools import YamlLoader
from j2tools import t_factory

loader = PrefixLoader({
'en': YamlLoader('templates_en.yaml'),
'ru': YamlLoader('templates_ru.yaml'),})

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:

```yaml
# templates.yaml
info:
users: |
System have {{users}} active {{users|plural('en','user','users')}}.
```

```python
# main.py
from jinja2 import Environment
from j2tools import YamlLoader
from j2tools import plural

jinja = Environment(loader=YamlLoader('templates.yaml'))
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:

```yaml
# templates.yaml
info:
uptime: |
System uptime: {{started|elapsed(show_seconds=True)}}.
newyear: |
To next year remaining {{newyear|remaining}}!
```

```python
# main.py
from jinja2 import Environment
from j2tools import YamlLoader
from j2tools import elapsed, remaining
import datetime

jinja = Environment(loader=YamlLoader('templates.yaml'))
jinja.filters['elapsed'] = elapsed
jinja.filters['remaining'] = remaining

started = datetime.datetime.now()
newyear = datetime.datetime(2020, 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:

```yaml
# templates.yaml
home:
welcome: |
Welcome, {{username}} {{UN('THUMBS UP SIGN')}}!
```

```python
# main.py
from jinja2 import Environment
from j2tools import YamlLoader
from j2tools import uchar

jinja = Environment(loader=YamlLoader('templates.yaml'))
jinja.globals['UN'] = uchar

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



Download files

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

Source Distribution

j2tools-2019.3.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

j2tools-2019.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file j2tools-2019.3.tar.gz.

File metadata

  • Download URL: j2tools-2019.3.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for j2tools-2019.3.tar.gz
Algorithm Hash digest
SHA256 90bbe226e886aa50c8756cb3c5e27eb078a904e19bb691f7caa6bda21d893c1a
MD5 7351e71599250c4ff135a0f1cb71a836
BLAKE2b-256 b52b1c7320ff415c850add670c98f481ba8b704cd79b4bf3c680cb50e26aed35

See more details on using hashes here.

File details

Details for the file j2tools-2019.3-py3-none-any.whl.

File metadata

  • Download URL: j2tools-2019.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for j2tools-2019.3-py3-none-any.whl
Algorithm Hash digest
SHA256 17cac91a547d4975dc6dace5a39a9951beca0f643b0843a768ebafd86f77637f
MD5 f8dff26fd3ba195fec1a9ea2405c0871
BLAKE2b-256 3f088596f0b5942f4c012d49f885cf0ad86333445e994c0d1f073ad38517d925

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