Python Tornado framework configuration Uses Jinja2 template engine

Source: Internet
Author: User
Jinja2 is a built-in template engine in popular Web framework Flask, and is similar to Django's template engine, here, let's take a look at how to configure the Jinja2 template engine for the Python Tornado framework. Jinja2 is a built-in template engine in the popular Web framework Flask, and is similar to the Django template engine, here, let's take a look at how to configure the Jinja2 template engine for the Python Tornado framework.

Tornado has a template engine by default, but the function is simple (in fact, I can use it almost). The jinja2 syntax is similar to the django template, so I decided to use it.

Download jinja2

Use pip to download the SDK)

pip install jinja2

In this way, you can use it.

Tornado and jinja2 integration

The integration of tornado and jinja2 is very simple (in fact, it is relatively simple to find it on the Internet). I don't know what I found from there, but I did not need to talk about the code directly.

# Coding: utf-8import tornado. webfrom jinja2 import Environment, FileSystemLoader, TemplateNotFoundclass TemplateRendring (object): "" A simple class to hold methods for rendering templates. "def render_template (self, template_name, ** kwargs): template_dirs = [] if self. settings. get ('Template _ path', ''): template_dirs.append (self. settings ['Template _ path']) env = Environment (loader = FileSystemLoader (template_dirs) try: template = env. get_template (template_name) does T TemplateNotFound: raise TemplateNotFound (template_name) content = template. render (kwargs) return content # re-write BaseHandler to render class BaseHandler (tornado. web. requestHandler, TemplateRendering): "" Tornado RequestHandler subclass. "def initialize (self): pass def get_current_user (self): user = self. get_secure_cookie ('user') return user if user else None def render_html (self, template_name, ** kwargs): kwargs. update ({'Settings': self. settings, 'static _ url': self. settings. get ('static _ url_prefix', '/static/'), 'request': self. request, 'current _ user': self. current_user, 'xsrf _ token': self. xsrf_token, 'xsrf _ form_html ': self. xsrf_form_html,}) content = self. render_template (template_name, ** kwargs) self. write (content)

In this way, you can replace self. render with self. render_html in tornado.

For more articles about Tornado framework configuration using Jinja2 template engine, refer to PHP!

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.