我想找到一種方法來調用由上下文中可用的數據確定的def模板。如何在Python模板語言Mako中使用僅在運行時已知的名稱調用模板定義?
編輯:一個簡單的同一個問題的實例。
,能夠發出在上下文對象的值:
# 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
使用顯然是錯誤的。我需要什麼來動態指定一個模板,然後調用它?
你能描述你的目標是什麼?很明顯,這是令人費解的 - 並且有一些你想要完成的事情可能會更簡單。這是什麼原因呢? – 2009-05-29 02:25:42