Python Learning Note-day17 (JINJA2)

Source: Internet
Author: User
Tags git clone

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.