将列表输出到Lift中的模板 [英] Output a list to a template in lift

查看:66
本文介绍了将列表输出到Lift中的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Lift中的模板中输出事物列表?

How can I output a list of things in a template in Lift?

例如,假设我有 List [User] ,我想将其输出为表格.在Django中,我将使用上下文变量用户",并在模板中对其进行遍历,如下所示:

Let say for example that I have List[User] and I want to output it as a table. In Django, I would use a context variable "users" and iterate through it in the template like so:

 //controller
 user = User.objects.all() 
 context = {'users' : users}
 return render_to_template('results.html', context}

 //view
 <table>
 {% for user in users %}
 <tr><td>{{user.name}}</td>
     <td>{{user.email}}</td>
 </tr>
 {% endfor %}
 </table>

感谢您的帮助.

PS:您能否也给我展示一个scala方面的例子-因为我对如何解决这个问题一无所知.

PS: Could you also show me an example of the scala side - as I am clueless about how to approach this problem.

推荐答案

模板

<ul>
  <lift:UserSnippet.showAll> 
    <li><foo:userName />: <foo:age /></li> 
  </lift:UserSnippet.showAll> 
</ul>

摘要类

我假设usersList[User].

import scala.xml.NodeSeq
import net.liftweb.util.Helpers

class UserSnippet { 
  def showAll(in: NodeSeq): NodeSeq = {
    users.flatMap { user => Helpers.bind("foo", in, "userName" -> user.name, "age" -> user.age) }
  }
} 

请参阅设计者友好模板片段以获取更多信息.

See the lift wiki articles on designer friendly templates and snippets for more information.

这篇关于将列表输出到Lift中的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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