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