How to display a list of templates?

I want to show all users present in the database. I want to put all users in a list, and then display this list in a template.

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

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

I want to know how to get users from a 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 to where to go from here?

+5
source share
1 answer

This is the main syntax that is often displayed in docs and samples (check i.e. computer-databasesample

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...
}
+6
source

All Articles