Java Velocity循环每3个项目添加div [英] Java Velocity loop add div around every 3 items

查看:181
本文介绍了Java Velocity循环每3个项目添加div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下foreach循环:

I'm using the following foreach loop

#foreach( $product in $allProducts )
    <p>$product</p>
#end

得到

<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>
<p>something</p>

是否有可能在Velocity中每3个项目包装一个div?例如,得到以下结果:

Is it possible to wrap a div every 3 items in Velocity? For example to get the following result:

<div>    
<p>something</p>
<p>something</p>
<p>something</p>
</div>
<div>
<p>something</p>
<p>something</p>
<p>something</p>
</div>

我试过使用 #if($ velocityCount%3 == 0) / code>在foreach循环中,但它不起作用。

I tried using #if ($velocityCount % 3 == 0) in the foreach loop but it doesn't work.

推荐答案

您没有告诉我们哪个Velocity版本你正在使用。 $ velocityCount 在最新版本中不存在(至少在1.7.x中)。尝试使用 $ foreach.index

You did not tell us which Velocity version you were using. $velocityCount does not exist anymore in last versions (at least in 1.7.x). Try with $foreach.index:

#foreach( $product in $allProducts )
    #if( $foreach.index %3 == 0 )
        #if( !$foreach.first )
        </div>
        #end
    <div>
    #end
    <p>$product</p>
    #if( $foreach.last )
    </div>
    #end
#end

这篇关于Java Velocity循环每3个项目添加div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆