我试图找到一种调用def模板的方法,该模板由上下文中可用的数据决定。在
编辑:同一问题的简单实例。在
可以在上下文中发出对象的值:
# in python
ctx = Context(buffer, website='stackoverflow.com')
# in mako
<%def name="body()">
I visit ${website} all the time.
</%def>
产生:
^{pr2}$我想允许根据数据定制输出。在
# in python
ctx = Context(buffer, website='stackoverflow.com', format='text')
# in mako
<%def name="body()">
I visit ${(format + '_link')(website)} all the time. <-- Made up syntax.
</%def>
<%def name='html_link(w)'>
<a href='http://${w}'>${w}</a>
</%def>
<%def name='text_link(w)'>
${w}
</%def>
在上下文中更改format
属性应该会更改
I visit stackoverflow.com all the time.
到
I visit <a href='http://stackoverflow.com'>stackoverflow.com</a> all the time.
我在body
def
中使用的合成语法显然是错误的。我需要什么来动态地指定一个模板,然后调用它?在
使用mako的
local
名称空间,但下面是一个有效的示例:根据需要,这会发出:
^{pr2}$如果您首先(从另一个模板:)生成模板,然后用您的数据运行该模板如何?在
相关问题 更多 >
编程相关推荐