Getting mako exception while using include tag in mako template

299 Views Asked by At

I am trying to use <%include /> tag in mako template, but getting exception. I have all the files in the same directory. Can someone help me figure out what am I missing here?

$cat sample1.py
from mako.template import Template
fn = "sample1.txt"
templateDef = open(fn, "r").read()
t = Template(templateDef).render()
print(t)

$cat sample1.txt
<%include file="header.html"/>
hi
<%include file="footer.html"/>

$cat header.html 
<html>
header
</html>

$cat footer.html 
<html>
footer
</html>

This is the error I get:

$python3 sample1.py
mako.exceptions.TemplateLookupException: Template 'memory:0x2b80733bc3c8' has no TemplateLookup associated

Syntax wise I do not see any issue. Even though all the files are in the same directory, I can't get it to work. Any help?

1

There are 1 best solutions below

0
Lakshmi On

Use TemplateLookup function

a=TemplateLookup(directories=[‘dirname’])

Temp=a.get_template(‘filename’)

Print(Temp.render())

Visit https://docs.makotemplates.org/en/latest/usage.html#mako.lookup.TemplateLookup