Javascript Freemarker,列表索引和计数条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10139685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:02:13  来源:igfitidea点击:

Freemarker, list index and count condition

javascripthtmlfreemarker

提问by Will Hancock

I'm writing a JS Array of objects in a Freemarker template. But I am having serious issues not including a comma after the last item.

我正在 Freemarker 模板中编写一个 JS 对象数组。但是我遇到了严重的问题,最后一项后不包括逗号。

<#assign pages = module.pages.page>
wh.pages = [
<#list pages as page>
{"name" : "${page.@name}", "href" : "${page.@href}"}
<#if (index+1) < pages?size>,</#if>
</#list>
]

So during the list repeat, while index + 1 is less than the length/size of the pages variable, it should write a comma. So that when it equals the size, it should omit the comma.

所以在列表重复期间,当 index + 1 小于 pages 变量的长度/大小时,它应该写一个逗号。所以当它等于大小时,它应该省略逗号。

So how can this be achieved?

那么如何实现呢?

回答by darijan

Try with item_has_next

试试 item_has_next

In your example:

在你的例子中:

<#if pages_has_next>,</#if> 

回答by Will Hancock

Index has to be prefixed with your item name. In example:

索引必须以您的项目名称为前缀。例如:

<#if (index+1) < pages?size>,</#if>

should have been:

本来应该:

<#if (page_index+1) < pages?size>,</#if>

回答by abanmitra

I have a list of items and using that I am going to create a function with multiple parameters with the comma. My code is as follows:

我有一个项目列表,我将使用它创建一个带有多个带逗号的参数的函数。我的代码如下:

def ${function.name}(<#list function.params as param> ${param.name}:${param.paramType}<#if function.params?size != (param?index+1)>,</#if> </#list>)

it will generate the following output:

它将生成以下输出:

   def func1( input1:String,  input2:String )

回答by Wouter Van der Beken

this may be a simple answer

这可能是一个简单的答案

<#sep>,</#sep>