如何将数组传递给 mako 模板?
How do you pass an array to a mako template?
我想将字符串数组传递给 Mako 的渲染方法,但这不起作用:
from mako.template import Template
mytemplate = Template(filename="some.template")
str = mytemplate.render(first="John", last="Smith", nicknames=[
"JJ", "Johnny", "Jon"])
print(str)
some.template =
Hello ${first} ${last}
Your nicknames are:
% for n in ${nicknames}:
${n}
% endfor
在“%”行中,您正在编写不需要转义的常规 Python 代码:
% for n in nicknames:
${n}
% endfor
我想将字符串数组传递给 Mako 的渲染方法,但这不起作用:
from mako.template import Template
mytemplate = Template(filename="some.template")
str = mytemplate.render(first="John", last="Smith", nicknames=[
"JJ", "Johnny", "Jon"])
print(str)
some.template =
Hello ${first} ${last}
Your nicknames are:
% for n in ${nicknames}:
${n}
% endfor
在“%”行中,您正在编写不需要转义的常规 Python 代码:
% for n in nicknames:
${n}
% endfor