Question

Background:

I have the following directory structure:

/absolute/path/to/templates
            components/
                   component1.mak
                   component2.mak
            template1.mak

The plan is for the templates in the template directory to include a bunch of components.

in template1.mak I have something like:

 <%include file="xxx.mak"/>

where xxx is either components/component1.mak or just component1.mak (I tried both with different results, detailed below)

mylookup = TemplateLookup(directories=[yyy,])
oTemplate = Template(filename='/path/to/templates/template1.mak', lookup=mylookup)
oTemplate.render(args)

where yyy is either '/absolute/path/to/templates' or '/absolute/path/to/templates/components'

The Problem:

No matter what combination of xxx and yyy values I use I get a template lookup exception. The value of yyy (the lookup path) doesn't seem to have any effect of the exception.

If xxx (the include tag path) is 'components\component1.mak' the error says it can't find the file /absolute/path/to/templates/components/component1.mak. And if xxx is just 'component1.mak' then the error is it can't find /absolute/path/to/templates/component1.mak.

The Question

How can I get mako to include the stuff in the components directory? And what am I missing or not understanding about template lookup?

Was it helpful?

Solution

Try putting a leading / when defining your xxx call in the template.

OTHER TIPS

Just wanted to provide a more informative answer - the path is really depends on which file executed the code.

example 1

web/file.cgi              // was called first from the web
web/templates/header.mako // was called somewhere in file.cgi

In this case, the lookup dir should be:

mylookup = TemplateLookup(directories=['templates/'])

example 2

// was called first from the web
web/file.cgi                 

// was imported and called in file.cgi
../somewhere/in-PYTHONPATH/mymodules/modulename.py

// was called from modulename.py
../somewhere/in-PYTHONPATH/mymodules/templates/edit.html 

Then your lookup dir should actually be:

 mylookup = TemplateLookup(directories=['../../../../somewhere/in-PYTHONPATH/mymodules/templates/'])
 OR
 mylookup = TemplateLookup(directories=['/fullpath/to/somewhere/in-PYTHONPATH/mymodules/templates/'])

You needed the fullpath because web/file.cgi isn't located in a relative path with the modules in the backend ...

As for some reason, Mako Templates doesn't know to lookup in the relative directory to the mymodules/modulename.py file of where it called the mymodules/templates/edit.html not sure why ( or maybe it's just my older version of Mako Templates )

The <%include file="xxx.mak"/> though, behave different, as it does looking up relativly to where the file is found, this is why the / pulled it a one level up - and then it looked into templates/ .. but it again, it doesn't work the same for modules somewhere in the server ....

hope it helps others ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top