NVelocity 中的 For 循环

发布于 2024-09-11 15:04:23 字数 77 浏览 13 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

罪#恶を代价 2024-09-18 15:04:23

您可以在 foreach 循环中使用范围运算符 [n..m] 来模拟正常循环。您还可以通过常规方式访问多维数组元素,例如 $array[n][m]

例如,如果您有这样的 2d 数组(对不起,Java 代码):

String[][] testArray = new String[][] {{"a1","b1"},{"a2","b2"},{"a3","b3"}};

您可以在 Velocity 中循环它,如下所示:

#set($sizeX = $testArray.size() - 1)
#set($sizeY = $testArray[0].size() - 1)
#foreach($i in [0..$sizeX])
    #foreach($j in [0..$sizeY])
        e[$i][$j] = $testArray[$i][$j] <br/>
    #end
#end

哪个输出:

e[0][0] = a1
e[0][1] = b1
e[1][0] = a2
e[1][1] = b2
e[2][0] = a3
e[2][1] = b3 

更新

根据变更日志,显然括号语法仅在 Velocity 1.7b1 中引入。在旧版本中,我们只需要用 get(i) 替换括号,因为 Velocity 中的数组由 ArrayList(在 Java 中)支持。所以,这应该有效:

#set($sizeX = $testArray.size() - 1)
#set($sizeY = $testArray.get(0).size() - 1)
#foreach($i in [0..$sizeX])
    #foreach($j in [0..$sizeY])
        e[$i][$j] = $testArray.get($i).get($j) <br/>
    #end
#end

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):

String[][] testArray = new String[][] {{"a1","b1"},{"a2","b2"},{"a3","b3"}};

You can loop through it in Velocity like this:

#set($sizeX = $testArray.size() - 1)
#set($sizeY = $testArray[0].size() - 1)
#foreach($i in [0..$sizeX])
    #foreach($j in [0..$sizeY])
        e[$i][$j] = $testArray[$i][$j] <br/>
    #end
#end

Which outputs:

e[0][0] = a1
e[0][1] = b1
e[1][0] = a2
e[1][1] = b2
e[2][0] = a3
e[2][1] = b3 

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 by ArrayList (in Java). So, this should work:

#set($sizeX = $testArray.size() - 1)
#set($sizeY = $testArray.get(0).size() - 1)
#foreach($i in [0..$sizeX])
    #foreach($j in [0..$sizeY])
        e[$i][$j] = $testArray.get($i).get($j) <br/>
    #end
#end
浅浅 2024-09-18 15:04:23

遗憾的是,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...

素染倾城色 2024-09-18 15:04:23
Hashtable entries = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
entries["listtool"] = new ListTool();
VelocityContext context = new VelocityContext(entries);

在 listtool 类中,您编写 C# 代码来完成获取二维数组。

public Object get(Object list, int x,int  y)
    {
         return  ((IList)list)[x][y];
    }

$listtool.get($obj,x,y);
Hashtable entries = new Hashtable(StringComparer.InvariantCultureIgnoreCase);
entries["listtool"] = new ListTool();
VelocityContext context = new VelocityContext(entries);

in listtool class you write your C# code to finish the fetch 2 dimensional array.

public Object get(Object list, int x,int  y)
    {
         return  ((IList)list)[x][y];
    }

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