如何在 Lift 中不迭代整个页面
如何在 Lift 中绑定一些变量,以便我可以在模板的“迭代”部分之外使用它们。
这让我很困惑,所以我将通过一个例子来提出这个问题。
假设我有一些片段方法可以提供用户列表以及这些用户的总数:
def users(in: NodeSeq) :NodeSeq={
val (entries:List[User], total:Int) = // read entries and total from somewhere
// expensive operation
entries.flatMap(user => Helpers.bind("patient", in,
"userID" -> user.userID,
"userName" -> user.userName))
现在我想在模板中使用它:
...
<lift:Search.users>
<tr>
<td><user:userID/></td>
<td><user:userName/></td>
</tr>
</lift:Search.users>
...
但是,我还想输出总数计数用户数 - 但在页面的其他地方:
<b>Total users: <user:total></b> <--- ?????
...
<lift:Search.users>
<tr>
<td><user:userID/></td>
<td><user:userName/></td>
</tr>
</lift:Search.users>
...
正如您可以想象的那样,这不起作用,因为它位于 Search 代码段标记之外。此外 - 如果我尝试将总数绑定到用户 - 它会解除我之前对用户的所有绑定!
我尝试将提升标签放在整个文档周围,但这只会使整个页面重复出现!
<body>
<lift:Search.users> <---- clones the whole page!
<b>Total users: <user:total></b>
<table>
...
<tr>
<td><user:userID/></td>
<td><user:userName/></td>
</tr>
</table>
</lift:Search.users>
</body>
我是 Lift 的新手 - 所以我一定错过了一些非常明显的东西。
How can I bind some variables in Lift, so that I can use them outside the "iterating" part of the template.
This is very confusing to me, so I'll pose the question with the help of an example.
Let say I have some snippet method that can provide a list of users, and a total count of these users:
def users(in: NodeSeq) :NodeSeq={
val (entries:List[User], total:Int) = // read entries and total from somewhere
// expensive operation
entries.flatMap(user => Helpers.bind("patient", in,
"userID" -> user.userID,
"userName" -> user.userName))
now I want to use it within the template:
...
<lift:Search.users>
<tr>
<td><user:userID/></td>
<td><user:userName/></td>
</tr>
</lift:Search.users>
...
However, I also want to output the total count of users - but somewhere else on the page:
<b>Total users: <user:total></b> <--- ?????
...
<lift:Search.users>
<tr>
<td><user:userID/></td>
<td><user:userName/></td>
</tr>
</lift:Search.users>
...
as you can imagine this does not work, because it is outside the Search snippet tag. Furthermore - if I try to bind the total to users - it unbinds all my previous binds for the user!
I tried putting the lift tags around the whole document, but that just makes the whole page repeat itself!!
<body>
<lift:Search.users> <---- clones the whole page!
<b>Total users: <user:total></b>
<table>
...
<tr>
<td><user:userID/></td>
<td><user:userName/></td>
</tr>
</table>
</lift:Search.users>
</body>
I am new to Lift - so I must be missing something really obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不能说它是否是最佳甚至有效的解决方案,但您可以尝试使用 RequestVar: http://simply.liftweb.net/index-4.4.html#toc-Section-4.4 。
您可以计算用户列表和数量,并将数据保存到 RequestVar 中,该数据将在当前请求的另一个片段中可用。
I can't say whether it is optimal or even working solution but you can try using RequestVar: http://simply.liftweb.net/index-4.4.html#toc-Section-4.4 .
You can calculate list and count of users and save the data into RequestVar which will be available in another snippet in current request.
我认为最简单的方法是对总计使用单独的片段方法,并使用其自己的绑定定义将其与迭代绑定分开。但我对此有点陌生,所以可能有更简单的方法。
您可能还想研究新的设计师友好风格来定义您的片段。我不认为这会让事情变得更容易,但总体来说可能更可取。 Simply Lift 中概述了此方法:http://simply.liftweb。 net/index-3.4.html#toc-Section-3.4
I think the easiest thing to do would be to use a separate snippet method for the total, with its own bind definition to separate it from the iterating bind. But I'm a bit new to this, so there may be an easier way.
You may also want to look into the newer designer-friendly style for defining your snippets. I don't think it will make this much easier, but it is probably preferable overall. This approach is outlined in Simply Lift here: http://simply.liftweb.net/index-3.4.html#toc-Section-3.4
我只是将用户计数变量读入不同的函数,然后从那里输出......
希望这有意义吗?
I would just read the user count variable into a different function, and output from there....
Hope that makes sense?