jinja2简介
python的模板引擎,设计思想来自与django的模板引擎,和其非常相似 pip install jinjia2 pip install MakeupSafe
#模块加载 from jinja2 import Template # template = Template('hello {{name}}') # print(template.render(name='rocky')) from jinja2 import Environment,PackageLoader,select_autoescape env = Environment( loader=PackageLoader('webarch','templates'), autoescape=select_autoescape(['html','xml']) ) d = { 'userlist':[ (1,'tom',20), (2,'allen',40) ] } template = env.get_template('index.html') print(template.render(**d)) #解构userlist = []
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <ul> {% for id,name,age in userlist %} <li>{{loop.index}}{{id}},{{name}},{{age}}</li> {% endfor %} </ul> </body> </html>