Python's Tornado framework configuration uses the JINJA2 template engine

Source: Internet
Author: User
JINJA2 is a built-in template engine in the popular web framework flask, and similar to the Django template engine, here's a look at how the Jinja2 template engine is configured for the Python Tornado framework

Tornado default has a template engine but the function is simple (in fact I can use almost) is rather cumbersome to use, and the JINJA2 syntax is similar to the Django template so decided to use him.

Download JINJA2

or download it with PIP (it's really cool)

Pip Install JINJA2

This makes it possible to use the.

Tornado and JINJA2 Integration

Tornado and JINJA2 integration is very simple (in fact, online search is relatively simple), do not know where to find the anyway found, do not say directly on the code

#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) except Templatenotfound:raise Templatenotfound (template_n AME) content = Template.render (Kwargs) return content # is to re-write Basehandler rendered by Jinja2 template class Basehandler (TORNADO.WEB.R  Equesthandler, 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.cu Rrent_user, ' Xsrf_token ': Self.xsrf_token, ' xsrf_form_html ': self.xsrf_form_html,}) content = Self.render _template (Template_name, **kwargs) self.write (content)

This allows you to replace Self.render with self.render_html in tornado.

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.