如何让 Django 模板在 Mako 模板中呈现自身?

发布于 2024-10-23 15:52:10 字数 843 浏览 1 评论 0原文

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

我刚刚将 django-articles 添加到我们的应用程序列表中,它使用 Django 模板。它假设 base.html 文件是一个覆盖的 Django 模板。不幸的是,我们的主站点是使用 Mako 构建的。我还无法找到一种干净的方式让 Django 模板在 Mako 中托管自身。

我想我想要的是一个模板标签,它将调用 Mako 并请求“嵌入”。

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

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

我想要的是能够从 Django 模板执行类似的操作:

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

在“mako/base.html”中包含以下内容:

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

    ${next.body()}

  </body>
</html>

以前有人必须这样做吗?我对编写模板标签不太熟悉。你认为这可以做到吗?另一种选择可能是在 Mako 中重写所有模板,但这对我来说根本没有吸引力。我认为模板标签对于许多使用 Mako 的项目来说非常有用。

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.

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.

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

Our Mako templates all currently do the following:

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

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

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

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

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

    ${next.body()}

  </body>
</html>

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.

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

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

发布评论

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

评论(1

兔小萌 2024-10-30 15:52:10

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

一旦弄清楚如何解析模板标签的输入(实际上只是验证参数),您就可以定义 Node.render() 方法来使用模板和上下文调用 Mako 的渲染函数在标签中给出,并将输出返回到 Django 模板。不过,当您使用“body”作为第二个参数时,我对您的描述有点困惑。相对于 Mako 模板,这意味着什么?我必须承认我从未使用过 Mako,但快速浏览一下文档就会发现它与 Django 和 Jinja2 系统有许多相似之处;如果这个假设是错误的,我深表歉意。如果您可以通过 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.

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!

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