Velocity语句将类添加到最后一个菜单项?

时间:2010-08-22 22:16:32

标签: java velocity

我有一个简单菜单的速度代码:

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

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

</div>

我想给最后一个菜单链接一个“最后”的类。有条件的是什么?

2 个答案:

答案 0 :(得分:9)

由于某种原因,检测最后一项是Velocity中最多的错误点,我创建了3个关于它的错误报告,即使它说它们已经解决了 - 但它仍然不能完全符合我的知识。

如果您使用 Velocity 1.6 及以下,则有以下选项:

1)使用循环工具

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

但这不起作用(see bug #1

2)使用内部计数器$velocityCount

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

这很有效。

Velocity 1.7

1)您应该只能使用$foreach.last

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

但这不再有效(请参阅bug #2bug #3

2)将当前计数器与列表大小进行比较:

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

这很有效。

是的,这么简单的任务和很多麻烦。

答案 1 :(得分:2)

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

工作得很好。