Alternative Templates in Whirlwind

10 views
Skip to first unread message

JohnG

unread,
Feb 28, 2012, 12:35:50 PM2/28/12
to Whirlwind
Hey,

I'm working on another project based on Whirlwind. (Still my favourite
pythonic stack this side of Google App Engine.)

I'm trying to use use Tornado templates over Mako, try as I might I
can't get comfortable with Mako. My first approach was to do this:

class BaseRequestTornado(BaseRequest):
def render_template(self, template_file, **kwargs):
loader = template.Loader("/path/to/views")

self.write(loader.load(template_file).generate(menu=self.menu_lookup,
**kwargs))

So all that happens here is override the render_template with the
mine. It wasn't until I checked the code in BaseRequest that I noticed
it does quite a bit more (variables, middleware etc).

So the question is: Should i look at a clean solution to including
changeable templates into Whirlwind or is something that no ones
bothered for and whirlwind will always be Mako based?

Thanks
John

Matt Dennebaum

unread,
Feb 28, 2012, 2:18:17 PM2/28/12
to whirlw...@googlegroups.com
Hey John,

Glad to hear your still using whirlwind. We've been fairly quiet lately but still actively using and updating the code base. Would love to hear what you've done/doing with it (if your able to share).

To answer your question, alternate template backends would be cool. We're pretty happy with Mako so it hasn't been on our prioroty list but by all means that shouldn't mean you should be stuck with something you don't like.

The approach you started on seems like the way to go. You'll want to make sure you keep the middleware functionality in there. IE:

self.middleware_manager.run_view_hooks(view=kwargs)

There are also a few more whirlwind specific things happening in that function so you prob want to pick through and see what you care about. 

Submit a pull request if you get to a place your happy with. Happy to integrate it into the project if it's a clean solution. 

best,
Matt

JohnG

unread,
Feb 28, 2012, 4:10:23 PM2/28/12
to Whirlwind
Hi Matt,

I've kept an eye on github and seen there have been fixes which was
great to see.

The project I'm working on right now is a very simple website for a
company, and I'll be honest Whirlwind is totally overkill for what the
site needs. However I choose to use Whirlwind as a warm for something
more complex I'll be working on in a couple of months.

I've also been testing out WTForms with the stack and with just a
small amount of glue I got that working fine. It probably needs a more
complete solution, WTForm includes these for Django GAE etc.

Thanks for the feedback on templates I'll report back if I get
something working.

John

JohnG

unread,
Feb 29, 2012, 10:49:37 AM2/29/12
to Whirlwind
Found a very simple way to do what I need to do with no code changes
needed to request.py, here's the code:

# Psudeo Template Lookup that returns our Template Object
class TemplateLookupTornado(object):
def get_template(self, template_name):
return TemplateTornado(template_name)

# Psudeo Template that maps "render" to "generate"
class TemplateTornado(object):
def __init__(self, template_file):
loader = template.Loader(options.template_path)
self.template = loader.load(template_file)

def render(self, **kwargs):
return self.template.generate(**kwargs)

class BaseRequestTornado(BaseRequest):
def _get_template_lookup(self,extra_imports=None):
return TemplateLookupTornado()

So my BaseRequestTornado class over rides the internal
_get_template_lookup and returns an object TemplateLookupTornado class
that implements the get_template method, returning a TemplateTornado
object, So these classes map the mako calls for template, load and
render into Tornado ones. This meands the "render_template" call can
just execute as normal (and I get access to the xsrf stuff - which is
why I had to solve this problem).
Reply all
Reply to author
Forward
0 new messages