This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I recently played around with the Velocity template language available in the Roller Weblogger software powering blogging sites such as JRoller.com and was disappointed to realize that I wasn't able to access the elements of an array in anyway other than using a foreach loop. This is what the documentation suggests:
However, this approach fails because the object returned by the get() method is of type String. So, I needed a way of getting the first element of the array. I couldn't figure out what would do it. I tried square brackets, get(0), and what not. This is what I got working:
I wonder... Is there a better way? There has to be, right?
Thanks Mike. I actually searched the Velocity mailing list (again, this time with more patience) and found this message confirming my fears... I also noticed that RC1 for Velocity 1.4 is out, but decided not to read the change log as the JRoller.com site is not going to upgrade in a while anyway.
Joined: Jan 23, 2002
Originally posted by Junilu Lacar: Took a quick look at the documentation you linked to and the only thing I can think of to suggest is to refactor a bit and "extract to a macro" the logic for displaying the first element.
Umm. I didn't think of that. I'll go and test it right away.