Re: How to configure Jinja2 properly?

519 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Gabor Lenard

nieprzeczytany,
24 lip 2011, 05:51:0124.07.2011
do webapp2
Sorry, I was wrong. My solution to update the template path doesn't even work. :)

So the real question would be: how to re-configure the template path (when the handler is not in the root directory of the app)?



On 24.07.2011, at 11:18, Gabor Lenard wrote:

I like to keep my handlers in a directory called "handlers", not in the root dir of the app. Because of that I have to change the default configuration of Jinja2, otherwise it will try to look up templates in "handlers/templates". So I change the path in my base handler like this:

class BaseHandler(webapp2.RequestHandler):
  
  @webapp2.cached_property
  def jinja2(self):
      # Returns a Jinja2 renderer cached in the app registry.
      j2 = jinja2.get_jinja2(app=self.app)
      j2.config['template_path'] = join(dirname(dirname(__file__)), 'templates')
      return j2

However, in spite of the @cached_property decorator, this code is called at every page request (tested by logging it) which bothers me.

Why is the @cached_property decorator not working? 
Or what would be the proper way to configure Jinja2?

Thanks,
Gabor

Rodrigo Moraes

nieprzeczytany,
24 lip 2011, 06:05:2624.07.2011
do webapp2
On Jul 24, 6:51 am, Gabor Lenarwrote:
> So the real question would be: how to re-configure the template path (when the handler is not in the root directory of the app)?

Hi,
In this case you must configure an absolute path. For example:

import os

curr_path = os.path.abspath(os.path.dirname(__file__))

config = {}
config['webapp2_extras.jinja2'] = {
'template_path': os.path.join(curr_path, 'subdir',
'subsubdir', 'templates'),
}
app = webapp2.WSGIApplication(..., config=config)

Let me know if you have problems.

-- rodrigo

Rodrigo Moraes

nieprzeczytany,
24 lip 2011, 06:13:3524.07.2011
do webapp2
For some reason I missed your first message.

Notice that configuration is set primarily in the application. See:

http://webapp-improved.appspot.com/guide/app.html#config

By convention, modules that are configurable in webapp2 use the module
name as key and the configuration is set in a nested dict. So, e.g.,
i18n, jinja2 and sessions are configured like this:

config = {}
config['webapp2_extras.i18n'] = {
'default_locale': ...,
}
config['webapp2_extras.jinja2'] = {
'template_path': ...,
}
config['webapp2_extras.sessions'] = {
'secret_key': ...,
}
app = webapp2.WSGIApplication(..., config=config)

You only need to set the configuration that differs from the default
one. By convention, configurable modules have a 'default_config'
variable just for the purpose of documenting the default values, e.g.:

http://webapp-improved.appspot.com/api/extras.i18n.html#webapp2_extras.i18n.default_config

Hope this helps.

-- rodrigo

Gabor Lenard

nieprzeczytany,
24 lip 2011, 06:23:0724.07.2011
do web...@googlegroups.com

On 24.07.2011, at 12:13, Rodrigo Moraes wrote:

> Notice that configuration is set primarily in the application. See:


Thank you very much, that has solved my issue!

Gabor

JimJty

nieprzeczytany,
21 wrz 2011, 13:35:3521.09.2011
do webapp2
Yes that helped me too. I missed the configuration convention that you
mention. I am sure it is somewhere in the documentation, but maybe
also adding a reference on the jinja2 doc page (http://webapp-
improved.appspot.com/api/webapp2_extras/jinja2.html#api-webapp2-extras-
jinja2)

Thanks!
>    http://webapp-improved.appspot.com/api/extras.i18n.html#webapp2_extra...
Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0