2011-07-28 28 views
0

我通過Makotemplate讀取手動和看到如下代碼:爲什麼在mako中使用Context?

from mako.template import Template 
from mako.runtime import Context 
from StringIO import StringIO 

mytemplate = Template("hello, ${name}!") 
buf = StringIO() 
ctx = Context(buf, name="jack") 
mytemplate.render_context(ctx) 
print buf.getvalue() 

什麼好處使用右鍵?

回答

1

您可能不會直接使用它,它包含輸出緩衝區和可從模板內引用的變量字典。通常最好使用Templaterender方法。

>>> Template('hello ${name}!').render(name='jack') 
<<< u'hello jack!' 

您可以閱讀更多關於它here

相關問題