Mustache Templates

238 views
Skip to first unread message

Artemi Krymski

unread,
Feb 28, 2011, 7:04:33 AM2/28/11
to python-...@googlegroups.com
I'm interesting in getting mustache templating engine to work under
tornado ( http://mustache.github.com/ )

Anyone has any experience with adding alternative template engines to tornado?

In particular I'm interested in emitting python code like template.py
already does

Ben Darnell

unread,
Feb 28, 2011, 4:32:22 PM2/28/11
to python-...@googlegroups.com, Artemi Krymski
On Mon, Feb 28, 2011 at 4:04 AM, Artemi Krymski <akry...@gmail.com> wrote:
I'm interesting in getting mustache templating engine to work under
tornado ( http://mustache.github.com/ )

Anyone has any experience with adding alternative template engines to tornado?

It should just work.  Simply call self.write(pystache.render()) instead of self.render()

 

In particular I'm interested in emitting python code like template.py
already does

Why?  tornado.template generates code so that you can use arbitrary python expressions in your templates, but Mustache doesn't allow that.  

-Ben

Artemi Krymski

unread,
Mar 1, 2011, 7:18:16 AM3/1/11
to Ben Darnell, python-...@googlegroups.com
For speed reasons - I'm afraid mustache will be quite slow the way
it's implemented.

But I just love the clean separation of code and logic, and the fact
that I can use the same templates within javascript

Joe Bowman

unread,
Mar 1, 2011, 8:39:29 AM3/1/11
to python-...@googlegroups.com, Ben Darnell
If a lot of your page views can be cached by something like Squid, then the speed of your template engine matters less.

Artemi Krymski

unread,
Mar 2, 2011, 5:09:13 AM3/2/11
to python-...@googlegroups.com, Joe Bowman, Ben Darnell
cant cache much, it's a realtime app

Cliff Wells

unread,
Mar 2, 2011, 5:31:31 AM3/2/11
to python-...@googlegroups.com
So... what are you asking for? You mentioned generating Python code so
I assumed that pystache had some related capability and was going to
offer some suggestions for caching the resulting bytecode, but I just
perused the source code and it appears to be a simple regex-based
engine, no code generation in sight.

I'm entirely unclear on what you hoped to hear.

Regards,
Cliff

Artemi Krymski

unread,
Mar 2, 2011, 5:41:21 AM3/2/11
to python-...@googlegroups.com, Cliff Wells
it would be a matter of writing a mustache engine in python that generates code a la tornado, as an alternative syntax for tornado templates

ideally it would guess the template engine to use based on the file extension instead of hard coding

Cliff Wells

unread,
Mar 2, 2011, 7:52:34 AM3/2/11
to python-tornado
On Wed, 2011-03-02 at 10:41 +0000, Artemi Krymski wrote:
> it would be a matter of writing a mustache engine in python that
> generates code a la tornado, as an alternative syntax for
> tornado templates

This is a project all in itself and may not actually make a lot of sense
for Moustache anyway. Based on my admittedly brief overview of the code,
it appears to do all of its analysis outside the template using regular
expressions. That is, there is no Python code inside the template to
compile. Tornado's default template engine actually allows embedding
of Python code inside the template, so it is a different beast
altogether.

In any case, even if pystache is somewhat slow, there are lots of ways
to work around this. If raw speed was the primary way to make a website
fast, then there would be exactly zero Ruby on Rails websites in
existence. But of course we know there are plenty, some of them with a
large amount of traffic.

For a lot of good ideas, I'd suggest reading this article:

http://kovyrin.net/2007/08/05/using-nginx-ssi-and-memcache-to-make-your-web-applications-faster/

Even just using the SSI trick will make a big improvement, since it can
drastically cut down the amount of HTML you have to generate
dynamically.

Bottom line is, if you like Moustache, then use it. Just use it wisely.

> ideally it would guess the template engine to use based on the file
> extension instead of hard coding

That's not really necessary. You can use any template engine you want
with Tornado. As Ben pointed out, you simply call whatever method the
engine provides to render a template. I use Breve with Tornado and it
requires no integration at all.

Regards,
Cliff


Reply all
Reply to author
Forward
0 new messages