模板工具包:评估 WRAPPER 块内的模板语句?
好吧,我已经看了两个多小时的手册,也试图在獾书中找到解决方案,但找不到任何有效的方法。
以下是包装器 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是为什么您尝试的方法不起作用。
作为 http://template-toolkit.org/docs/manual/Directives.html# section_WRAPPER 解释说,首先评估您的包装内容,然后使用作为
content
传入的已评估模板处理body.tt
。因此,content
在content.tt
加载之前就已完成。也就是说,有一种方法可以做到这一点,但它有点难看。这是您的内容:
这是
body.tt
:并且
content.tt
没有改变:虽然这有效,但我不保证必须这样做的人的理智以后维护一下。
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 ascontent
. Thereforecontent
is finished beforecontent.tt
is loaded.That said, there is a way to do it, but it is a little ugly. Here is your content:
Here is
body.tt
:And
content.tt
did not change:While this works, I make no promises about the sanity of the person who has to maintain it later.