mako templates in Pyramid 1.3.1+

119 views
Skip to first unread message

Adrien

unread,
Jun 29, 2012, 9:07:35 AM6/29/12
to pylons-discuss
Hi list,

I'm getting an error with the 'render_to_response' function for mako
templates with pyramid >= 1.3.1.

The following code worked for versions <=1.3.0:

## In __init__.py:

config.add_route('pages', '/p/{pagename}.html')

## In views.py:

@view_config(route_name='pages')
def rendertemplates(request):
pagename = request.matchdict['pagename']

result = render_to_response('myproject:templates/
%s.mako'%pagename,
{},
request=request
)

return result

# index.mako:
<%inherit file="base.mako" />

<p> hello index.html </p>

# base.mako:
<html>
<body>
${next.body()}
</body>
</html>


I'm using this very simple setup (well, templates are a little bit
more complicated) to serve quasi-static pages with the benefit of mako
templates.

This setup worked up to Pyramid 1.3.0.
Now I get a lookup error:
"""
TemplateLookupException: Cant locate template for uri u'myproject
$templates/base.mako'
"""

If I use an asset(?) for the inheritance section in index.mako then it
works again:

#index.mako:
<%inherit file="myproject:base.mako" />


Is this a new mako templates inheritance convention? Is there a way to
get rid of the "myproject:" asset in templates? This would help
upgrading the Pyramid version without changing all mako templates.

I tried to set "mako.directories = myproject:templates" in
development.ini but it didn't change anything.

By the way, is there a security issue using the content
request.matchdict['pagename'] for rendering templates without escaping
special chars?

Thanks!
Adrien

Blaise Laflamme

unread,
Jun 29, 2012, 9:56:23 AM6/29/12
to pylons-...@googlegroups.com
It's an already known problem and should be fixed for next version, look here:


However it's also a good habit to use asset spec, I personally prefer it over relative path most of the time.

Adrien

unread,
Jun 30, 2012, 4:49:07 AM6/30/12
to pylons-discuss
On 29 juin, 15:56, Blaise Laflamme <bla...@laflamme.org> wrote:
> It's an already known problem and should be fixed for next version, look
> here:
>
> Mako Lookup Exception for Namespace in 1.3.1<https://github.com/Pylons/pyramid/issues/606>

Thanks you Blaise for the link. I'll use assets for my next
templates.

Adrien

Graham Higgins

unread,
Jun 30, 2012, 9:40:04 AM6/30/12
to pylons-...@googlegroups.com
On Fri, 2012-06-29 at 06:56 -0700, Blaise Laflamme wrote:
> It's an already known problem and should be fixed for next version.

Works for me when hand-added to 1.4dev.

> However it's also a good habit to use asset spec, I personally prefer
> it over relative path most of the time.

Could you explain a little more why the asset approach is a "good
habit"? It's specificity to Pyramid seems to run counter to re-use
principles.


--
Graham Higgins

http://bel-epa.com/gjh/
signature.asc

Blaise Laflamme

unread,
Jun 30, 2012, 11:11:16 AM6/30/12
to pylons-...@googlegroups.com, g...@bel-epa.com
Hi Graham,

if you are using relative templates with inheritance and/or includes you'll probably get the error. It seems somehow the URI of the inherited template already gets : replaced by $ (mako probably bet this) and then tries to render the template bypassing the conditional looking for not being abspath and containing a semi-colon.

For asset specs it's just personal preferences, I use a lot of templates not relative to the same root within the same project or across multiple packages and sometimes mix up mako-jinja2-chameleon, so that's a simple and well supported way to do it and it works with all supported template languages. The other way would be to provide all template paths to mako.directories but in that case won't support other template languages until you do the same for all of them.

It's also the way to override assets so it makes sense to use the same way of specifying assets across my projects(s). It's not a global principle for all frameworks but I think it could be considered a good one for pyramid.
Reply all
Reply to author
Forward
0 new messages