模板工具包:评估 WRAPPER 块内的模板语句?

发布于 2024-11-02 22:13:08 字数 773 浏览 4 评论 0原文

好吧,我已经看了两个多小时的手册,也试图在獾书中找到解决方案,但找不到任何有效的方法。

以下是包装器 (body.tt),您明白了:

[%- PROCESS 'const.tt' -%]
<?xml version="1.0" encoding="utf-8"?>
Loads of HTML
[%- content -%]
More HTML

包装的模板如下所示:

[% WRAPPER 'body.tt' %]
Other HTML
[%- bar -%]
More other HTML
[% END %]

最后 const.tt 如下所示:

[% bar = 'foo' %]

...并且由于某种原因,包装模板内的 bar 实例不会被评估。我有什么想法可以对其进行评估吗?

我尝试过:

[%- content | eval -%]

...这没有用。

请注意,在包装模板(上面的第二块)中,我希望能够从 const.tt 计算变量 bar ,而不必添加另一个 PROCESS ' const.tt' 到该模板。毕竟变量应该可以从 body.tt 中获得。

我忘了提及:模板工具包版本 2.22

Alright, I have looked over the manual for more than two hours now, also tried to find a solution in the badger book, but couldn't come up with anything that works.

The following is the wrapper (body.tt), well you get the idea:

[%- PROCESS 'const.tt' -%]
<?xml version="1.0" encoding="utf-8"?>
Loads of HTML
[%- content -%]
More HTML

The wrapped templates look like this:

[% WRAPPER 'body.tt' %]
Other HTML
[%- bar -%]
More other HTML
[% END %]

And finally const.tt looks like this:

[% bar = 'foo' %]

... and for some reason the instance of bar inside the wrapped template does not get evaluated. Any ideas how I could get that evaluated?

I have tried:

[%- content | eval -%]

... which did not work.

Note, that in the wrapped template (2nd block above) I want to be able to evaluate the variable bar from const.tt without having to add another PROCESS 'const.tt' to that template. After all the variable should be available from body.tt.

And I forgot to mention: Template Toolkit version 2.22

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

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

发布评论

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

评论(1

人事已非 2024-11-09 22:13:08

这就是为什么您尝试的方法不起作用。

作为 http://template-toolkit.org/docs/manual/Directives.html# section_WRAPPER 解释说,首先评估您的包装内容,然后使用作为 content 传入的已评估模板处理 body.tt。因此,contentcontent.tt 加载之前就已完成。

也就是说,有一种方法可以做到这一点,但它有点难看。这是您的内容:

%- PROCESS 'body.tt' -%]
[%- WRAPPER body -%]
Other HTML
[%- bar -%]
More other HTML
[% END %]

这是body.tt

[%- PROCESS 'content.tt' -%]
[% BLOCK body -%]
<?xml version="1.0" encoding="utf-8"?>
Loads of HTML
[%- content -%]
More HTML
[% END -%]

并且content.tt没有改变:

[% bar = 'foo' %]

虽然这有效,但我不保证必须这样做的人的理智以后维护一下。

Here is why what you tried doesn't work.

As http://template-toolkit.org/docs/manual/Directives.html#section_WRAPPER explains, your wrapped content is evaluated first, and then body.tt is processed with the already evaluated template passed in as content. Therefore content is finished before content.tt is loaded.

That said, there is a way to do it, but it is a little ugly. Here is your content:

%- PROCESS 'body.tt' -%]
[%- WRAPPER body -%]
Other HTML
[%- bar -%]
More other HTML
[% END %]

Here is body.tt:

[%- PROCESS 'content.tt' -%]
[% BLOCK body -%]
<?xml version="1.0" encoding="utf-8"?>
Loads of HTML
[%- content -%]
More HTML
[% END -%]

And content.tt did not change:

[% bar = 'foo' %]

While this works, I make no promises about the sanity of the person who has to maintain it later.

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