First of all an unclarity:
http://docs.pylonsproject.org/projects/pyramid/en/latest/changes.html
"""
A mako.directories setting is no longer required to use Mako templates
Rationale: Mako template renderers can be specified using an absolute
asset spec. An entire application can be written with such asset specs,
requiring no ordered lookup path.
"""
Does this mean it is not recommended to add this setting? The narative
documentation does mention the adding of this setting here:
http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/templates.html#templating-with-mako-templates
"""
For the above view callable to work, the following setting needs to be
present in the application stanza of your configuration’s ini file:
mako.directories = mypackage:templates
"""
At the moment I have not added this setting, but I did add the mako
renderer to __init__.py:
"""
from pyramid.mako_templating import renderer_factory as mako_factory
config.add_renderer('.mako', mako_factory)
"""
With the 1.3 alpha release I noticed that the scaffold is using the
config.scan combined with the decorators, so I tried to use my mako
templates in line with this setup (__init__.py):
"""
config.add_route('indices', '/indices/{area}/{date}')
"""
And in views.py:
"""
@view_config(route_name='indices', renderer='indices.mako')
def indices(request):
[...]
"""
The indices.mako template can be found in the (default) templates
directory. Visiting this view results in a
mako.exceptions.TopLevelLookupException (Cant locate template for uri
'indices.mako'). My question is what (and in wht file) should be set
differently or added to make the renderer find my template file?
When I do add the 'mako.directories = baseload:templates' to the ini
file there is a different message:
"""
Traceback (most recent call last):
File
"C:\Projects\Pyramid\lib\site-packages\pyramid\mako_templating.py",
line 154, in __call__
result = template.render_unicode(**system)
File "C:\Projects\Pyramid\lib\site-packages\mako\template.py", line
311, in render_unicode
as_unicode=True)
File "C:\Projects\Pyramid\lib\site-packages\mako\runtime.py", line
661, in _render
**_kwargs_for_callable(callable_, data))
File "C:\Projects\Pyramid\lib\site-packages\mako\runtime.py", line
693, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "C:\Projects\Pyramid\lib\site-packages\mako\runtime.py", line
719, in _exec_template
callable_(context, *args, **kwargs)
File "c:\projects\pyramid\baseload\baseload\templates/indices.mako",
line 17, in render_body
<link rel="stylesheet"
href="${request.static_url('static/css/style.css')}">
File "C:\Projects\Pyramid\lib\site-packages\pyramid\url.py", line 369,
in static_url
package = caller_package()
File "C:\Projects\Pyramid\lib\site-packages\pyramid\path.py", line 52,
in caller_package
module = caller_module(level+1)
File "C:\Projects\Pyramid\lib\site-packages\pyramid\path.py", line 26,
in caller_module
module = sys.modules[module_name]
KeyError: 'indices_mako'
"""
Thijs
This is necessary in order to do the normal ``renderer="mytemplate.mak")``
I learned this the hard way. I think it was present in an earlier
version of the standard scaffolds, but was removed at some point. At
least, it's in the Akhet scaffold, and I don't think I would have put
it there if it hadn't been in the standard scaffolds.
> """
> A mako.directories setting is no longer required to use Mako templates
> Rationale: Mako template renderers can be specified using an absolute
> asset spec. An entire application can be written with such asset specs,
> requiring no ordered lookup path.
> """
This means you can use an asset spec instead of a relative path.
``renderer="mypackage:templates/mytemplate.mak"``
Chameleon uses asset specs; the default scaffold has
``renderer="templates/my_template.pt"``, so I assume you can just
change the extension and create the template, and it should work.
http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/assets.html#asset-specifications
> from pyramid.mako_templating import renderer_factory as mako_factory
> config.add_renderer('.mako', mako_factory)
This is built into Pyramid so you don't need to add it. Both .mak and
.mako are registered for Mako. If you want to use another extension
like .html, then you have to add this line.
> File "C:\Projects\Pyramid\lib\site-packages\pyramid\path.py", line 26,
> in caller_module
> module = sys.modules[module_name]
> KeyError: 'indices_mako'
I got something like this when I put in an add_renderer line for .html
. It wasn't exactly the same error though, and after a couple tries it
seemed to go away on its own. I think the error was a
setuptools.require() something, which is related (it's indirectly an
import). I think I deleted my .pyc files and regenerated the package
metadata ("python setup.py egg_info") to get rid of any stale files,
but I can't be sure any of those fixed it. I know I didn't modify
anything in the Pyramid package.
By the way, I'm getting an incompatibility with
``mako.strict_undefined = true``. It raises an undefined error in the
debug toolbar on 'session' if you don't have Beaker sessions set up.
I'll do some more poking around and file a bug report.
--
Mike Orr <slugg...@gmail.com>
In order to locate the 'issue', I did a complete new install (new
virtualenv and easy_install pyramid). After the populate script, I tried
running the example from the scaffold which is using Chameleon. This
works, page is rendering fine, not issues whatsoever. One thing I did
notice is this entry in 'Settings' from the debug toolbar:
mako.directories []
However some checking cleared up that this is added by the toolbar
itself:
if not 'mako.directories' in config.registry.settings:
config.registry.settings['mako.directories'] = []
After simply copying mytemplate.pt to mytemplate.mako in the templates
directory (not sure whether parsing will be fine, but aiming at the
initial exception), the deault views.py is changed to point to the mako
template:
@view_config(route_name='home', renderer='templates/mytemplate.mako')
Running this setup leaves us with the same error as indicated before:
mako.exceptions.TopLevelLookupException: Cant locate template for uri
'templates/mytemplate.mako'
As suggested I added mako.directories to the development.ini, however
the exception above remains. But as this already contains the
'templates' folder I changed the renderer to file name only:
@view_config(route_name='home', renderer='mytemplate.mako')
And... success! Would this indeed be considered the recommended
configuration for Mako?
Thijs
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>
I know that ``mako.directories = mypackage:templates`` combined with
``renderer="mytemplate.mako"`` works. I don't know if
``renderer="templates/mytemplate.mako"`` works because I'm not an
expert in asset specs. But it seems to be what the documentation
implies. Perhaps the documentation needs to be made clearer or is
wrong about asset specs working for Mako at this time.
--
Mike Orr <slugg...@gmail.com>
Replace this: @view_config(route_name='home', renderer='templates/
mytemplate.mako')
With this: @view_config(route_name='home', renderer='/templates/
mytemplate.mako')
I am using the mako.directories config in the development.ini, & as my
templates are in sub-folders, I have to prefix those folder paths with
a forward-slash...
Cheers,
Bruce Coble
> >http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/ass...
>
> > > from pyramid.mako_templating import renderer_factory as mako_factory
> > > config.add_renderer('.mako', mako_factory)
>
> > This is built into Pyramid so you don't need to add it. Both .mak and
> > .mako are registered for Mako. If you want to use another extension
> > like .html, then you have to add this line.
>
> > > File "C:\Projects\Pyramid\lib\site-packages\pyramid\path.py", line 26,
> > > in caller_module
> > > module = sys.modules[module_name]
> > > KeyError: 'indices_mako'
>
> > I got something like this when I put in an add_renderer line for .html
> > . It wasn't exactly the same error though, and after a couple tries it
> > seemed to go away on its own. I think the error was a
> > setuptools.require() something, which is related (it's indirectly an
> > import). I think I deleted my .pyc files and regenerated the package
> > metadata ("python setup.py egg_info") to get rid of any stale files,
> > but I can't be sure any of those fixed it. I know I didn't modify
> > anything in the Pyramid package.
>
> > By the way, I'm getting an incompatibility with
> > ``mako.strict_undefined = true``. It raises an undefined error in the
> > debug toolbar on 'session' if you don't have Beaker sessions set up.
> > I'll do some more poking around and file a bug report.
>
> > --
> > Mike Orr <sluggos...@gmail.com>