List the template in the elevator

How can I list things in a template in an elevator?

Say, for example, that I have a [User] List , and I want to display it in a table. In Django, I would use the "users" context variable and scroll it in the template as follows:

 //controller
 user = User.objects.all() 
 context = {'users' : users}
 return render_to_template('results.html', context}

 //view
 <table>
 {% for user in users %}
 <tr><td>{{user.name}}</td>
     <td>{{user.email}}</td>
 </tr>
 {% endfor %}
 </table>

I appreciate any help.

PS: Could you also show me an example of the scala side, since I don’t know how to approach this problem.

+3
source share
2 answers

Template

<ul>
  <lift:UserSnippet.showAll> 
    <li><foo:userName />: <foo:age /></li> 
  </lift:UserSnippet.showAll> 
</ul>

Fragment Class

I guess there usersis List[User].

import scala.xml.NodeSeq
import net.liftweb.util.Helpers

class UserSnippet { 
  def showAll(in: NodeSeq): NodeSeq = {
    users.flatMap { user => Helpers.bind("foo", in, "userName" -> user.name, "age" -> user.age) }
  }
} 

See wiki articles on design templates and snippets for more information.

+5
source

java, ArrayList java-... ....

java java, .

( , "", java , , )

//SCALA Code

import scala.collection.JavaConversions._
import my.java.package.something._
import scala.xml.NodeSeq
import net.liftweb.util.Helpers

class mySnippet {

    //You want to run the ".toList" on your java list, this will convert it into a scala list
    val myScalaList = my.java.package.something.buildMyList().toList

    //This is the function that will bind the list to the html view
    def displayPeople(html : NodeSeq) : NodeSeq = {
      myScalaList.flatMap{person => bind("info", html, 
          "name", person.name,
          "age", person.age,
          "sex", person.sex)}
    }
}



//HTML code

    <table>
        <tr>
            <td>Name</td>
            <td>Age</td>
            <td>Sex</td>
        </tr>

       <lift:mySnippet.displayPeople>
            <tr>
                <td><info:name></info:name></td>
                <td><info:age></info:age></td>
                <td><info:sex></info:sex></td>
            </tr>
       </lift:mySnippet.displayPeople>
    </table>

, :)

+2

All Articles