Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 386
yannisf Avatar asked Oct 04 '11 09:10

yannisf


People also ask

What is Apache Velocity used for?

Velocity can be used to generate web pages, SQL, PostScript and other output from templates. It can be used either as a standalone utility for generating source code and reports, or as an integrated component of other systems.


4 Answers

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

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

like image 180
summerbulb Avatar answered Sep 20 '22 19:09

summerbulb


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

like image 24
k.honsali Avatar answered Sep 21 '22 19:09

k.honsali


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
like image 28
Sid Mannem Avatar answered Sep 22 '22 19:09

Sid Mannem


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.

like image 31
Nagaraju Badaeni Avatar answered Sep 21 '22 19:09

Nagaraju Badaeni