开发者

Velocity statement to add class to last menu item?

开发者 https://www.devze.com 2023-01-12 10:27 出处:网络
I have this velocity code for a simple menu: <div id=\"header\" class=\"navstrip\"> #foreach( $navItem in $navItems )

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?


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.


#for ($page in $pages)
 .....
  #if ($velocityHasNext), #end
#end

works pretty well.

0

精彩评论

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