tags:

views:

29

answers:

2

I have this velocity code for a simple menu:

<div id="header" class="navstrip">

  #foreach( $navItem in $navItems )
     <a href="$navItem.URL">$navItem.Title</a> |
  #end

</div>

I would like to give the last menu link a class of "last". What would the conditional be for that?

+1  A: 

Detecting last item is the most buggy spot in Velocity for some reason, I created 3 bug reports about it and even though it says they are solved - it still doesn't work perfectly to my knowledge.

If you are using Velocity 1.6 and below then there are following options:

1) Using loop tool

#foreach( $navItem in $loop.watch($navItems) )
    #if($loop.last) 
        last 
    #end
#end

But this doesn't work (see bug #1)

2) Using internal counter $velocityCount:

#foreach( $navItem in $navItems)
    #if($velocityCount == $navItems.size()) 
        last 
    #end
#end

This works.

In Velocity 1.7:

1) You should be able to just use $foreach.last:

#foreach( $navItem in $navItems)
    #if($foreach.last) 
        last 
    #end
#end

But this doesn't work again (see bug #2 and bug #3)

2) Comparing current counter to list size:

#foreach( $navItem in $navItems)
    #if($foreach.count == $navItems.size()) 
        last 
    #end
#end

This works.

Yeah, such simple task and so many troubles.

serg
Wow Serg, thanks for a comprehensive answer. I didn't find this info anywhere else...
Conando
@Conando Make it output `$foreach.count` or `$velocityCount` in a loop and see what it returns.
serg
Actually, neither answer seems to work though : )I have tried both #2 answers above in the following setup:`#foreach( $navItem in $navItems ) #if($foreach.count == $navItems.size()) <a href="$navItem.URL" class="navstrip last">$navItem.Title</a> #else <a href="$navItem.URL" class="navstrip" >$navItem.Title</a> #end #end`This always applies 'last'
Conando
Hi Serg - velocityCount outputs 1 - 7 correctly; $foreach.count just echos itself "$foreach.output"
Conando
I should add I working through an unusual interface: using PageLime for a client which uses velocity in the background to process navigation items.
Conando
@Conando If `$velocityCount` outputs correctly then how come it doesn't work? Try to output `$navItems.size()` then as well and see why it doesn't match. `$foreach.count` was introduced in 1.7b1. Are you sure you are using 1.7?
serg
navItems.size or navItems.size(), just outputs itself also. Weird. I think I give in on Velocity...have to find a different hack...
Conando
Since it's through PageLime, they haven't said which version and haven't heard anything yet on the forums there...
Conando
@Conando I am pretty sure it is 1.6 then is if not less. What is a type of `$navItems`, some `<ArrayList>` I assume? You just need to find a way to get its size.
serg
Though Thomas of PageLime now has emailed me so will update here...
Conando
This is what works for me Serg - #if ($velocityCount == $navItems.Count)
Conando
@Conando ok. $navItems must be some non standard collection, I assumed it implements `<List>`.
serg
A: 

Hi i am using struts tags in Velocity file but all Struts tags are hidden what is the problem

MyVelocity.vm

<%@ taglib prefix="s" uri="/struts-tags" %> Hello

stextfield ("label=Username" "name=username")

ranjan