liftweb 有迭代标签吗?

发布于 2024-10-04 18:54:49 字数 367 浏览 8 评论 0原文

我想知道,Lift 有迭代标签(例如:for、foreach)吗? 当我使用 JSP 时,我可以轻松地迭代一个 List,只需使用 JSP,将对象传递给标签即可。类似于 this像这样。 我知道这不是最好的例子,但你确实明白我的意图。 总而言之,Lift 是否存在这种情况?如果不存在,我该如何做到这一点?

我想做的最后一件事是对 html 进行硬编码。

I was wondering, does Lift have an iteration tag (ex: for, foreach)?
When I was working with JSP, i could easily iterate a List, just with JSP, passing the object to the tag. Something like this, or like this.
I know it's not really the best example, but you do understand my intentions.
To sum up, does this exist with Lift, or if not, how would I manage to do such thing?

The last thing i want to do is hardcode html.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

拔了角的鹿 2024-10-11 18:54:49

简而言之:不可以。Lift 的设计严​​格地将逻辑与设计分开,因此禁止在模板标记中使用通用标签。

请查看查看第一篇文章,了解 lift 如何处理迭代。

文章中的一个示例:您的标记:

<table>
  <lift:show.users>
    <tr>
      <td><f:first_name>David</f:first_name></td>
      <td><f:last_name>Pollak</f:last_name></td>
    </tr>
  </lift:show.users>
</table>

您的代码:

class Show {
  def users(xhtml: NodeSeq): NodeSeq = 
    Users.findAll.flatMap(user => 
      bind("f", xhtml, 
        "first_name" -> user.firstName, 
        "last_name" -> user.nameName
      )
    )
}

现在,当 lift 看到标签 时,它会调用相应的方法,并以标签的内容作为参数。然后,该标记将被 users 方法的返回值替换。

users 方法对所有 Users 进行迭代,对于每个用户,它将第一个和第二个名称的值绑定到内部>xhtml。然后将所有这些迭代连接起来(通过 flatMap)并返回。

当我开始使用电梯时,我总是发现这种方法有点过于僵化;到处都有一个小环,那怎么可能受伤呢?但现在我知道从模板代码中调用创建您自己的代码片段是多么容易,我无法想象再使用像 jsp 这样的东西了。它在比较中较弱并且使您的标记变得混乱。当然,您会失去 Scala 的大部分验证能力。

注意:

模板标签中填充内容的原因是出于设计目的。在这种情况下,虚拟标签和值将在绑定期间被替换。因此,模板设计者可以用或多或少有意义的内容填充标签,这使得编码人员可以更好地理解设计者想到的语义。

In short: No. Lift’s design strictly separates logic from design and as such forbids the use of generalised tags in the template markup.

Have a look at the view first article in order to see how lift can handle iterations.

An example from the article: Your markup:

<table>
  <lift:show.users>
    <tr>
      <td><f:first_name>David</f:first_name></td>
      <td><f:last_name>Pollak</f:last_name></td>
    </tr>
  </lift:show.users>
</table>

Your code:

class Show {
  def users(xhtml: NodeSeq): NodeSeq = 
    Users.findAll.flatMap(user => 
      bind("f", xhtml, 
        "first_name" -> user.firstName, 
        "last_name" -> user.nameName
      )
    )
}

Now, what lift does when it sees the tag <lift:show.users> is calling the corresponding method with the tag’s content as the argument. The tag will then be replaced by the return value of the users method.

The users method does the iteration over all Users and for each user, it binds the values of first and second name to the inner xhtml. All those iterations are then concatenated (through flatMap) and returned.

When I started with lift, I’d always found this approach a little too rigid; having a tiny loop here and there, how could that possibly hurt? But now that I know how easy it is to call and create your own snippets from the template code, I cannot imagine using anything like jsp anymore. It is weak in comparison and clutters your markup. And of course, you lose much of Scala’s power of validation.

Notes:

The reason the template tags are filled with content is for designing purposes. In this case the dummy tags and values are going to be replaced during the bind. The template designers can thus fill the tags with more or less meaningful content which allows the coder to better understand the semantics the designer had in mind.

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