apache freemarker-将列表呈现为表格

问题描述 投票:0回答:1

大家早上好,

我想知道是否有人可以帮助我完成这项工作?我正在尝试使用Apache Freemarker将ArrayList呈现为表。

想象下面的代码(java):

public static void main(String[] args) throws Exception {
// freemarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
cfg.setDirectoryForTemplateLoading(new File("./src/"));
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false);
;
Template temp = cfg.getTemplate("template.ftl");

Map m = Collections.singletonMap("names", Arrays.asList("foo", "bar", "baz", "qux", "quux",
"corge", "grault", "garply", "waldo", "fred",
"plugh", "xyzzy", "thud"
));

//
Writer out = new OutputStreamWriter(System.out);
temp.process(m, out);
}

使用此模板:

<Start>
<#list 0..names?size-1 as i>
${names[i]}
</#list>
<end>

它将像这样输出:

<Start>
foo
bar
baz
qux
quux
corge
grault
garply
waldo
fred
plugh
xyzzy
thud
<end>

我想知道freemarker是否有可能将其放在列中:

<Start>
foo        bar         baz
qux        quux        corge
grault     garply      waldo
fred       plugh       xyzzy
thud
<end>

有什么想法吗?任何建议都值得欢迎。

list freemarker
1个回答
0
投票

[?chunk可以将一个序列切成更小的序列(请参阅https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_chunk),因此,以HTML表格为例,您可以这样做:

<table>
  <#list names?chunk(3) as row>
    <tr>
      <#list row as name><td>${name}</td></#list>
    </tr>
  </#list>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.