• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

apache velocity #foreach loop

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to iterate through a string array, also want to print the iteration number,

output should be,
1. Not
2. my
3. fault

but when it produces,
${foreach.count} . Not
${foreach.count} . my
${foreach.count} . fault

Anyone can tell what's going wrong here ? on server console I get
INFO: Velocity [debug] Null reference [template '/Test.vm', line 3, column 13] : $foreach.count cannot be resolved.

Thanks
Bharat
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried "$foreach.count" instead of "$!{foreach.count}"?
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Ulf, I tried $foreach.count then it produces
$foreach.count . Not
$foreach.count . my
$foreach.count . fault

saw some stringe behavior when I use $!{foreach.count} output is

. Not
. my
. fault


~Bharat
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version? Is foreach's scope control set to false?
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using velocity-1.6.1.jar,velocity-tools-*-1.4.jar.
velocity.properties is

resource.loader = file
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = D:/eclipse/workspace/manager/WebContent/vm
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 2

Thanks,
Bharat
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally

$velocityCount is the answer.

Thanks verybody.
~Bharat
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know its beena while since the last reply on this thread, but i'll try my luck anyway -

I'm using velocity templates in my spring app n have some simple templates working successfully. Like passing a username and the reset password to the email body etc.
my next task is to learn how to pass a dynamic list of objects into the template.
say, for example, i will pass to the template an object array that contains the names of members. it may be 3 objects once and 5 the next.
i cannto seem to figure out a way to do this considering its my second day on velocity :-) can anyone please advise

cheers
p
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prashanth try this

prepare you template say userTemplate.vm


you have to put userList object in velocity context. for this do the following




 
prashanth talkad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much Bharat. Cheers - prashanth
reply
    Bookmark Topic Watch Topic
  • New Topic