开发者

Velocity and $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 stri开发者_运维问答ng 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?


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

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


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


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


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


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


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


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

Velocity and $foreach.count


$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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜