Search code examples
scalalift

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!


Solution

  • 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.