如何在 Lift 中不迭代整个页面

发布于 2024-11-07 23:00:24 字数 1536 浏览 3 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爺獨霸怡葒院 2024-11-14 23:00:24

我不能说它是否是最佳甚至有效的解决方案,但您可以尝试使用 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.

单身狗的梦 2024-11-14 23:00:24

我认为最简单的方法是对总计使用单独的片段方法,并使用其自己的绑定定义将其与迭代绑定分开。但我对此有点陌生,所以可能有更简单的方法。

您可能还想研究新的设计师友好风格来定义您的片段。我不认为这会让事情变得更容易,但总体来说可能更可取。 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

勿忘初心 2024-11-14 23:00:24

我只是将用户计数变量读入不同的函数,然后从那里输出......

//Scala Code

private val users = List("kevin", "drozzy", "kyle")

def userList(html : NodeSeq) : NodeSeq = {
users.flatMap(user => bind("user", html, "name" -> user))
}

def userCount = "#count *" #> {users.length}

//HTML Code

<b>Total users: 
    <lift:SearchUsers.userCount>
        <div id="count"/>
    </lift:SearchUsers.userCount>
</b>

<table>
    <lift:SearchUsers.userList>
    <tr>
        <td><user:name/></td>
    </tr>
    </lift:SearchUsers.userList>
</table>

希望这有意义吗?

I would just read the user count variable into a different function, and output from there....

//Scala Code

private val users = List("kevin", "drozzy", "kyle")

def userList(html : NodeSeq) : NodeSeq = {
users.flatMap(user => bind("user", html, "name" -> user))
}

def userCount = "#count *" #> {users.length}

//HTML Code

<b>Total users: 
    <lift:SearchUsers.userCount>
        <div id="count"/>
    </lift:SearchUsers.userCount>
</b>

<table>
    <lift:SearchUsers.userList>
    <tr>
        <td><user:name/></td>
    </tr>
    </lift:SearchUsers.userList>
</table>

Hope that makes sense?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文