速度和 $foreach.count
我使用的是速度 1.7,并且在 foreach 循环中我想打印计数。在模板中,我在 #foreach/#end 部分中有以下字符串:
Count: $foreach.count
并期望在渲染结果中看到类似的内容
Count: 1
...
Count: 2
...
,但我看到的是:
Count: $foreach.count
...
Count: $foreach.count
...
有什么想法我做错了什么吗?
I am using velocity 1.7 and within a foreach loop I want to print the count. In the template I have the following string in a #foreach/#end section:
Count: $foreach.count
and was expecting to see in the rendered result something like
Count: 1
...
Count: 2
...
but all I see is:
Count: $foreach.count
...
Count: $foreach.count
...
Any ideas what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
$foreach.count
和$counter
都不适合我。这个答案建议使用
$ velocityCount
,它对我有用。Neither
$foreach.count
nor$counter
worked for me.This answer suggests using
$velocityCount
, and it worked for me.您的代码是部分的,我们没有看到 foreach 指令。
另外,我知道 foreach 循环有一个名为
$counter
的内置变量,尽管在指南中它们确实引用了$foreach.count
Your code is partial, we don't see the foreach directive.
Else, I know that the foreach loop has a built-in variable called
$counter
, though in the guide they do refer to$foreach.count
我尝试使用
$counter
&$foreach.count
但这些都不适合我。但是,
$velocityCount
标记有效,下面是示例。输入代码:
输出:
I tried with
$counter
&$foreach.count
but neither of these worked for me.However, the
$velocityCount
tag worked and below is the example.Input code:
Output:
我不知道为什么名为 $count 的 foreach 循环内置变量不能作为指南参考。但 $velocityCount 对我有用。
在velocity.properties文件中有一个名为directive.foreach.counter.name的属性velocityCount,因此默认的$count变量可能不起作用。
I do not know why the foreach loop built-in variable called $count is not working as guide refer. But $velocityCount is worked for me.
There is property called directive.foreach.counter.name is velocityCount in velocity.properties file, so default $count variable may not be working.
k.honsalis 的答案已被弃用。
此时,您只能使用 $velocityCount,即使文档将引用已弃用的方法。
k.honsalis answer is deprecated.
At this point you can only use $velocityCount, even though the documentation will refer to deprecated methods.
默认变量是velocityCount,但如果需要,您可以更改变量名称和初始值(仅在之前的2.0版本中)。
http://people.apache.org/~henning/velocity/htmlsingle/ VelocityUsersGuide.html
The default variable is velocityCount, but you can change the variable name and initial value (only in prior 2.0 versions) if you want.
http://people.apache.org/~henning/velocity/htmlsingle/VelocityUsersGuide.html
我目前正在像这样格式化我的
email_html.vm
。请注意,我正在使用
#set( $count = 1 )
和
#set( $count = $count + 1 )
输出
I am currently formatting my
email_html.vm
like so.Note, I am using
#set( $count = 1 )
and
#set( $count = $count + 1 )
Output
$velocityCount
对我有用,我正在使用速度 1.5$foreach.count
&$计数器
$velocityCount
is works for me and i'm using velocity 1.5$foreach.count
&$counter
$foreach.count
(以1
开头)和$foreach.index
(以0
开头)对我有用速度 2.3。文档中提到了更多可用的循环变量。
$foreach.count
(starts with1
) and$foreach.index
(starts with0
) worked for me with Velocity 2.3.More available loop variables are mentioned in the docs.