在Tapestry组件模板中,是否有一种简单的方法来渲染一些标记X次,其中X是组件的参数?
我在Tapestry文档中找到的就是Loop组件:
<table class="navigation" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<tr>
<t:loop source="pageNames" value="pageName">
<td class="${tabClass}">
<t:pagelink page="pageName">${pageName}</t:pagelink>
</td>
</t:loop>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
但是,如果我只想渲染X次,而不需要传递任何参数,那就太过分了.对于这个用例,我真的希望像(伪代码):
<table class="navigation" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
<tr>
<t:loop times="${x}">
<!-- same markup every time -->
</t:loop>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
但似乎没有这样的东西存在 - 或者是这样吗?
现在我的解决方法是提供List的存根实现,它给出大小X,并将其用作我的循环源:
类:
private int x;
public List<Object> getX() {
return new AbstractList<Object>() {
public Object get(int arg0) {
return null;
}
public int size() {
return x;
}
};
}
Run Code Online (Sandbox Code Playgroud)
模板:
<t:loop source="x">
<!-- same markup each time -->
</t:loop>
Run Code Online (Sandbox Code Playgroud)
但这非常难看 - 当然有一种更好的办法可以做一些如此简单的事情吗?
这可以通过循环和tapestry的范围运算符来完成
Santa Claus said: <t:loop t:source="1..3">Ho</t:loop>
Run Code Online (Sandbox Code Playgroud)
http://tapestry.apache.org/property-expressions.html