模板工具包 FOR 和 IF 行为
我在使用模板工具包时遇到奇怪的行为,假设我有一个数据结构(在 perl 中定义并作为参数传递):
( { FLAG => 1, some => data}, { some => data}, etc )
并且在模板中我有一个像
[FOR ITEMS]
[IF (FLAG) ]
do something
[ELSE]
do something else
[END]
[END]
模板一样的循环似乎永远不会输入 else,这是我可以获得的唯一方法输入 else 就是更改数据:
( { FLAG => 1, some => data}, { FLAG => 0, some => data}, etc )
使其计算结果为 false。 但是,如果我随后将数据结构更改为,
( { some => data}, { some => data}, etc )
它总是会输入 else (因为它评估 undef 为 false)。
看起来似乎有一些“溢出”的变量保留在下一次迭代的范围内,除非它们被覆盖,这是预期的行为吗?如果是这样,它是否记录在任何地方?
编辑: 后来我发现这是导致上述行为的另一个问题,这是由于我使用“ELSEIF”而不是模板中其他地方正确的“ELSIF”。 如果有疑问,请查看 jira 提出的解决方案,它解决了我在原始帖子中描述的问题。
I am experiencing weird behavior with template toolkit, say I have a data structure (defined in perl and passed as a parameter):
( { FLAG => 1, some => data}, { some => data}, etc )
and in a template I have a loop like
[FOR ITEMS]
[IF (FLAG) ]
do something
[ELSE]
do something else
[END]
[END]
the template seems to never enter the else, the only way I can get it to enter the else is to change the data:
( { FLAG => 1, some => data}, { FLAG => 0, some => data}, etc )
so that it evaluates to false.
However if I then change the data structure to
( { some => data}, { some => data}, etc )
It always enters the else (because it evaluates undef to be false).
It seems as if there is some 'bleed over' of variables remaining in scope for the next iteration unless they are overwritten, is this expected behavior? and if so is it documented anywhere?
EDIT:
Later on I found it was a different issue causing the above behavior, it was due to me using "ELSEIF" and not the correct "ELSIF" elsewhere in the template.
If in doubt check out the solution proposed by jira which solved the problem I described in my origional post.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像下面这样修改模板,将循环变量分配给命名哈希。然后它就会按照你的预期运行。
You can modify the template like below assigning the loop variable to a named hash. Then it will behave as you expect.