在模板中处理条件内容的首选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>
我没有在代码段中看到一种明显的方式(通过绑定或CSS变换器)根据适当的“收藏”状态有条件地保留一个表单与另一个表单。
来自Java / SpringMVC / JSP背景,这将通过一个简单的<c:choose>
语句来解决,但是我花了很多时间试图解决这个问题,我只能假设我要去关于这完全倒退...
提前致谢,提升大师!
答案 0 :(得分:6)
我不是自称是电梯大师,但这里有两个对我来说合理的选择:
有一个代码段,一个la DoOrUndoFavorite
,在该代码段中,您将检查用户的收藏状态并呈现一个或另一个(if(favorited){...} else{...}
)表单。
或强>
保持您的代码段不变,并在每个代码段的呈现代码中,如果该代码段不应呈现,则返回Nil
作为绑定的NodeSeq
。
答案 1 :(得分:1)
我认为上面提到的电梯邮件列表中的这篇文章演示了条件html:
请务必点击“显示引用文字”以查看相关回复,以下是引用,以防万一:
>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*