如何使用免费标记获取列表中的第一项? [英] How can I get the first item in a list using free marker?
问题描述
我有以下代码,其中包含约12个项目,但我只需要检索第一个项目.如何显示列表中的第一项?
I have the following code which contains about 12 items but I only need to retrieve the first item. How can I display the first item in my list?
我的代码是:
<#list analysttest.rss.channel.item as item>
<div>
<h3 class="bstitle">${item.title}</h3>
<span class="bsauthor">${item.author}</span>
<span>${item.pubDate}</span>
<p>${item.description}</p>
</div>
</#list>
推荐答案
analysttest.rss.channel.item[0]
给出第一个项目,为了方便起见,您可以将其#assign
缩写为简称.请注意,至少必须有一项存在,否则您将得到一个错误. (或者,您可以执行<#assign item = analysttest.rss.channel.item[0]!someDefault>
之类的操作,其中someDefault
之类的''
,[]
,{}
等,取决于您的需要.甚至还有一个更短的<#assign item = analysttest.rss.channel.item[0]!>
形式,它使用多类型的通用"值作为默认值...请参见手册.)
analysttest.rss.channel.item[0]
gives the fist item, which you can #assign
to a shorther name for convenience. Note that at least 1 item must exist, or else you get an error. (Or, you can do something like <#assign item = analysttest.rss.channel.item[0]!someDefault>
, where someDefault
is like ''
, []
, {}
, etc, depending on what you need. There's even a shorter <#assign item = analysttest.rss.channel.item[0]!>
form, which uses a multi-typed "generic nothing" value as the default... see in the Manual.)
也可以列出,尽管只有一项是奇数:<#list analysttest.rss.channel.item[0..*1] as item>
,其中*1
表示最大长度为1(需要FreeMarker 2.3.21或更高版本).即使您有0个项目,此方法也有效(不输出任何内容).
Listing is also possible, though odd for only one item: <#list analysttest.rss.channel.item[0..*1] as item>
, where *1
means at most length of 1 (requires FreeMarker 2.3.21 or later). This works (and outputs nothing) even if you have 0 items.
这篇关于如何使用免费标记获取列表中的第一项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!