5

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?

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
Sheena
  • 15,590
  • 14
  • 75
  • 113
  • Have you tried this with a leading `/` when defining your xxx call in the template? Or using raw strings in your python code? – bdiamante Apr 19 '13 at 14:25
  • @bdiamante: That did the trick :) Post it as an answer so I can give you some points – Sheena Apr 20 '13 at 05:39

2 Answers2

7

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

bdiamante
  • 15,980
  • 6
  • 40
  • 46
  • 1
    Could you share a full, working example? Trying to get my head around the Mako templating but it's not awfully simple :) – Per Lundberg Apr 12 '16 at 20:44
  • 1
    This little quirk stumped my project for the last few hours. Thanks for sharing this tip! For those wondering what you actually do: Simply add a leading `/` to the template name in your include statement (like this - `<%include file="/xxx.mako" />` – Sundar Aug 02 '22 at 05:52
6

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 ...

Ricky Levi
  • 7,298
  • 1
  • 57
  • 65