What is the preferred Lift method for handling conditional content in a template?

What is the preferred Lift method for handling conditional content in a template?

As a concrete example, let's introduce the general design of the Add to My List button. If you are not in your favorites, click the button to add it. If you are already in your favorites, there is a button to delete it. Something like that:

<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 fragment (via CSS bindings or transformers) to conditionally save one form against another based on the corresponding โ€œfavorableโ€ state.

Based on the Java / SpringMVC / JSP background, this would be solved with a simple <c:choose> statement, but with as much time as I spent trying to figure it out, I can only assume that I am going to do this all the way back. ..

Thanks in advance to guru lifting!

+4
source share
2 answers

I do not pretend to be the elevator guru, but here are two options that seem reasonable to me:

You have one fragment, a la DoOrUndoFavorite , and inside this fragment you must check the user's favorable condition and display one form or another ( if(favorited){...} else{...} ).

or

Keep your snippets as they are, and inside each snippet rendering code, return Nil as NodeSeq for your binding if that snippet should not be displayed.

+6
source

I think this post from the aforementioned elevator mailing list demonstrates conditional html:

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

don't forget to click "show quoted text" to see the corresponding answer, 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 amazing XML handling capaibilities, it 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 (eg, <users:some> & <users:none>) > > > Thanks, > > > David* 
+1
source

Source: https://habr.com/ru/post/1369391/


All Articles