哪个是在App Engine上使用jinja2的首选方法? [英] Which is the preferred method to use jinja2 on App Engine?

查看:126
本文介绍了哪个是在App Engine上使用jinja2的首选方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初在App Engine上使用App Engine上的示例实现了Jinja2: https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates 直接导入jinja2:

  import jinja2 
import os

jinja_environment = jinja2.Environment(
loader = jinja2.FileSystemLoader(os.path.dirname(__ file__)))

class MainPage(webapp2.RequestHandler):
def get(self):
greetings ='somestring'
template_values = {
'greetings':greetings,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))

但我目前正在使用Simpleauth( https://github.com/crhym3/simpleauth ),它遵循Nick Johnson在这里描述的实现: http://blog.notdot.net/2011/11/Migrating-to-Python-2-7-part- 2-Webapp-and-templates ,其中jinja2从webapp2_extras导入:

  import os 
import webapp2
from webapp2_extras import jinja2
$ b $ class BaseHandler(webapp2.RequestHandler):
@ webapp2.cached_property
def jinja2(self):
return jinja2.get_jinja2 (app = self.app)

def render_template(self,filename,** template_args):
self.response.write(self.jinja2.render_template(filename,** template_args))

class IndexHandler(BaseHandler):
def get(self):
self.render_template('index.html',name = self.request.get('name')) )

哪些是首选的m使用jinja2的方法?(他们似乎并没有很好地合作,并且倾向于标准化为最佳选项。) / div>

我想他们几乎是一样的。此外,webapp2_extras.jinja2另外还会缓存jinja2.Environment()初始化(对于请求持续时间)。另外,您可以利用webapp2的配置/注册表系统。



查看 get_jinja2()源代码,你会发现它只是jinja2.Environment()的一个方便的包装,带有一些默认的环境参数和启用的扩展名(例如i18n)。


I originally implemented Jinja2 on App Engine using the examples shown on the App Engine site here: https://developers.google.com/appengine/docs/python/gettingstartedpython27/templates where jinja2 is imported directly:

import jinja2
import os

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):
        greetings = 'somestring'
        template_values = {
            'greetings': greetings,
        }
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

But I'm currently bolting on Simpleauth (https://github.com/crhym3/simpleauth) which follows the implementation that Nick Johnson described here: http://blog.notdot.net/2011/11/Migrating-to-Python-2-7-part-2-Webapp-and-templates where jinja2 is imported from webapp2_extras:

import os
import webapp2
from webapp2_extras import jinja2

class BaseHandler(webapp2.RequestHandler):
  @webapp2.cached_property
  def jinja2(self):
        return jinja2.get_jinja2(app=self.app)

  def render_template(self, filename, **template_args):
        self.response.write(self.jinja2.render_template(filename, **template_args))

class IndexHandler(BaseHandler):
  def get(self):
    self.render_template('index.html', name=self.request.get('name'))

Which of these is the preferred method for using jinja2? (They don't seem to play together nicely, and would prefer to standardize on the best option.)

解决方案

I guess they are pretty much the same. What webapp2_extras.jinja2 does in addition is that it caches jinja2.Environment() initialization (for the request duration). Plus, you can leverage config/registry system of the webapp2.

Looking at get_jinja2() source you'll see that it's just a handy wrapper for jinja2.Environment() with some default environment args and enabled extensions (e.g. i18n).

这篇关于哪个是在App Engine上使用jinja2的首选方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆