Jinja2 example for generating a local file using a template
2015-10-15 13:42
465 查看
原文链接:https://pythonadventures.wordpress.com/archives/
Here I want to show you how to generate an HTML file (a local file) using a template with theJinja2 template
engine.
Python source (proba.py)
#!/usr/bin/env python
import os
from jinja2 import Environment, FileSystemLoader
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader(os.path.join(PATH, 'templates')),
trim_blocks=False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def create_index_html():
fname = "output.html"
urls = ['http://example.com/1', 'http://example.com/2', 'http://example.com/3']
context = {
'urls': urls
}
#
with open(fname, 'w') as f:
html = render_template('index.html', context)
f.write(html)
def main():
create_index_html()
########################################
Jinja2 template (templates/index.html)
Resulting output
If you execute proba.py, you will get this output:
Here I want to show you how to generate an HTML file (a local file) using a template with theJinja2 template
engine.
Python source (proba.py)
#!/usr/bin/env python
import os
from jinja2 import Environment, FileSystemLoader
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader(os.path.join(PATH, 'templates')),
trim_blocks=False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def create_index_html():
fname = "output.html"
urls = ['http://example.com/1', 'http://example.com/2', 'http://example.com/3']
context = {
'urls': urls
}
#
with open(fname, 'w') as f:
html = render_template('index.html', context)
f.write(html)
def main():
create_index_html()
########################################
Jinja2 template (templates/index.html)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Proba</title> </head> <body> <center> <h1>Proba</h1> <p>{{ urls|length }} links</p> </center> <ol align="left"> {% set counter = 0 -%} {% for url in urls -%} <li><a href="{{ url }}">{{ url }}</a></li> {% set counter = counter + 1 -%} {% endfor -%} </ol> </body> </html>
Resulting output
If you execute proba.py, you will get this output:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Proba</title> </head> <body> <center> <h1>Proba</h1> <p>3 links</p> </center> <ol align="left"> <li><a href="http://example.com/1">http://example.com/1</a></li> <li><a href="http://example.com/2">http://example.com/2</a></li> <li><a href="http://example.com/3">http://example.com/3</a></li> </ol> </body> </html>
相关文章推荐
- Jinja2粗浅使用经验
- web.py中使用Jinja2问题汇总
- Jinja2学习
- [Webpy]在webpy中使用jinja2模板
- [Flask]学习杂记--模板
- 解决jinja2 模板和AngularJS分隔符冲突的方法
- Django开发运维后台:基本环境配置
- Kali安装OWTF、PIP、tornado
- 用python搭建一个web service 接口 ( RESTful API )
- Sanic 连接postgresql数据库
- Ansible使用jinja2管理配置文件以及jinja2语法简介
- Jinja2 简明使用手册
- Flask Web 开发 博客文章_4
- Flask Web 开发 Jinja2 macro 信息
- Flask学习总结笔记(4)-- Jinja2模板引擎之二
- Flask学习总结笔记(3)-- Jinja2模板引擎之一
- flask实现在首页文章列表中图片简略表示
- 人工智能自动代码生成3---数据库获取代码实现1
- 人工智能自动代码生成2---环境搭建
- 人工智能自动代码生成1---前言