我试图找到一种调用由上下文中可用数据确定的 def 模板的方法。
编辑:同一问题的更简单实例。
可以在上下文中发出对象的值:
# in python
ctx = Context(buffer, website='stackoverflow.com')
# in mako
<%def name="body()">
I visit ${website} all the time.
</%def>
产生:
I visit stackoverflow.com all the time.
我想允许根据数据自定义输出。
# 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
显然是错误的。我需要什么来动态指定一个模板,然后调用它?