回答
只有#foreach
。你將不得不在你的上下文中迭代一些東西。例如。使bar
可那是某種類型的數組或Collection
:
#foreach ($number in [1..34])
$number
#end
//謝謝!我不明白他們爲什麼沒有最基本的循環! – Moon 2011-04-16 01:06:47
foreach是最基本的一種循環。因爲是先進的。 – 2011-04-18 16:05:45
請注意,Velocity中的範圍是包含的,所以第二個示例將循環34次,'$ number'從1到34. – Starwarswii 2017-07-25 14:25:02
想添加foreach循環內部的迭代信息可以從訪問:或者,如果你想遍歷一個數字範圍
#foreach ($foo in $bar)
$foo
#end
特別$foreach
屬性:
#foreach ($foo in $bar)
count: $foreach.count
index: $foreach.index
first: $foreach.first
last: $foreach.last
#end
(我最後一次檢查last
包含一個錯誤,雖然)
正是我需要的。我需要在我的模板中編寫索引。感謝這篇文章! – 2013-05-26 03:56:29
如果你遇到Velocity 1.6(Confluence使用的fe),只有'$ velocityCount'和'$ velocityHasNext'可用(http://velocity.apache.org/engine/releases/velocity-1.6/user- guide.html#Loops) – sendmoreinfo 2014-06-17 07:21:10
你也可以從'$ foreach.parent.whatever'的嵌套循環中獲得這些變量,例如'$ foreach.parent.parent.index'從三重嵌套的foreach循環中獲取外部循環的索引。 – Starwarswii 2017-07-25 14:23:00
我找到了解決方案,當我試圖循環列表。 將該列表放入另一個類中,併爲列表obj創建getter和setter。 例如
public class ExtraClass {
ArrayList userList = null;
public ExtraClass(List l) {
userList = (ArrayList) l;
}
public ArrayList getUserList() {
return userList;
}
public void setUserList(ArrayList userList) {
this.userList = userList;
}
}
然後,對於速度環境,將Extraclass作爲輸入。 例如。
ExtraClass e = new ExtraClass(your list);
VelocityContext context = new VelocityContext();
context.put(「data」,e); 在模板
#foreach ($x in $data.userList)
$x.fieldname //here $x is the actual obj inside the list
#end
這適用於地圖嗎? – 2016-11-21 16:47:44
- 1. 速度模板foreach循環
- 2. 關於for循環(模板循環)
- 3. 燒瓶模板在for循環中
- 4. 在模板中做一個for循環
- 5. jinja模板for循環
- 6. 並行For循環與簡單for循環在C#中。速度測試
- 7. 在for循環中使用模數
- 8. 在Underscore.js模板中使用for循環和if語句?
- 9. Django - 使用for循環在模板中顯示圖像
- 10. Django模板語言:在其他語言中使用for循環
- 11. 如何在for循環中使用bc
- 12. 如何在for循環中使用datetime?
- 13. 如何在Firebird中使用For循環?
- 14. 如何在Arrays.asList中使用for循環()
- 15. Django的模板 - 在for循環
- 16. 模式使用for循環
- 17. Django - 如何在模板中解開元組''for'循環
- 18. Django模板:如何將項目保存在{%for%}循環中?
- 19. 如何使用for循環
- 20. 如何使用for循環
- 21. 如何爲循環使用HTML模板
- 22. 在模板中使用循環
- 23. 速度模板引擎中List的嵌套循環?
- 24. 如何在while循環的嵌套for循環中使用OpenMP?
- 25. 如何使用for循環在Java中打印x模式?
- 26. 使用Aurelia中的$ {$ index} for循環模板的另一部分
- 27. 使用數組for循環for循環
- 28. For循環for循環:擺錘建模
- 29. 在PHP中使用$ _GET for for循環
- 30. 在Java中使用Var for For循環
最好的答案是波紋管的折,通過@serg:http://stackoverflow.com/a/5683891/299843 – redben 2014-11-12 23:13:48