Templates#

In cijoe templates refer to Jinja templates with the .jinja2 file extension. Any such file that is reachable by the automatic collection of resources, will be available as a template.

Consider the simple HTML template below with filename template.html.jinja2.

<!doctype html>
<html lang="en">
<body>
  <h1>Hello {{ name }}!</h1>
</body>
</html>

See the official Jinja Template Designer Documentation for more information on how to construct Jinja templates.

In a cijoe script, you can access the HTML template via the cijoe resources. The example script below will populate the template with the initiator’s hostname, generating a new file with the HTML.

"""
example of using templates
==========================

An example of how to use a template from the cijoe resources. 

The script renders a Jinja template with filename `template.html.jinja2` that
takes `name` as a parameter, and creates a new html file from the template 
where all instances of "{{ name }}" has been replaced with the initiator's 
hostname.

"""

from argparse import Namespace

import jinja2

from cijoe.core.command import Cijoe
from cijoe.core.resources import get_resources


def main(args: Namespace, cijoe: Cijoe):
    resources = get_resources()
    template_path = resources["templates"]["template.html"].path

    jinja_env = jinja2.Environment(
        autoescape=True, loader=jinja2.FileSystemLoader(template_path.parent)
    )
    template = jinja_env.get_template(template_path.name)

    err, state = cijoe.run_local("hostname")
    if err:
        return err

    hostname = state.output().strip()

    with open("hello.html", "a") as file:
        html = template.render({"name": hostname})
        file.write(html)

    return 0