如何获取一个Django模板在Mako模板中呈现自己? [英] How can I get a Django Template to render itself within a Mako Template?

查看:127
本文介绍了如何获取一个Django模板在Mako模板中呈现自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们之前决定在Django项目中使用Mako Templates。我们还支持Django模板,因为很多可重用的应用程序(显然)假设Django Templating是可用的。我发现可以从Mako渲染Django模板,但是我还没有找到一种办法让它的工作方式相反。

We made the decision quite awhile ago to use Mako Templates in our Django project. We're also supporting Django Templates, since a lot of reusable apps (obviously) assume that Django Templating is available. I've found it possible to render Django Templates from Mako, but I haven't been able to find a way to make it work the other way around.

我刚刚将django文章添加到我们的应用程序列表中,它使用Django Templating。它假定 base.html 文件是一个覆盖的Django模板。不幸的是,我们的主要网站是使用Mako构建的。我还没有找到一个干净的方式Django模板到主机自己在Mako。

I've just added django-articles to our list of apps, and it uses Django Templating. It assumes that the base.html file is an overriden Django Template. Unfortunately, our main site is built using Mako. I can't yet figure out a clean way for Django Templates to host themselves within Mako.

我认为我想要的是一个模板标签,它会调用Mako并请求嵌入。

What I think I want, is a template tag that will call out to Mako and request to be 'embedded'.

我们的Mako模板目前正在执行以下操作:

Our Mako templates all currently do the following:

<%inherit file="mako/base.html"/>

我想要的是能够从Django模板中执行这样的操作:

What I want is to be able to do something like this from a Django Template:

{% render_in_mako 'mako/base.html' 'body' %}

在mako / base.html中有以下内容:

With the following in `mako/base.html':

</head>
  <body>
    <%include file="header.html" />

    ${next.body()}

  </body>
</html>

有没有人要这样做?我不太熟悉编写模板标签。你认为这可以做到吗?替代方案可能是重写Mako中的所有模板,这并不吸引我。一个模板标签对于使用Mako的很多项目来说将是非常有用的。

Has anyone had to do this before? I'm not very familiar with writing template tags. Do you think this can be done? The alternative is probably going to be re-writing all the templates in Mako, and that doesn't appeal to me at all. A template tag would be incredibly useful to a lot of projects that are using Mako I think.

推荐答案

好的问题!可以通过Django的自定义格式,在Django模板中渲染Mako模板模板标签。 Django文档更详细地解释了这个过程,但基本上你必须解析标签的输入,确保参数是有效的,然后创建一个 django.template.Node 对象。 Node 是一个对象,它接收你的模板标签参数,并定义一个 render()方法来处理它们。然而,您必须特别小心,以避免模板标签返回的文本,因为您绝对不希望HTML标记被转义。显然Django的人也考虑过这一点,而且在同一个文档中也有一段名为自动转义注意事项,解释了如何做。

Good question! Rendering a Mako template within a Django template can be done, thanks to Django's custom template tags. The Django docs explain the process in greater detail, but basically you'll have to parse the tag's input, ensure that the arguments are valid, and create a django.template.Node object from that. The Node is an object that takes your template tag arguments and defines a render() method to process them. You'll have to take special care, however, to escape the text your template tag returns, since you definitely don't want HTML tags to be escaped. Apparently the Django folks have considered this as well, and there's a section in the same doc called "Auto-escaping considerations" that explains how to do it.

一旦你弄清楚如何解析输入从模板标签(这真的只是验证参数),您可以定义 Node.render()方法来调用Mako的渲染函数,其中包含模板和上下文标签,并将输出返回到Django模板。当你用身体作为第二个参数时,我的描述有些困惑。这相对于Mako模板是什么意思?我必须承认我从来没有使用过Mako,但是快速浏览文档显示了与Django和Jinja2系统有许多相似之处。如果这个假设是错误的,我很抱歉。如果您可以通过Django模板标签文档进行操作,这似乎是一个合理的任务。祝你好运!

Once you figure out how to parse the input from the template tag (which is really just validating the arguments), you can define the Node.render() method to call Mako's rendering function with the template and context given in the tag, and return the output to the Django template. I was a bit confused by your description, though, when you used 'body' as the second argument. What does this mean, relative to the Mako template? I must admit I've never used Mako, but a quick glance at the documentation shows many similarities to the Django and Jinja2 systems; I apologize if this assumption is mistaken. If you can make it through the Django template tag docs, this seems like a reasonable undertaking. Best of luck!

这篇关于如何获取一个Django模板在Mako模板中呈现自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆