liftweb 有迭代标签吗?
我想知道,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简而言之:不可以。Lift 的设计严格地将逻辑与设计分开,因此禁止在模板标记中使用通用标签。
请查看查看第一篇文章,了解 lift 如何处理迭代。
文章中的一个示例:您的标记:
您的代码:
现在,当 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:
Your code:
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 theusers
method.The
users
method does the iteration over allUsers
and for each user, itbind
s the values of first and second name to the innerxhtml
. All those iterations are then concatenated (throughflatMap
) 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.