【问题标题】:What is the preferred Lift way to handle conditional content in a template?处理模板中的条件内容的首选 Lift 方式是什么?
【发布时间】:2013-03-16 01:40:50
【问题描述】:

在模板中处理条件内容的首选 Lift 方式是什么?

作为一个具体的例子,让我们想象一下“添加到我的收藏夹”类型按钮的常见构造。如果不在您的收藏夹中,则有一个按钮可以单击以添加它。如果已经在您的收藏夹中,则有一个按钮可以将其删除。像这样的:

<div class="lift:MySnippet">

  <!-- other stuff -->

  <div class="favorite">
    <form id="doFavorite" class="lift:MySnippet.favorite?form=post">
      <input type="submit" value="Add to Favorites" />
    </form>
    <form id="doUnfavorite" class="lift:MySnippet.unfavorite?form=post">
      <input type="submit" value="Remove from favorites" />
    </form>
  </div>

  <!-- other stuff -->

</div>

我没有在 sn-p 中看到明显的方式(通过绑定或 CSS 转换器)根据适当的“收藏”状态有条件地保持一种形式与另一种形式。

来自 Java/SpringMVC/JSP 背景,这可以通过一个简单的&lt;c:choose&gt; 语句来解决,但是我花了尽可能多的时间来解决这个问题,我只能假设我正在解决这个问题完全倒退……

提前感谢电梯大师!

【问题讨论】:

  • 按照 Dylan 所说的,但只是为了澄清。 Lift 的模板不包含任何逻辑,所有的逻辑都在 sn-ps 上。附带说明一下,对于此功能,我将使用 ajaxButton 而不是常规表单,您可以在 github 上查看定义。最后,如果您在Lift mailing list 上发布您的问题,您将获得更多答案@ 大多数 Lift 开发人员不会来 SO
  • @fmpwizard 我认为我对我的例子太过分了,而不是我真正问题的核心。在这种情况下,我真正追求的是“提升”方式 1. 将所有条件内容放在模板中,然后将其从 sn-p 或 2. 使用容器 html 元素并动态生成条件 html从sn-p。 #1 “感觉”对我来说是正确的,但我不想逆着 Lift 潮流! (另外,感谢您对邮件列表的提醒!)

标签: scala lift


【解决方案1】:

我并没有声称自己是电梯专家,但这里有两个对我来说似乎合理的选择:

有一个 sn-p,一个 la DoOrUndoFavorite,在该 sn-p 中,您将检查用户的收藏状态并呈现一个或另一个 (if(favorited){...} else{...}) 表单。

保持您的 sn-ps 不变,并且在每个 sn-p 的渲染代码中,如果该 sn-p 不应该渲染,则返回一个 Nil 作为您的绑定的 NodeSeq

【讨论】:

  • 谢谢!我会研究这些选项。
【解决方案2】:

我认为上述提升邮件列表中的这篇文章演示了条件 html:

https://groups.google.com/forum/?fromgroups#!searchin/liftweb/conditional$20view/liftweb/CQG-wTx_qkc/pbD6PURwbksJ

一定要点击“显示引用文字”才能看到相关回复,这里是引用,以防万一:

>On Oct 18, 10:05 pm, "Jason Anderson" < jla...@gmail.com> wrote:
> Ah, that makes sense
>
> perhaps the simple/*.html pages in the lift example should be
> changed to use this style of rendering rather than embedding the 
> templates in the snippet?
>
> it would also give you a chance to implement/test out that attribute
> binding for links you mentioned in another reply
>
> On 10/18/07, David Pollak < d...@athena.com> wrote:
>
>
>
> > Jason,
>
> > <lift:snippet type="...">
> > <cond:true>
> > <table> 
> > <tr><td><f:name/></td></tr>
> > </table>
> > </cond:true>
>
> > <cond:false>
> > Hey, there are no users... sorry
> > </cond:false>
> > </lift:snippet>
>
> > You can represent both cases (or even multiple cases) in the XHTML
> > and let the snippet decide which subsection of the XHTML to use. 
> > Given Scala's amazing XML handling capaibilities, it's a single line
> > of Scala code to select the code block:
>
> > val trueBlock = (xhtml \ "true").filter(_.prefix == "cond").headOr 
> > (<span>Template not defined</span>)
>
> > It does make the template look a little ugly, but no worse than an
> > ERB or JSP block looks.
>
> > Also, the <cond:.../> is a convention I've been using, but you can 
> > use any tags (e.g., <users:some> & <users:none>)
>
> > Thanks,
>
> > David*

【讨论】:

    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 2012-08-10
    • 2022-06-18
    • 2011-11-07
    • 2011-12-03
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多