A memorandum of the super-basic part when using Jinja2
Python 3.5.2 Jinja2 2.9.6
Place the file like this.
File placement
./
├template.txt.j2
└friend_builder.py
You can embed specific parameters in the template file. It is also possible to write repetitive syntax.
text:template.txt.j2
great!
you
{{character}}
I'm a friend who is good at!
Noshi!
friend_builder.py
from jinja2 import Template, Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template('template.txt.j2')
data = {
"character": "cuisine",
}
rendered = template.render(data)
print(str(rendered))
As the processing content,
I can't even try to make it simpler!
The execution result is as follows.
Output result
$ python friend_builder.py
great!
you
cuisine
I'm a friend who is good at!
Noshi!
The `cooking`
is safely embedded in the `{{character}}`
part of the template.
Recommended Posts