如何在二维数组中显示自由标记列表元素

How to display free marker list elements in 2d array

我是 freemarker 模板的新手。有人可以帮我解决这个问题吗?

我正在从 Spring 控制器获取产品列表,我想在每行中显示 4 个产品。我使用 div 而不是 table。任何帮助表示赞赏。

下面的代码在每一行中显示一个元素。

<div class="container">
  <#list products as product>
      <div class="row">
          <div class="col-12">
              <div class="popular-products-slides owl-carousel">
                  <!-- Single Product section item starts here -->
                  <div class="single-product-wrapper">
                      <!-- Product Image -->
                      <div class="product-img">
                          <img src="${product.frontImageURL}" alt="" style="width: 200px; height: 400px;">
                          <!-- Hover Thumb -->
                          <img class="hover-img" src="${product.backImageURL}" alt="" style="width: 200px; height: 400px;">
                      </div>
                      <!-- Product Description -->
                      <div class="hover-content">
                          <div class="product-description">
                              <a href="${product.hyperLink}" target="_blank">
                                  <h4>${product.productName}</h4>
                              </a>
                          </div>
                      </div>
                  </div>
                  <!-- single product section item ends here-->
              </div>
          </div>
      </div>
  </#list>
</div>

#list

包围你的div
 <#list products as product>
  // your div
  </#list>

and to list the key-value pairs of a hash (since 2.3.25):

<#list hash as key, value>
Part repeated for each key-value pair
 </#list>

那我猜你在找 ?chunk。引用 https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_chunk 中的示例:

<#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>

<#list seq?chunk(4) as row>
  <#list row as cell>${cell} </#list>
</#list>

<#list seq?chunk(4, '-') as row>
  <#list row as cell>${cell} </#list>
</#list>

输出:

  a b c d
  e f g h
  i j

  a b c d
  e f g h
  i j - -