Variable value flask template engine of jinja2

Introduction:
Jinja2 is a flask template engine, it can effectively separate business logic and page logic, make the code more readable and easier to understand and maintain
. It is flexible and simple to use. We only need to provide a template and add it in the template. Use { {variable}} to occupy a good position. When using it, pass the variable in outside the template file to restore the appearance of the file itself.

Here is only one usage: variable value

Installation:
pip3 install jinja2

The test is a successful installation:
Python -c "import jinja2"

Use:
1. Create the template file: replace the variables inside with { {variable name}}
Insert picture description here
2. In other files, you need to transfer data to the template.
First, import the template file, and then transfer the variables that need to be filled in Just go in, the code is as follows

import jinja2
from the directory where the template is located import *

    env = jinja2.Environment(loader=jinja2.FileSystemLoader("模板文件所需要的绝对路径,不需要加模板文件"))
    temp = env.get_template('模板文件名')
    dag_content = temp.render(“需要传入的变量”)  # 生成文件内容

dag_content is the file content of the incoming variable. If you need to generate a new landing, you also need to generate a file, and then write it into the generated file to generate a new landing file

    with open(path,'w') as f:
        f.write(dag_content)

Available for reference: https://www.cnblogs.com/sui776265233/p/10570712.html#_label1
https://www.w3cschool.cn/yshfid/thlnsozt.html
https://segmentfault.com/a/1190000018002480

Guess you like

Origin blog.csdn.net/weixin_43202081/article/details/107696074