NVelocity 中的 For 循环
NVelocity 支持 #for 循环吗? 我浏览了文档,我所能找到的只是#foreach 循环。
我想循环一个二维数组。
Does NVelocity support #for loops?
I've looked through the documentation and all I could find was the #foreach loop.
I want to loop over a 2 dimensional array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 foreach 循环中使用范围运算符
[n..m]
来模拟正常循环。您还可以通过常规方式访问多维数组元素,例如$array[n][m]
。例如,如果您有这样的 2d 数组(对不起,Java 代码):
您可以在 Velocity 中循环它,如下所示:
哪个输出:
更新:
根据变更日志,显然括号语法仅在 Velocity 1.7b1 中引入。在旧版本中,我们只需要用
get(i)
替换括号,因为 Velocity 中的数组由ArrayList
(在 Java 中)支持。所以,这应该有效:You can use range operator
[n..m]
in foreach loop to emulate normal loop. You can also access multidimensional array elements in a usual way like$array[n][m]
.For example if you have such 2d array (sorry for Java code):
You can loop through it in Velocity like this:
Which outputs:
UPDATE:
Apparently bracketed syntax was introduced only in Velocity 1.7b1 according to changelog. In older versions we would just need to replace brackets with
get(i)
as arrays in Velocity are backed byArrayList
(in Java). So, this should work:遗憾的是,NVelocity“按原样”不支持 for 循环,仅支持 foreach。甚至 Castle Project 的 fork 也只改进了 foreach 循环。
AFAIK,对于 .NET 项目来说,NVelocity 已经走进了死胡同。我们在我们的项目中使用它,使用与lonely7345类似的代码来解决它的缺点,并且我们一直使用它,因为直到最近,.net还没有更好或更简单的模板引擎。
但是,我们考虑使用 Razor 作为独立模板引擎...
Alas, NVelocity "as is" does not support for loops, only foreach. Even Castle Project's fork improves only foreach loop.
AFAIK, for .NET projects NVelocity is on a dead-end. We are using it in our projects, using code not unlike lonely7345 to address its shortcomings, and we kept using it because, until recently, there was no better or easier templating engine for .net.
However, we are considering using Razor as a Standalone templating engine...
在 listtool 类中,您编写 C# 代码来完成获取二维数组。
in listtool class you write your C# code to finish the fetch 2 dimensional array.