如何在 Python 模板语言 Mako 中调用仅在运行时已知的名称的模板定义?

发布于 2024-07-23 12:09:25 字数 1115 浏览 1 评论 0原文

我正在尝试找到一种调用由上下文中可用数据确定的 def 模板的方法。

编辑:同一问题的一个更简单的例子。

可以在上下文中发出对象的值:

# in python
ctx = Context(buffer, website='stackoverflow.com')

# in mako
<%def name="body()">
I visit ${website} all the time.
</%def>

生成:

I visit stackoverflow.com all the time. 

我希望允许根据数据自定义输出。

# in python 
ctx = Context(buffer, website='stackoverflow.com', format='text')

# in mako
<%def name="body()">
I visit ${(format + '_link')(website)} all the time. <-- Made up syntax.
</%def>

<%def name='html_link(w)'>
<a href='http://${w}'>${w}</a>
</%def>

<%def name='text_link(w)'>
${w}
</%def>

更改上下文中的 format 属性应将输出从 更改

I visit stackoverflow.com all the time.

I visit <a href='http://stackoverflow.com'>stackoverflow.com</a> all the time.

我在 body def组成语法 code> 显然是错误的。 我需要什么来动态指定模板,然后调用它?

I am trying to find a way of calling def templates determined by the data available in the context.

Edit: A simpler instance of the same question.

It is possible to emit the value of an object in the context:

# in python
ctx = Context(buffer, website='stackoverflow.com')

# in mako
<%def name="body()">
I visit ${website} all the time.
</%def>

Produces:

I visit stackoverflow.com all the time. 

I would like to allow a customization of the output, based upon the data.

# in python 
ctx = Context(buffer, website='stackoverflow.com', format='text')

# in mako
<%def name="body()">
I visit ${(format + '_link')(website)} all the time. <-- Made up syntax.
</%def>

<%def name='html_link(w)'>
<a href='http://${w}'>${w}</a>
</%def>

<%def name='text_link(w)'>
${w}
</%def>

Changing the format attribute in the context should change the output from

I visit stackoverflow.com all the time.

to

I visit <a href='http://stackoverflow.com'>stackoverflow.com</a> all the time.

The made up syntax I have used in the body def is obviously wrong. What would I need to dynamically specify a template, and then call it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˇ宁静的妩媚 2024-07-30 12:09:25

需要对 mako 的 local 命名空间进行一些操作,但这里有一个工作示例:

from mako.template import Template
from mako.runtime import Context
from StringIO import StringIO

mytemplate = Template("""
<%def name='html_link(w)'>
<a href='http://${w}'>${w}</a>
</%def>
<%def name='text_link(w)'>
${w}
</%def>
<%def name="body()">
I visit ${getattr(local, format + '_link')(website)} all the time.
</%def>
""")

buf = StringIO()
ctx = Context(buf, website='stackoverflow.com', format='html')
mytemplate.render_context(ctx)
print buf.getvalue()

根据需要,它会发出:

I visit 
<a href='http://stackoverflow.com'>stackoverflow.com</a>
 all the time.

Takes some playing with mako's local namespace, but here's a working example:

from mako.template import Template
from mako.runtime import Context
from StringIO import StringIO

mytemplate = Template("""
<%def name='html_link(w)'>
<a href='http://${w}'>${w}</a>
</%def>
<%def name='text_link(w)'>
${w}
</%def>
<%def name="body()">
I visit ${getattr(local, format + '_link')(website)} all the time.
</%def>
""")

buf = StringIO()
ctx = Context(buf, website='stackoverflow.com', format='html')
mytemplate.render_context(ctx)
print buf.getvalue()

As desired, this emits:

I visit 
<a href='http://stackoverflow.com'>stackoverflow.com</a>
 all the time.
半暖夏伤 2024-07-30 12:09:25

如果您首先生成模板(从另一个模板:),然后使用您的数据运行它怎么样?

How about if you first generate the template (from another template :), and then run that with your data?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文