Jinja2

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

Programming Model

Find out what the latest version is from https://pypi.org/project/Jinja2/

Then add this to your requirements.txt

jinja2 == 3.0.3

Once the virtual environment is updated, use it as such:

from pathlib import Path
from jinja2 import Environment, FileSystemLoader

template_dir = Path('...')
yaml = Environment(loader=FileSystemLoader(template_dir))\
  .get_template('my-template.yaml.j2') \
  .render(variable_1='some value', variable2='some other value')

Templating Language

Variable

raw content followed by {{ variable1 }}

Whitespace Management

A leading dash removes all whitespace (including new lines) between the last non space template character and it. A trailing dash removes all whitespace (including new lines) between it and the next non-whitespace character from the template.

 {{-
 -}}
 {%-
 -%}