速度和 $foreach.count

发布于 2024-12-08 03:41:40 字数 315 浏览 0 评论 0原文

我使用的是速度 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 技术交流群。

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

发布评论

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

评论(9

懷念過去 2024-12-15 03:41:40

$foreach.count$counter 都不适合我。

这个答案建议使用 $ velocityCount,它对我有用。

Neither $foreach.count nor $counter worked for me.

This answer suggests using $velocityCount, and it worked for me.

通知家属抬走 2024-12-15 03:41:40

您的代码是部分的,我们没有看到 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

顾忌 2024-12-15 03:41:40

我尝试使用 $counter & $foreach.count 但这些都不适合我。

但是,$velocityCount 标记有效,下面是示例。

输入代码:

#foreach($entry in $entries)    
    <p>In for Loop count is : $velocityCount</p>     
#end    

输出:

In for Loop count is : 1

In for Loop count is : 2

In for Loop count is : 3

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:

#foreach($entry in $entries)    
    <p>In for Loop count is : $velocityCount</p>     
#end    

Output:

In for Loop count is : 1

In for Loop count is : 2

In for Loop count is : 3
清欢 2024-12-15 03:41:40

我不知道为什么名为 $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.

对风讲故事 2024-12-15 03:41:40

k.honsalis 的答案已被弃用。

此时,您只能使用 $velocityCount,即使文档将引用已弃用的方法。

#foreach($item in $items)
counter 0: $foreach.index
counter 1: $foreach.count
counter 2: $counter
counter 3: $velocityCount
#end

Output:

$foreach.index
$foreach.count
$counter
1

k.honsalis answer is deprecated.

At this point you can only use $velocityCount, even though the documentation will refer to deprecated methods.

#foreach($item in $items)
counter 0: $foreach.index
counter 1: $foreach.count
counter 2: $counter
counter 3: $velocityCount
#end

Output:

$foreach.index
$foreach.count
$counter
1
稚然 2024-12-15 03:41:40

默认变量是velocityCount,但如果需要,您可以更改变量名称和初始值(仅在之前的2.0版本中)。

VelocityEngine engine = new VelocityEngine();
engine.setProperty("directive.foreach.counter.name", "velocityCount");
engine.setProperty("directive.foreach.counter.initial.value", 1);

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.

VelocityEngine engine = new VelocityEngine();
engine.setProperty("directive.foreach.counter.name", "velocityCount");
engine.setProperty("directive.foreach.counter.initial.value", 1);

http://people.apache.org/~henning/velocity/htmlsingle/VelocityUsersGuide.html

奈何桥上唱咆哮 2024-12-15 03:41:40

我目前正在像这样格式化我的 email_html.vm

请注意,我正在使用

#set( $count = 1 )

#set( $count = $count + 1 )

<html>
<body>
<table style="border: 1px solid black; border-collapse: collapse">
    #set( $count = 1 )
    #foreach( $film in $filmList )
        <tr>
            <td colspan=2 style="background: bisque; text-align: center"><b>Movie $count</b></td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Title</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getTitle() </td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Synopsis</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getSynopsis() </td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Trailer</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getTrailerLink() </td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">More Information</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">
                https://www.landmarktheatres.com/$film.getMoreInfoLink() </td>
        </tr>
        #set( $count = $count + 1 )
    #end
</table>
</body>
</html>

输出

在此处输入图像描述

I am currently formatting my email_html.vm like so.

Note, I am using

#set( $count = 1 )
and
#set( $count = $count + 1 )

<html>
<body>
<table style="border: 1px solid black; border-collapse: collapse">
    #set( $count = 1 )
    #foreach( $film in $filmList )
        <tr>
            <td colspan=2 style="background: bisque; text-align: center"><b>Movie $count</b></td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Title</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getTitle() </td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Synopsis</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getSynopsis() </td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">Trailer</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left"> $film.getTrailerLink() </td>
        </tr>
        <tr>
            <th style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">More Information</th>
            <td style="border: 1px solid black; border-collapse: collapse; padding: 5px; text-align: left">
                https://www.landmarktheatres.com/$film.getMoreInfoLink() </td>
        </tr>
        #set( $count = $count + 1 )
    #end
</table>
</body>
</html>

Output

enter image description here

初与友歌 2024-12-15 03:41:40

$velocityCount 对我有用,我正在使用速度 1.5 $foreach.count & $计数器

$velocityCount is works for me and i'm using velocity 1.5 $foreach.count & $counter

仲春光 2024-12-15 03:41:40

$foreach.count (以 1 开头)和 $foreach.index (以 0 开头)对我有用速度 2.3

文档中提到了更多可用的循环变量。

$foreach.count (starts with 1) and $foreach.index (starts with 0) worked for me with Velocity 2.3.

More available loop variables are mentioned in the docs.

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