Freemarker:访问列表中的元素,也就是在列表中
Freemarker: Accessing elements in a list, which is in a list
我目前有以下结构:
List<List<ClusterEntry>> clusters = new ArrayList<List<ClusterEntry>>();
//fill clusters and the list in clusters
input.put("clusters", clusters);
clusters 描述了我的集群,cluster.get(i) 包含一个集群中的所有元素。
对于报告,我想将带有 freemarker 的结果输出到 .md 文件中
目标是拥有与集群一样多的列,并且在每一行中都有一个对应集群的条目。
这是我的 .ftl 代码:
<#list clusters as c> | Cluster ${c_index} |
<#list c[c_index] as entry> | ${entry.name} | </#list>
</#list>
然而,这会导致以下错误
The value you try to list is an extended_hash+string (package.ClusterEntry wrapped into f.e.b.StringModel), thus you must specify two loop variables after the "as"; one for the key, and another for the value, like <#... as k, v>).
如何正确访问条目?
如果你已经在这里阅读这篇文章,我怎样才能用这个嵌套循环正确地实现我的列行结构?目前我假设这都是单独的行
而不是 <#list c[c_index] as entry>
写 <#list c as entry>
,因为 c
已经包含来自 clusters
的当前列表项。
至于生成一个漂亮的降价 table,这是相当棘手的,因为在开始渲染任何东西之前你必须知道最长嵌套 List<ClusterEntry>
的长度。此外,如果它必须在源级别上看起来不错,您还必须计算出每列的最大宽度。在模板中计算这些东西不会很好......也许你应该在 Java 中这样做并将信息传递给数据模型中的模板。
我目前有以下结构:
List<List<ClusterEntry>> clusters = new ArrayList<List<ClusterEntry>>();
//fill clusters and the list in clusters
input.put("clusters", clusters);
clusters 描述了我的集群,cluster.get(i) 包含一个集群中的所有元素。 对于报告,我想将带有 freemarker 的结果输出到 .md 文件中 目标是拥有与集群一样多的列,并且在每一行中都有一个对应集群的条目。 这是我的 .ftl 代码:
<#list clusters as c> | Cluster ${c_index} |
<#list c[c_index] as entry> | ${entry.name} | </#list>
</#list>
然而,这会导致以下错误
The value you try to list is an extended_hash+string (package.ClusterEntry wrapped into f.e.b.StringModel), thus you must specify two loop variables after the "as"; one for the key, and another for the value, like <#... as k, v>).
如何正确访问条目? 如果你已经在这里阅读这篇文章,我怎样才能用这个嵌套循环正确地实现我的列行结构?目前我假设这都是单独的行
而不是 <#list c[c_index] as entry>
写 <#list c as entry>
,因为 c
已经包含来自 clusters
的当前列表项。
至于生成一个漂亮的降价 table,这是相当棘手的,因为在开始渲染任何东西之前你必须知道最长嵌套 List<ClusterEntry>
的长度。此外,如果它必须在源级别上看起来不错,您还必须计算出每列的最大宽度。在模板中计算这些东西不会很好......也许你应该在 Java 中这样做并将信息传递给数据模型中的模板。