如何删除 Template Toolkit 中的变量?
查看模板工具包手册的 Template::Manual::VMethods 部分,我不知道没有看到任何方法可以做到这一点。另外,将 undef
分配给变量也不起作用 - variable.define
在事后返回 true。
Looking at Template::Manual::VMethods section of the Template Toolkit manual I don't see any method doing this. Also assigning undef
to variable doesn't work - variable.defined
returns true after the fact.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,谷歌搜索“删除变量”站点:mail.template-toolkit.org/pipermail/templates/带来了问题[模板] 我可以“DELETE some_var”吗? 来自 Felipe Gasper,来自 Petr 的两个答案丹尼利克.彼得建议:
Well, googling
"delete variable" site:mail.template-toolkit.org/pipermail/templates/
brought the question [Templates] Can I “DELETE some_var”? from Felipe Gasper with two answers from Petr Danihlik. Petr suggests:我查看了 Catalyst::View: TT 代码,以便理解变量上下文。
我总结了以下子例程,它负责渲染工作:
TT
process()
是通过$c->stash
中的变量副本调用的,那么为什么要这样做呢?我们需要搞乱$c->stash
来删除本地副本吗?也许我们不知道。此外,TT
define()
VMethod 与其他方法一样,似乎是为列表构建的。当对标量调用 VMethod 时,标量会自动提升为单元素列表:也许由于这个原因,IF 测试总是返回 true。我对携带
DBIx::Class::ResultSet
对象引用的变量进行了一些测试,这似乎在测试变量时有效:第一行删除变量,第二行进行正确的测试。
更新
如果您可以添加
EVAL_PERL => 1
标志在 Catalyst 视图的config()
参数内,然后您可以在模板中使用
[% RAWPERL %]
指令,这使您可以直接访问Template::Context
对象:那么您可以删除变量,并且.define()
VMethod 会做正确的事情。I looked at Catalyst::View:TT code, in order to understand variables context.
The following subroutine, which I summarized a little bit does the rendering work:
TT
process()
is called with copies of variables in$c->stash
, so why do we need to mess with$c->stash
to get rid of a local copy? Maybe we don't.Moreover, TT
define()
VMethod, like other methods, seem to have been built for lists. Scalars are auto-promoted to single element list when a VMethod is called on them: maybe for this reason the IF test returns always true.I did some tests with variables carrying references to
DBIx::Class::ResultSet
objects, and this seems to work while testing for a variable:The first line deletes the variable, and the second does proper test.
UPDATE
If you can add
EVAL_PERL => 1
flag in your Catalyst View, insideconfig()
argumets,then you can use
[% RAWPERL %]
directive in templates, which gives you direct access to theTemplate::Context
object: then you can delete vars and.defined()
VMethod does the right thing.