为什么在mako中使用上下文?

2024-06-07 08:46:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

什么样的利润使用环境?在


Tags: 代码namefromimportmakocontexttemplate手册
1条回答
网友
1楼 · 发布于 2024-06-07 08:46:03

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

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

你可以阅读更多关于它的here。在

相关问题 更多 >

    热门问题