コンテキストで利用可能なデータによって決定される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は明らかに間違っています。テンプレートを動的に指定して、それを呼び出すには何が必要ですか?