简体   繁体   中英

Why use Context with mako?

I reads manual by Makotemplate and see follows code:

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

What profit use Context?

You probably won't use it directly, it contains both the output buffer and a dictionary of variables that can be referenced from within the template. It's usually preferable to use the render method of Template .

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

You can read more about it here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM