尝试遍历项目的选项列表但不打印最后一个选项
Trying to loop through a list of options for an item but not print the very last option
<#list orderItem.options as option>
<tr>
<td class="order-item-detail">
${option.name} :
</td>
</tr>
<tr>
<td class="order-item-red">
${option.value}
</td>
</tr>
</#list>
上面的代码(在 html 内)是我用来循环遍历项目内的选项列表的代码。目前它循环遍历所有选项并打印它们。但是我想这样做,所以它不会打印此列表中的最终选项。
我有点受限,因为我必须单独通过 html 来完成此操作。我假设我需要某种类型的 if 语句来告诉它在到达最后一个选项时停止,或者具体告诉它在哪个选项内容处停止但我似乎无法弄清楚如何编写它。
option_index
给你当前选项的索引,?size
给你长度,你只需要将它们与 if 语句
进行比较
option_index
基于 0,因此您需要将大小减 1 才能不包括最后一项
注意 - 您也可以使用 option?index
来获取索引,具体取决于您使用的是哪个版本的 freemarker,但是 option_index
可以在较新的 freemarker 版本和旧版本中使用 [=23] =]
为了完整起见,我还将添加 ?is_last
,感谢 ddekany 的回答,用法 <#if option?is_last>
总之如果你有更新的freemarker版本,if语句可以这样写
已更新 - 假定 Freemarker 2.3.23 或更高版本
<#if option?is_last>
....
</#if>
原回答
<#list orderItem.options as option>
<#if option_index < orderItem.options?size - 1>
<tr>
<td class="order-item-detail">
${option.name} :
</td>
</tr>
<tr>
<td class="order-item-red">
${option.value}
</td>
</tr>
</#if>
</#list>
尺寸文档
https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_size
The number of sub variables in sequence (as a numerical value). The
highest possible index in sequence s is s?size - 1 (since the index of
the first subvariable is 0) assuming that the sequence has at least
one subvariable.
索引文档
https://freemarker.apache.org/docs/ref_builtins_loop_var.html#ref_builtin_index
Returns the 0-based index where the iteration (which is identified by
the loop variable name) currently stands.
您可以删除列表的最后一项(请注意,这会给已经空的列表带来错误):
<#list orderItem.options[0 ..< orderItem.options?size - 1] as x>
...
</#list>
或者,您可以使用 ?is_last
来检查您是否在最后一项,然后添加一个使用它的嵌套 #if
:
<#list orderItem.options as option>
<#if !option?is_last>
...
</#if>
</#list>
<#list orderItem.options as option>
<tr>
<td class="order-item-detail">
${option.name} :
</td>
</tr>
<tr>
<td class="order-item-red">
${option.value}
</td>
</tr>
</#list>
上面的代码(在 html 内)是我用来循环遍历项目内的选项列表的代码。目前它循环遍历所有选项并打印它们。但是我想这样做,所以它不会打印此列表中的最终选项。
我有点受限,因为我必须单独通过 html 来完成此操作。我假设我需要某种类型的 if 语句来告诉它在到达最后一个选项时停止,或者具体告诉它在哪个选项内容处停止但我似乎无法弄清楚如何编写它。
option_index
给你当前选项的索引,?size
给你长度,你只需要将它们与 if 语句
option_index
基于 0,因此您需要将大小减 1 才能不包括最后一项
注意 - 您也可以使用 option?index
来获取索引,具体取决于您使用的是哪个版本的 freemarker,但是 option_index
可以在较新的 freemarker 版本和旧版本中使用 [=23] =]
为了完整起见,我还将添加 ?is_last
,感谢 ddekany 的回答,用法 <#if option?is_last>
总之如果你有更新的freemarker版本,if语句可以这样写
已更新 - 假定 Freemarker 2.3.23 或更高版本
<#if option?is_last>
....
</#if>
原回答
<#list orderItem.options as option>
<#if option_index < orderItem.options?size - 1>
<tr>
<td class="order-item-detail">
${option.name} :
</td>
</tr>
<tr>
<td class="order-item-red">
${option.value}
</td>
</tr>
</#if>
</#list>
尺寸文档
https://freemarker.apache.org/docs/ref_builtins_sequence.html#ref_builtin_size
The number of sub variables in sequence (as a numerical value). The highest possible index in sequence s is s?size - 1 (since the index of the first subvariable is 0) assuming that the sequence has at least one subvariable.
索引文档
https://freemarker.apache.org/docs/ref_builtins_loop_var.html#ref_builtin_index
Returns the 0-based index where the iteration (which is identified by the loop variable name) currently stands.
您可以删除列表的最后一项(请注意,这会给已经空的列表带来错误):
<#list orderItem.options[0 ..< orderItem.options?size - 1] as x>
...
</#list>
或者,您可以使用 ?is_last
来检查您是否在最后一项,然后添加一个使用它的嵌套 #if
:
<#list orderItem.options as option>
<#if !option?is_last>
...
</#if>
</#list>