Python 为什么将上下文用于mako?

Python 为什么将上下文用于mako?,python,mako,Python,Mako,我阅读了Makotemplate的手册,看到了以下代码: 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) p

我阅读了Makotemplate的手册,看到了以下代码:

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()

使用上下文有什么好处?

您可能不会直接使用它,它包含输出缓冲区和可从模板中引用的变量字典。通常最好使用
模板
渲染
方法

>>> Template('hello ${name}!').render(name='jack')
<<< u'hello jack!'
>>模板('hello${name}!')。呈现(name='jack')