Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a list to template?

I want to show all users present in a database. I want to place all users in a list and then render that list to a template.

Then I want to iterate over the list of users displaying each in a <p> tag

For u in users:
 <p>u.username</p>
Endfor 

I want to know how to retrieve the users from the database.

Public static Result render_f() {
  List<String> users = ask in db;
return ok(template.render(users)); 

Is the above approach reasonable? If not can I get some pointers on where to go from here?

like image 768
doniyor Avatar asked Jun 23 '12 09:06

doniyor


1 Answers

That's the basic syntax, often showed in docs and samples (check ie. computer-database sample

app/models/User.java

@Entity
public class User extends Model{

    @Id
    public Long id;
    public String name;

    public static Finder<Long,User> find = new Finder<Long,User>(Long.class, User.class);

}

app/controllers/Application.java

Public static Result render_f() {
    List<User> users = User.find.all();
    return ok(template.render(users));
}

template.scala.html

@(users: List[User])

@for(user <- users){
   <p>user.id</p>
   <p>user.name</p>
   etc...
}
like image 176
biesior Avatar answered Oct 15 '22 23:10

biesior