Jinja

From RaySoft

Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document. [1]

Documentation

Examples

Simple example

Code:

from datetime import datetime
from jinja2 import FileSystemLoader, Environment
from locale import setlocale, LC_TIME

TMPL_FILE = 'template.md'

setlocale(LC_TIME, 'de_CH.UTF-8')

tmpl_loader = FileSystemLoader(
    searchpath='.', encoding='utf-8',
)

tmpl_env = Environment(
    loader=tmpl_loader, block_start_string='<!--%', block_end_string='%-->',
    trim_blocks=True, lstrip_blocks=True, keep_trailing_newline=True,
)

tmpl = tmpl_env.get_template(TMPL_FILE)

args = {
    'title': 'Jinia2 Example',
    'date':  datetime.now(),
    'type':  'long',
}

print(tmpl.render(args=args))

Template:

---
date: {{ args.date.strftime('%A, %-d. %B %Y') }}
---

# {{ args.title }}

<!--% if args.type == 'long' %-->
## Print long text
<!--% elif args.type == 'short' %-->
## Print short text
<!--% endif %-->

Output:

---
date: Freitag, 5. Mai 2023
---


# Jinia2 Example

## Print long text

References

  1. Repository contributors. "Jinia." GitHub. https://github.com/pallets/jinja (accessed 05.05.2023)