Search code examples
scalalift

Lift - Binding list of values to template


Here is my template -

      <div class="lift:Admin.showInvitees">
          <tr class="success"> <!-- "success" "error" -->
            <td><invitee:name></invitee:name></td>
            <td><invitee:description></invitee:description></td>
            <td><invitee:status></invitee:status></td>
            <td></td>
          </tr>
      </div>

and here is the snippet -

class Admin {  
  def showInvitees(in: NodeSeq): NodeSeq = {
    val invitees = Invitation.allInvitations
    invitees.flatMap{invitee => bind("invitee", in, "name" -> invitee.name.is, "status" -> invitee.status.is, "description" -> invitee.description.is)}
  }
} 

I checked this question and used the solution. But it's not working for me. Values are not getting bound at all. This is what I get as output -

<tr class="success"> 
   <td><invitee:name></invitee:name></td>
   <td><invitee:description></invitee:description></td>
   <td><invitee:status></invitee:status></td>
</tr>

Need help binding a list of values to template.

After the first answer I tried this as well -

def showInvitees(in: NodeSeq): NodeSeq = {
val invitees = Invitation.allInvitations
(".invitees" #> invitees.map{ inv =>
    ".invitee-name" #> inv.name.is &
    ".invitee-description" #> inv.description.is &
    ".invitee-status" #> inv.status.is
})(in)

}

Even this is not working- Template -

<div class="lift:Admin.showInvitees">
      <tr class="invitees success"> <!-- "success" "error" -->
        <td><span class="invitee-name"></span></td>
        <td><span class="invitee-description"></span></td>
        <td><span class="invitee-status"></span> </td>
        <td></td>
      </tr>
  </div>

Solution

  • This is the old syntax for binding values. Try the new one:

    // snippet
    class Admin {  
      def showInvitees = {
        case class Invitee(name: String, descr: String, status: String)
        val invitees = List(Invitee("Alex", "for cookies", "ok"), Invitee("Bob", "for beer", "kicked out"))
        ".invitee" #> invitees.map { i =>
          ".invitee-name" #> i.name.is &
          ".invitee-descr" #> i.description.is &
          ".invitee-status" #> i.status.is
        }
      }
    } 
    
    // html
    <div class="Admin.showInvitees">
      <table>
        <tr class="invitee success"> 
          <td><span class="invitee-name"></span></td>
          <td><span class="invitee-descr"></span></td>
          <td><span class="invitee-status"></span></td>
        </tr> 
      </table>
    </div>
    
    // result
      <table>
        <tbody><tr class="invitee success">
          <td>Alex</td>
          <td>for cookies</td>
          <td>ok</td>
        </tr><tr class="invitee success">
          <td>Bob</td>
          <td>for beer</td>
          <td>kicked out</td>
        </tr>
      </tbody></table>