Jinja2

Introduction

The Jinja2 Extension provides the Jinja2OutputHandler for output rendering, and the Jinja2TemplateHandler for file/content templating based on the Jinja2 templating language.

Documentation References:

API References:

Requirements

  • Jinja2

Cement 3.0.8+:

pip install cement[jinja2]

Applications using Cement <3.0.8 should continue to include jinja2 in their dependencies.

Configuration

Application Configuration Settings

This extension honors the following settings under the primary namespace (ex: [myapp]) of the application configuration:

Setting

Description

template_dir

Directory path of a local template directory.

Application Meta Options

This extension honors the following App.Meta options:

Option

Description

template_handler

A template handler to use as the backend for templating

template_dirs

A list of data directories to look for templates

template_module

A python module to look for templates

Usage

Output Handler

from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['jinja2']
        output_handler = 'jinja2'

with MyApp() as app:
    app.run()

    # create some data
    data = {
        'foo': 'bar',
    }

    # render the data to STDOUT (default) via a template
    app.render(data, 'my_template.jinja2')

Template Handler

from cement import App

class MyApp(App):
    class Meta:
        label = 'myapp'
        extensions = ['jinja2']
        template_handler = 'jinja2'

with MyApp() as app:
    app.run()

    # create some data
    data = {
        'foo': 'bar'
    }

    # copy a source template directory to destination
    app.template.copy('/path/to/source/', 
                      '/path/to/destination/', 
                      data)

    # render any content as a template
    app.template.render('foo -> {{ foo }}', data)

Last updated