首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >删除Lithium/Freemarker中的重复项

删除Lithium/Freemarker中的重复项
EN

Stack Overflow用户
提问于 2013-04-10 20:07:57
回答 2查看 1.8K关注 0票数 4

我正在使用Lithium自定义组件来创建写过最多博客文章的用户列表。我有一个用户列表,这些用户写了一篇博客文章,然后进行了rest调用,以获取每个用户写的博客文章的数量。

代码语言:javascript
复制
<#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>

给出一个输出

代码语言:javascript
复制
Blog posts 4 userid /users/id/2477

Blog posts 4 userid /users/id/2477

Blog posts 4 userid /users/id/2477

我的问题是如何删除此列表中的重复作者?

EN

回答 2

Stack Overflow用户

发布于 2015-08-11 21:46:25

@Wolfgang希望我们可以修改Lithium REST API =)!

不幸的是,情况并非如此,所以我们必须处理我们得到的东西,这可能是这样做的:

代码语言:javascript
复制
<#-- 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>

代码是未经测试的,所以不能百分之百确定它是否可以工作,但我希望我选择的方法通过上面的代码变得清晰……

票数 3
EN

Stack Overflow用户

发布于 2015-03-29 11:18:39

常见问题解答文章http://freemarker.org/docs/app_faq.html#faq_modify_seq_and_map概述了为什么这是棘手的。基本上,它需要修改您的restful接口来为您完成这项工作:

代码语言:javascript
复制
<#assign authors = rest("blogs/id/audiofile/uniquauthors")>

将是一个新的restful接口的示例,然后在服务器端完成工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15925379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档