Examples of using the python jinja2 template
- 2021-10-15 10:59:21
- OfStack
Template syntax
Inheritance and super functions render with jinja2
Usefulness of templates
jinja2 can be used to modify the configuration file, and the key parts of the configuration file can be replaced with variables
Template syntax
Template syntax and django's template syntax are similar to those in jinja2, and there are three kinds of syntax:
Control structure {%%}Variable value {{}} Notes {# #} for cycle
{% for user in users %}
{{user.username}}
{% endfor %}
Iterative dictionary
{% for key,value in my_dict.iteritems() %}
{{ key }}
{{ value }}
{% endfor %}
Filter
Commonly used filters are:
{{ 'abc' | catialize }}
# Abc
Inheritance and super function
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<link rel="stylesheet" href="style.css" rel="external nofollow" />
<title>{% block title %}{% endblock %} - My Webpage</title>
{% endblock %}
</head>
<body>
<div id="content">{% block content %}{% endblock %}</div>
<div id="footer">
{% block footer %}
<script>This is javascript code </script>
{% endblock %}
</div>
</body>
</html>
{% extend "base.html" %} # Inheritance base.html Documents
{% block title %} Dachenzi {% endblock %} # Customize title Part of the content
{% block head %}
{{ super() }} # Used to get the original information
<style type='text/css'>
.important { color: #FFFFFF }
</style>
{% endblock %}
# Other unmodified intact inheritance
Rendering with jinja2
There is a class named Enviroment in the jinja2 module, an instance of which stores configuration and global objects, and then loads templates from the file system or other location
1. Basic usage
PackageLoader: Package Loader FileSystemLoader: Filesystem Loader
2.PackageLoader
The easiest way to load a document using the package loader is as follows:
from jinja2 import PackageLoader, Environment
env = Environment(loader=PackageLoader('python_project', 'templates')) # Create 1 Package loader objects
template = env.get_template("base.html") # Get 1 Template files
template.render(name="daxin", age=18)
The two tests for PackageLoader () are the name of the python package and the template directory name
get_template (): Gets a specific file in the template directory
render (): Accepts variables to render the template
3.FileSystemLoader
File system loader, you can directly access the files in the system without the need for template files to exist under a certain Python package
import os
from jinja2 import PackageLoader, Environment, FileSystemLoader
path_dir = "/mnt/e/files"
loader = FileSystemLoader(searchpath=path_dir)
env = Environment(loader=loader)
template = env.get_template("index.opf") # Template file
buf = template.render(name="daxin", age=18)
with open(os.path.join("path_dir", "index.opf"), "w" ) as fp:
fp.write(buf)
FileSystemLoader (): The searchpath parameter is followed by the directory where the file is located
get_template (): Gets a specific file in the template directory
render () accepts variables to render the template
The rendered content can be saved to the configuration file
The above is the python jinja2 template using the details of the example, more information about python jinja2 template please pay attention to other related articles on this site!