【问题标题】:How do you pass an array to a mako template?如何将数组传递给 mako 模板?
【发布时间】:2021-09-27 11:56:06
【问题描述】:

我想将一个字符串数组传递给 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 templates mako


    【解决方案1】:

    在 '%' 行中,您正在编写不需要转义的常规 Python 代码:

    % for n in nicknames:
    ${n}
    % endfor
    

    【讨论】:

      猜你喜欢
      • 2011-05-08
      • 2012-10-14
      • 2011-12-26
      • 2019-12-17
      • 1970-01-01
      • 2013-05-14
      • 2012-10-09
      • 2019-02-08
      • 1970-01-01
      相关资源
      最近更新 更多