0

我阅读了 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()

使用上下文有什么好处?

4

1 回答 1

1

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

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

你可以在这里阅读更多关于它的信息。

于 2011-07-28T12:00:17.637 回答