JINJA2 is a fully functional template engine for Python with full Unicode support and an optional integrated sandbox execution environment. It is fast and widely used. Using JINJA2 requires at least python2.4
Characteristics
Execution in sandbox
Powerful HTML auto-escape system to protect systems from cross-site scripting attacks (XSS)
Template inheritance
Compile the best Python code in time
Optional time to compile the template in advance
Easy to debug, with the number of rows in the exception pointing directly to the corresponding row in the template
Configurable syntax
Installation
Tar file Installation
Tar fielname.tar.gz
Enter the extracted files directory
Then sudo python setup.py install
PIP installation
sudo pip install jinja2
Installation of the development edition
git clone git://github.com/mitsuhiko/jinja2.git
CD JINJA2
Ln-s Jinja2/usr/lib/python2. X/site-packages
How to use
>>> from JINJA2 import Template
>>> template = Template (' Hello {{name}}! ')
>>> template.render (name= ' John Doe ')
U ' Hello John doe! '
An additional example
Cat func.py
#!/usr/bin/env python#-*-coding:utf-8-*-from jinja2 Import templatedef Index (): F = open (' index.html ') result = f . read () template = Template (result) data = Template.render (name= ' John Doe ', user_list=[' Alex ', ' Eric ') return da Ta.encode (' utf-8 ') NewData = index () print NewData
Cat index.html
<! DOCTYPE html>
Python func.py
<! DOCTYPE html>
This article is from the "'ll Notes" blog, so be sure to keep this source http://timesnotes.blog.51cto.com/1079212/1748411
Python Learning Note-day17 (JINJA2)