passing args to included template #395
-
A little cry for help. In the template named parent.tmpl I have included via the <%include /> tag the other template called member.tmpl, Have tried on multiple machines with different version of mako all same response, here are my templates and an example python file === makotemplates/parent.tmpl ===== <%include file="member.tmpl" args="mitem=member"/> % endfor === makotemplates/member.tmpl ===== ===== makoMWE2.py ===== create a dict of data to send into the templatemystruct = { mylookup = TemplateLookup(directories=['makotemplates'], strict_undefined=True) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi - First thing that will help, when posting code on github, use triple backticks for formatting. I had to copy and paste your code examples and manually guess where the indentation was. Next, in Python, when we have dictionary structures such as Finally, to declare arguments in a mako template that must be passed, use the page tag to declare those variables. your code can be formatted into a working demo as below from mako.lookup import TemplateLookup
lookup = TemplateLookup(strict_undefined=True)
lookup.put_string(
"parent.tmpl",
r"""
//----------------------------------------------------------------------
// ${dd['summary']}
//
{
% for item in dd['charlie']:
<%
if item['comment']:
comment = "// {0}".format(item['comment'])
else:
comment = ''
print("type(item.member) = {0}".format(type(item['member'])))
member = item['member']
print(type(member))
print(member)
%> ${comment}
<%include file="member.tmpl" args="mitem=member"/>
% endfor
};
""",
)
lookup.put_string(
"member.tmpl",
r"""
<%page args="mitem"/>
## item should be of type StructMemberInit
<%
print("type(mitem): {0}".format(type(mitem)))
if mitem['name']:
comment = "// {0}".format(mitem['name'])
else:
comment = ''
%> ${mitem['CS']} = ${mitem['value']}, ${comment}
""",
)
mystruct = {
"bob": [1, 2, 3, 4],
"charlie": [
{
"member": {"value": 0x430, "name": "address", "CS": 0x23},
"comment": "c1",
},
{
"member": {"value": 11, "name": "offset", "CS": 0x23},
"comment": "c2",
},
{"member": {"value": 4, "name": "size", "CS": 0x23}, "comment": "c3"},
],
"comment": "This is an example",
"summary": "This is a minimum working example",
}
mytemplate = lookup.get_template("parent.tmpl")
print(mytemplate.render(dd=mystruct)) |
Beta Was this translation helpful? Give feedback.
Hi -
First thing that will help, when posting code on github, use triple backticks for formatting. I had to copy and paste your code examples and manually guess where the indentation was.
Next, in Python, when we have dictionary structures such as
mystruct = {"bob": "charlie"}
, we use indexed access likemitem['ItemName']
, your example uses object syntax which does not work here based on the data you are passing.Finally, to declare arguments in a mako template that must be passed, use the page tag to declare those variables.
your code can be formatted into a working demo as below