# coding:utf-8 from jinja2 import Template x = """ <p>大爷的孙子</p> <ul> {% for i in my_list %} <li>{{ i }}</li> {% endfor %} </ul> """ template = Template(x) # 输出是一个HTML格式的字符串(视图模板) print(template.render(my_list=['大孙子', '二孙子', '小孙子']))
运行结果:
<p>大爷的孙子</p> <ul> <li>大孙子</li> <li>二孙子</li> <li>小孙子</li> </ul> Process finished with exit code 0