JinjaTemplates is a light wrapper around the Jinja2 templating library for Python.
First install Jinja2 to your python environment.
pip install Jinja2
Then, in the Julia REPL, install JinjaTemplates
Pkg.clone("https://github.com/dustinrb/JinjaTemplates.jl.git")
For more information on how to write Jinja2 templates, please see the official documentation for template designers.
paths
is an Array of paths pointing to the directories where you store your templates. kwargs
are any options to customize the Jinja2 Environment. The only exception is the loader, which uses a FileSystemLoader with itsn searchpath
set to the directories specified in path
render
renders the desired template and returns the result as a String. env
is a LazyTempalteLoader. Template is the template name/path relative to the paths specified when env
was created. kwargs
are any you wish to pass to the template.
Say we have a template index.html
in /home/username/webdev/templates
and we want to render it.
<!-- index.html -->
<!DOCUMENT html>
<html>
<head>
<title>Title<title>
</head>
<body>
<h1>This is my content!</h1>
{{content}}
</body>
</html>
using JinjaTemplates
# Set two paths for Jinja2 to look in
env = LazyTemplateLoader(
["/home/username/templates",
"/home/username/webdev/templates"]
)
# Render it!
# Because there are two paths specified, Jinja will search first in
# /home/username/templates and then /home/username/webdev/templates
# index.html is not found.
render(env, "index.html"; content="This is content!") # Returns:
# <!-- index.html -->
# <!DOCUMENT html>
# <html>
# <head>
# <title>Title<title>
# </head>
# <body>
# <h1>This is my content!</h1>
# This is content
# </body>
# </html>