lift:testCond - 它是如何工作的?

发布于 2024-10-05 05:07:57 字数 347 浏览 4 评论 0原文


我在电梯示例中发现了这一点:

<lift:TestCond.loggedout>
  <lift:embed what="/templates/_login_panel"/>
</lift:TestCond.loggedout>
How do I tweak this lift tag if I want to test any other condition? Is this some kind of <c:if/> tag in JSP or the idea is somewhere else?

i discovered this in lift examples:

<lift:TestCond.loggedout>
  <lift:embed what="/templates/_login_panel"/>
</lift:TestCond.loggedout>


How do I tweak this lift tag if I want to test any other condition? Is this some kind of <c:if/> tag in JSP or the idea is somewhere else?

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

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

发布评论

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

评论(2

合约呢 2024-10-12 05:07:57

lift:TestCond 指的是代码片段 object TestCond 仅提供 loggedInloggedOut 方法。 Lift 中没有通用的 可用,因为它会模糊代码和标记之间的界限。

如果您想要不同的行为,您需要自己实现此类测试并在代码中明确它们。但它真的很简单。通过查看源代码,您可以了解如何根据您的需求进行自定义。

loggedIn 的代码非常简单

def loggedIn(xhtml: NodeSeq): NodeSeq =  
  if (S.loggedIn_?) xhtml else NodeSeq.Empty  

,例如,您可以实现不同的行为

<lift:HasRole.administrator />

,允许更高级的

<lift:HasRole.any type="administrator manager" />

或类似的操作。但这实际上取决于您的用例,所以我认为不可能在 Lift 中使其通用。

lift:TestCond refers to the snippet object TestCond which only provides the loggedIn and loggedOut methods. There is no general <c:if/> available in Lift because it would blur the boundaries between code and markup.

If you want different behaviour, you’ll need to implement such tests yourself and make them explicit in your code. But its really simple. By looking at the source code, you can get an idea of how to customise this to your needs.

The code for loggedIn is as simple as

def loggedIn(xhtml: NodeSeq): NodeSeq =  
  if (S.loggedIn_?) xhtml else NodeSeq.Empty  

So, for example, you could implement a different behaviour which allows for

<lift:HasRole.administrator />

or, more advanced

<lift:HasRole.any type="administrator manager" />

or something similar. But this really depends on your use case, so I think it’s not possible to make this generic in Lift.

把昨日还给我 2024-10-12 05:07:57

作为旁注,我编写了一个小实用程序来为我执行此任务:

object SnippetUtil {
  def testCond[T](value: Box[T], in: NodeSeq, f: T => Boolean): NodeSeq =
    value match {
      case Full(v) if f(v) => in
      case _ => NodeSeq.Empty
    }
}

然后您可以在 DispatchSnippet 中使用它,例如:

object SearchSnippet extends DispatchSnippet {
  def dispatch = {
    case "hasParameter" => testCond[String](S.param("s"), _, _.nonEmpty)
    // ...
  }
}

您可以决定是否要编写 testCond[Type]( ...)testCond(...)。在第二种情况下,您必须指定函数的类型。例如 testCond(S.param("s"), _, (_: String).nonEmpty)

As a side-note I have written a small utility which performs this task for me:

object SnippetUtil {
  def testCond[T](value: Box[T], in: NodeSeq, f: T => Boolean): NodeSeq =
    value match {
      case Full(v) if f(v) => in
      case _ => NodeSeq.Empty
    }
}

Then you can use it like the following in a DispatchSnippet for instance:

object SearchSnippet extends DispatchSnippet {
  def dispatch = {
    case "hasParameter" => testCond[String](S.param("s"), _, _.nonEmpty)
    // ...
  }
}

You can decide whether or not you want to write testCond[Type](...) or testCond(...). In the second case you will have to specify the type of the function. E.g. testCond(S.param("s"), _, (_: String).nonEmpty).

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