我如何在Play Framework模板中对列表进行排序? [英] How can I sort a list in Play Framework templates?

查看:127
本文介绍了我如何在Play Framework模板中对列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模板呈现JSON内容:

  [
#{list data}
{
title:$ {_。title},
id:$ {_。id}
}
#{if!_isLast},#{/ if} #{/ list}
]

有没有办法对 data 在打印数据成员之前在模板中?解析方案

>

  // src 
%{
exampleList = [z,y,a, b]
}%

< ul>
#{list items:exampleList.sort(),as:'product'}
< li> $ {product}< / li>
#{/ list}
< / ul>
//呈现
< ul>
< li> a< / li>
< li> b< / li>
< li> y< / li>
< li> z< / li>
< / ul>

另外你可以给sort()一个lambda表达式如何排序,一些例子在这里:< a href =http://groovy.codehaus.org/JN1015-Collections =nofollow> http://groovy.codehaus.org/JN1015-Collections



但最好不要使用模板引擎来呈现JSON。您可以使用控制器中的Jackson http://wiki.fasterxml.com/JacksonInFiveMinutes 或使用来自控制器类: http://www.playframework.org/ documentation / api / 1.2.5 / play / mvc / Controller.html 。帕拉科已经给你提示要在控制器中排序。


I have this template rendering JSON content:

[
#{list data}
{
    "title": ${_.title},
     "id": ${_.id}
} 
#{if !_isLast},#{/if}#{/list}
]

Is there a way to sort data inside the template before printing the data members?

解决方案

It is possible:

//src
%{
    exampleList = ["z", "y", "a", "b"]
}%

<ul>
#{list items:exampleList.sort(), as:'product'}
    <li>${product}</li>
#{/list}
</ul>
//rendered
<ul>
    <li>a</li>
    <li>b</li>
    <li>y</li>
    <li>z</li>
</ul>

In addition you can give sort() a lambda expression how to sort, some examples are here: http://groovy.codehaus.org/JN1015-Collections

But it is best not to use the template engine at all to render JSON. You can use Jackson from your controller http://wiki.fasterxml.com/JacksonInFiveMinutes or use renderJson from a controller class: http://www.playframework.org/documentation/api/1.2.5/play/mvc/Controller.html . Palako gave you already the hint to sort in the controller.

这篇关于我如何在Play Framework模板中对列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆