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.