What is the preferred Lift way to handle conditional content in a template?
What is the preferred Lift way to handle conditional content in a template?
As a concrete example, let's imagine the common construct of an "Add to My Favorites" type button. If not in your favorites, there is a button to click to add it. If already in your favorites, there is a button to remove it. Something like this:
<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>
I don't see an obvious way in the snippet (via either binding or CSS transformers) to conditionally keep one form vs the other based on the appropriate "favorited" state.
Coming from a Java/SpringMVC/JSP background, this would be solved with a simple <c:choose>
statement, but with as much time as I've spent trying to figure this out, I can only assume I am going about this completely backwards…
Thanks in advance, Lift gurus!开发者_如何转开发
I don't claim to be a lift guru, but here's two options that seem reasonable to me:
Have one snippet, a la DoOrUndoFavorite
, and within that snippet you would check the favorited state for the user and render one or the other (if(favorited){...} else{...}
) form.
or
Keep your snippets as they are, and within each snippet's render code, return a Nil
as the NodeSeq
for your binding if that snippet should not render.
I think that this post from the above mentioned lift mailing list demonstrates conditional html :
https://groups.google.com/forum/?fromgroups#!searchin/liftweb/conditional$20view/liftweb/CQG-wTx_qkc/pbD6PURwbksJ
make sure to click "show quoted text" to see the relevant reply, here is a quote, just in case:
>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*
精彩评论