2013-04-10 103 views
3

我正在使用Lithium自定義組件創建撰寫了大多數博客文章的用戶列表。我有一個寫過博客文章的用戶列表,然後通過休息電話來獲取每個用戶編寫的博客文章的數量。在Lithium/Freemarker中刪除重複項目

<#assign authors = rest("blogs/id/audiofile/posts")> 

<#list authors.node_message_context.message.author as t> 
<#assign count = rest("${[email protected]}/posts/style/blog/count")> 
<#assign orderedCount = count.value> 
<#list orderedCount as c> 
<ul> 
<li>Blog posts ${c} userid ${[email protected]} 
</ul> 
</#list> 
</#list> 

給出

Blog posts 4 userid /users/id/2477 

Blog posts 4 userid /users/id/2477 

Blog posts 4 userid /users/id/2477 

我的問題的輸出如何刪除此列表中的重複的作者嗎?

回答

0

的FAQ文章 http://freemarker.org/docs/app_faq.html#faq_modify_seq_and_map 概述爲什麼這是棘手的。基本上,它要求修改您的RESTful接口做的工作適合你:

<#assign authors = rest("blogs/id/audiofile/uniquauthors")> 

將是一個新的RESTful接口的例子,然後工作是在服務器端完成。

2

@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", "https://stackoverflow.com/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", "https://stackoverflow.com/search?q=" + query) /> 

    <li>User with ID ${id} has written ${response.data.count} blog posts</li> 
</#list> 
</ul> 

代碼是未經測試,所以不是100%肯定,如果它的工作原理,但我希望我選擇的方法通過上面的代碼變得清晰...