2011-08-28 41 views
4

在模板中处理条件内容的首选Lift方法是什么?在模板中处理条件内容的首选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变压器)的片段一个明显的方式有条件地保持一种形式VS基础上,适当的“收藏”状态等。

从Java /用SpringMVC/JSP背景的,这将用一个简单的<c:choose>声明来解决,但正如我花了试图弄清楚这一点,尽可能多的时间,我只能假设我会对此完全向后...

在此先感谢,电梯大师!

+2

沿着什么迪伦说,但只是为了澄清。 Lift的模板不包含逻辑,所有逻辑都位于片段上。在附注中,对于这个功能,我会使用ajaxButton而不是常规的形式,你可以在[github]上看到定义(https://github.com/lift/framework/blob/master/web/webkit/的src /主/阶/净/ liftweb/HTTP/SHtml.scala)。最后,如果您将问题发布在[Lift邮件列表](http://groups.google.com/group/liftweb)上,那么您将获得更多答案。大多数电梯开发人员不会来到SO – fmpwizard

+0

@fmpwizard我认为我对自己的榜样不屑一顾,而不是真正的问题的核心。我真正在这种情况下是“提升”方式:1.将所有条件内容放入模板中,然后将其从代码片段中隐藏起来; 2.使用容器html元素并动态生成条件html片段。 #1“感觉”对我来说很合适,但我不想对着升降机潮流! (另外,感谢您的邮件列表!) –

回答

6

我不宣称自己是一个提升大师,但这里的两位似乎合理的,我选择:

有一个片段,一拉DoOrUndoFavorite,并在该段,您应该查看收藏状态的用户并渲染一个或另一个(if(favorited){...} else{...})窗体。

让您的片断,因为他们,并在每个片断的渲染代码,您绑定如果片断不应该渲染返回Nil作为NodeSeq

+0

谢谢!我会研究这些选项。 –

1

我认为,这个职位从上述升降邮件列表展示条件的html:

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

请务必点击“显示引用的文字”看到相关的答复,这里是一个报价,只是在情况下:

>On Oct 18, 10:05 pm, "Jason Anderson" < [email protected]> 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 < [email protected]> 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* 
相关问题