cement.ext.ext_jinja2¶
The Jinja2 Extension module provides output templating based on the Jinja2 Templating Language.
Requirements¶
- Jinja2 (
pip install Jinja2)
Configuration¶
To prepend a directory to the template_dirs list defined by the
application/developer, an end-user can add the configuration option
template_dir to their application configuration file under the main
config section:
[myapp]
template_dir = /path/to/my/templates
Usage¶
from cement.core.foundation import CementApp
class MyApp(CementApp):
class Meta:
label = 'myapp'
extensions = ['jinja2']
output_handler = 'jinja2'
template_module = 'myapp.templates'
template_dirs = [
'~/.myapp/templates',
'/usr/lib/myapp/templates',
]
with MyApp() as app:
app.run()
# create some data
data = dict(foo='bar')
# render the data to STDOUT (default) via a template
app.render(data, 'my_template.jinja2')
Note that the above template_module and template_dirs are the
auto-defined defaults but are added here for clarity. From here, you
would then put a Jinja2 template file in
myapp/templates/my_template.jinja2 or
/usr/lib/myapp/templates/my_template.jinja2.
-
class
cement.ext.ext_jinja2.Jinja2OutputHandler(*args, **kw)¶ Bases:
cement.core.output.TemplateOutputHandlerThis class implements the IOutput interface. It provides text output from template and uses the Jinja2 Templating Language. Please see the developer documentation on Output Handling.
-
Jinja2OutputHandler.render(data_dict, template=None, **kw)¶ Take a data dictionary and render it using the given template file. Additional keyword arguments are ignored.
Required Arguments:
Parameters: - data_dict – The data dictionary to render.
- template – The path to the template, after the
template_moduleortemplate_dirsprefix as defined in the application.
Returns: str (the rendered template text)
-