我正在使用Lithium自定义组件来创建撰写博客文章最多的用户列表。我有一个用户列表,这些用户已经写了博客文章,然后进行了电话 call 以获取每个这些用户所写的Blog文章的数量。
<#assign authors = rest("blogs/id/audiofile/posts")>
<#list authors.node_message_context.message.author as t>
<#assign count = rest("${t.@href}/posts/style/blog/count")>
<#assign orderedCount = count.value>
<#list orderedCount as c>
<ul>
<li>Blog posts ${c} userid ${t.@href}
</ul>
</#list>
</#list>
给出的输出
Blog posts 4 userid /users/id/2477
Blog posts 4 userid /users/id/2477
Blog posts 4 userid /users/id/2477
我的问题是如何删除此列表中的重复作者?
最佳答案
@Wolfgang Fahl希望我们可以修改Lithium REST API =)!
不幸的是,情况并非如此,因此我们必须处理所得到的,这可能是在做这样的事情:
<#-- this variable we need to store unique author ids -->
<#assign authorids = [] />
<#-- I'd use API v2 here, think for such stuff it's more powerful than v1 -->
<#assign query = 'SELECT author FROM messages WHERE conversation.style = "blog" AND board.id = "audiofiles"'?url />
<#assign response = rest("2.0", "/search?q=" + query) />
<#-- the response object will contain a list of blog post authors,
we loop trough them to get uniqe ids of every user that has written
a blog post, we need them later -->
<#list response.data.items as author>
<#-- make sure each author just gets added once -->
<#if !authorids?seq_contains(author.id)>
<#assign authorids = authorids + [author.id] />
</#if>
</#list>
<#-- now we loop trough the unique author ids and ask for the amount of
blog posts they have written -->
<ul>
<#list authorids as id>
<#assign query = 'SELECT count(*) FROM messages WHERE author.id = id AND board.id = "audiofiles"'?url />
<#assign response = rest("2.0", "/search?q=" + query) />
<li>User with ID ${id} has written ${response.data.count} blog posts</li>
</#list>
</ul>
代码未经测试,因此不能百分百确定它是否有效,但是我希望上面的代码使我选择的方法变得清晰...
关于freemarker - 在Lithium/Freemarker中删除重复项,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15925379/