【问题标题】:Why use Context with mako?为什么将 Context 与 mako 一起使用?
【发布时间】:2011-07-28 11:48:37
【问题描述】:

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

使用上下文有什么好处?

【问题讨论】:

    标签: python mako


    【解决方案1】:

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

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

    您可以阅读更多关于它的信息here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-30
      • 2021-04-08
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多