Replacing template engine

676 views
Skip to first unread message

Alexandre

unread,
Jan 26, 2010, 9:24:53 PM1/26/10
to web2py
Hi,

I want to replace web2py's template engine with Jinja 2, anyone has some experience doing something similar? I'm trying to assign response._caller with a custom function, but I'm not having much success, is that the right way?

Thanks,
Alexandre Rosenfeld

Eng Comp 06 - USP São Carlos
FoG - http://fog.icmc.usp.br
IM Team - AIESEC

Thadeus Burgess

unread,
Jan 26, 2010, 10:05:26 PM1/26/10
to web...@googlegroups.com
Its going to be your best bet in using a different templating language. YOu will have to make sure to only parse your HTML requests and not json/download etc.

Basically, response._caller is a function, that returns a string which is the html.

So you would instead of response.render(d) you would do jinja2.render(d) where d is the dict returned from the action, you will of course have to tell jinja where to find templates and which template to render based on action

jinja.render(d, '/applications/init/views/' + request.action+'.html')

-Thadeus




--
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To post to this group, send email to web...@googlegroups.com.
To unsubscribe from this group, send email to web2py+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/web2py?hl=en.

Timothy Farrell

unread,
Jan 27, 2010, 10:36:11 AM1/27/10
to web...@googlegroups.com
IIRC, response._caller acts like a function decorator.  So it takes a function and returns a function.  I used to use Genshi like this:

def renderGenshi(func):
    def _render():
        output = func()
        if not isinstance(dict, output):
            return output
        else:
            # real Genshi render code goes here
            return Genshi.render(response._view, output)
    return _render

response._caller = renderGenshi

Naturally it was more complicated than that, but you get the idea.

That said, make sure Jinja2 is really what you want.  I thought I liked it until I learned that its context is immutable.  (This means you can't set response.title in a template and expect that change to propagate to an included or extended template file.  This was a deal-breaker for me.)

In case you're curious, I switched off of Genshi back to web2py templates because Genshi only works on normal cPython platforms.  It blows up on Jython or AIX Python.

-tim

Thadeus Burgess

unread,
Jan 27, 2010, 10:51:20 AM1/27/10
to web...@googlegroups.com
I dislike django-like templating languages.

I can't sit down and write a single simple django app without the need to make a templatetag whose whole purpose is to format the data so I can use djangos other broken tag models to display this data.

Django has the arugment "well our designers arn't programmers so we cant trust them to use python in the templates". Really? Seriously? I have yet to come across a django developer that doesn't write his own views one way or another. He might hire a web designer to make a site, but he still goes in afterwords and hacks the design into his templating language. And even then that web designer has their own knowledge of django.

/rant off

the template language is one of the most divine gifts that web2py has given us.

-Thadeus

mdipierro

unread,
Jan 27, 2010, 11:05:58 AM1/27/10
to web2py-users
I agree with Thadeus.

Anyway. You can use any template language you like but be aware you
need to test what happens when you print a helper and what happens
when a string needs escaping. Not all template languages behave in the
same way in this regard.

You will also lose the ability to bytecode compile your app using
web2py admin. This has a measurable effects on speed.

You will also lose the ability to distribute your app without
additional dependencies. Since the template language needs to be
installed and it non-trivial to package it with the app itself.

On Jan 27, 9:51 am, Thadeus Burgess <thade...@thadeusb.com> wrote:
> I dislike django-like templating languages.
>
> I can't sit down and write a single simple django app without the need to
> make a templatetag whose whole purpose is to format the data so I can use
> djangos other broken tag models to display this data.
>
> Django has the arugment "well our designers arn't programmers so we cant
> trust them to use python in the templates". Really? Seriously? I have yet to
> come across a django developer that doesn't write his own views one way or
> another. He might hire a web designer to make a site, but he still goes in
> afterwords and hacks the design into his templating language. And even then
> that web designer has their own knowledge of django.
>
> /rant off
>
> the template language is one of the most divine gifts that web2py has given
> us.
>
> -Thadeus
>

> > On Tue, Jan 26, 2010 at 8:24 PM, Alexandre <airm...@gmail.com> wrote:
>
> >> Hi,
>

> >>  I want to replace web2py's template engine with Jinja 2<http://jinja.pocoo.org/2/>,


> >> anyone has some experience doing something similar? I'm trying to assign
> >> response._caller with a custom function, but I'm not having much success, is
> >> that the right way?
>
> >>  Thanks,
> >> Alexandre Rosenfeld
>
> >> Eng Comp 06 - USP São Carlos

> >> FoG -http://fog.icmc.usp.br


> >> IM Team - AIESEC
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "web2py-users" group.
> >> To post to this group, send email to web...@googlegroups.com.
> >> To unsubscribe from this group, send email to

> >> web2py+un...@googlegroups.com<web2py%2Bunsu...@googlegroups.com>


> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/web2py?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "web2py-users" group.
> > To post to this group, send email to web...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > web2py+un...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/web2py?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "web2py-users" group.
> > To post to this group, send email to web...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > web2py+un...@googlegroups.com<web2py%2Bunsu...@googlegroups.com>

Massimo Di Pierro

unread,
Jun 8, 2012, 11:18:59 PM6/8/12
to web...@googlegroups.com
:-)

On Friday, 8 June 2012 13:04:43 UTC-5, kirpit wrote:
Since this discussion comes as the first result of "web2py jinja2" searches, there you go a simple integration:

Cheers,
kirpit

pbreit

unread,
Jun 9, 2012, 12:02:51 AM6/9/12
to web...@googlegroups.com
Using Jinja2 in Web2py seems like a very bad idea unless 1) you have lots of content already in Jinja2 or 2) you are doing it purely as an exercise.

kirpit

unread,
Jun 9, 2012, 2:35:04 AM6/9/12
to web...@googlegroups.com
Actually not, if you don't want your designers to run pure python and sandbox their templates.

Robin Manoli

unread,
Mar 23, 2015, 3:45:32 AM3/23/15
to web...@googlegroups.com
I find this to be an interesting idea, to use jinja2 as a sandbox.

Why is it that it makes it impossible to compile the web2py app? Is it impossible to do that because of external modules?

Massimo Di Pierro

unread,
Mar 23, 2015, 1:54:54 PM3/23/15
to web...@googlegroups.com
You can use jinja2 with web2py. I do not endorse it but you can, like you can use any template engine. Just replace

def index():
      return dict(a=1,b=2)

with

from jinja2 import Environment, PackageLoader

def index():
      env = Environment(loader=PackageLoader('yourapplication', 'templates')) 
      j2 = env.get_template('mytemplate.html')
      return j2.render(a=1,b=2)

mind that jinja2 is not smart enough to figure out web2py helpers and forms so if you have

     form = SQLFORM(...) # or other helper
     return dict(form = form)

you will have to do

     form = SQLFORM(...) # or other helper
     return j2.render(form = form.xml())

Anyway. I do not buy the argument that this sandboxes developers. Lots of damage can be done with JS code alone and this does not sandbox that. Moreover you are crippling your developers for a false sense of security. You simply need a policy about what can do in templates and a review process to make policy was followed.

The jinja2 template can be thought of a subset of the web2py template set. There is nothing that jinja2 can do that web2py templates cannot do already. The opposite is not true.

Massimo

Robin Manoli

unread,
Mar 23, 2015, 8:10:24 PM3/23/15
to web...@googlegroups.com
Doesn't it sandbox designers from accessing the files from the web2py file system? The subset is a feature in this case, even if it's not a complete security fix. (I would appreciate some example of those javascript issues).

I've also had trouble with nesting blocks (from different files) in web2py templates, as well as including views based on variable names. Even if it's fixable, those things are smoother with jinja.

The main reason I'm using jinja is still the sandboxing. If web2py would allow me to choose what python functions of the app that is available for designers, I'd probably stick to web2py templates. On the hand there are some nice features of jinja that are not written in web2py, even if they can be.

Also, I'm still wondering about the bytecode issue? Does bytecoding require to use web2py-style templates? Or have trouble with external modules? Or can you still bytecode the app for speed, but without bytecoding the templates? Does it matter if the controller renders a view, or does bytecode have any value if a controller returns a string?

Thanks for your help,
- Robin

Massimo Di Pierro

unread,
Mar 23, 2015, 11:05:19 PM3/23/15
to web...@googlegroups.com
yes. the sandbox prevents the code in templates from accessing the file system. What I am saying is that it is not the only problem you can have with templates and developer should check the code no matter what. A template could include, for example, code that allows the designer to steal credentials from other users.

Checking templates is easy. Just print all the strings in between {{...}} and see if they do something funny like opening files. Takes a second to check.

web2py templates work within the specs and are very fast. If you have any problem with them, please let us know and we can help with the syntax.

Anyway, this is a mater of taste. You may find this code useful:


It converts a jinja2 template to a web2py template. There may be exceptions.

Massimo

Ron Chatterjee

unread,
Mar 24, 2015, 11:30:42 AM3/24/15
to web...@googlegroups.com
Web2py template is the best you have out there. you will be silly to look for something else. My opinion.
Reply all
Reply to author
Forward
0 new messages