List to Template

168 views
Skip to first unread message

Eduardo Santos

unread,
Mar 25, 2012, 11:25:24 PM3/25/12
to play-fr...@googlegroups.com
I have the following method:

 public static void obterRegioes() {
        Regiao regiaoDAO = new Regiao();
        List<Regiao> lstRegioes = regiaoDAO.findAll();

 }


I want to send the variable lstRegioes its value to the template and there make a FOR to build a menu.

How can i do it?

Thanks,
Eduardo

christian sarnataro

unread,
Mar 26, 2012, 4:34:18 AM3/26/12
to play-fr...@googlegroups.com
Supposing you're using play 1.2.x, just call render(lstRegioes)

Then you must have a file called "obterRegioes.html" which will receive your list. Or you can call render("otherTemplate", lstRegioes), if you want to use another template.

Eduardo Santos

unread,
Mar 26, 2012, 7:08:07 AM3/26/12
to play-fr...@googlegroups.com
I'm using play 2.0.

I want to send it to the index.html i'm already send to the template a string and Form[SomeTypeHere].

Any tips?

Thanks,
Eduardo

Leonard Punt

unread,
Mar 26, 2012, 7:25:44 AM3/26/12
to play-fr...@googlegroups.com
http://www.playframework.org/documentation/2.0/JavaTemplates

Op maandag 26 maart 2012 13:08:07 UTC+2 schreef Eduardo Santos het volgende:

Eduardo Santos

unread,
Mar 26, 2012, 10:18:17 AM3/26/12
to play-fr...@googlegroups.com
I already saw it and i didnt find anything that could help me :(

christian sarnataro

unread,
Mar 26, 2012, 12:43:54 PM3/26/12
to play-fr...@googlegroups.com
See then: 

I can see this snippet:

public static Result show(Long id) {
  Client client = Client.findById(id);
  return ok(views.html.Client.show(client));
}
I think that you can somehow use your variable lstRegioes instead of client.

Hope it helps

Eduardo Santos

unread,
Mar 26, 2012, 1:44:02 PM3/26/12
to play-fr...@googlegroups.com
With this example, i can use my list lstRegioes only if i create another template and make an include in the main template.

I dont think for every list/variable that i need to use in some template i have to create a separeted route/method/template and make an include to the main one to use the list/variable. If that is the way it should be done, i dont thing that is a good approach.

I think it should work like struts if i remeber well, u just need to use request().someMethod() to pass any kind of data (list, variable, maps, etc) to the template.

Thanks,
Eduardo

Leonard Punt

unread,
Mar 26, 2012, 1:51:33 PM3/26/12
to play-fr...@googlegroups.com
What do you exactly want? Pass a List to your template, or pass a List among some other variables/Lists to your template?

Leonard

Op maandag 26 maart 2012 19:44:02 UTC+2 schreef Eduardo Santos het volgende:

Eduardo Santos

unread,
Mar 26, 2012, 2:33:19 PM3/26/12
to play-fr...@googlegroups.com
At first i want to pass a list to my template and in the future if need some other list/variables.

I'm already rendering my template calling index.render(String, Form). So i'm sending an string and a form to my template. And now i need to send a list with some option to build a dinamic menu, thas why i want to send this list now.

Eduardo

Leonard Punt

unread,
Mar 26, 2012, 2:39:46 PM3/26/12
to play-fr...@googlegroups.com
Oké, this should do the trick:

Controller:
public static void obterRegioes() {
        Regiao regiaoDAO = new Regiao();
        List<Regiao> lstRegioes = regiaoDAO.findAll();
        render ok(views.html.templateName.render(lstRegioes));
}

View:
@(regioes: List[Regiao])

<ul> 
  @for(regiao <- regioes) {
    <li>@regiao.name</li>
  } 
</ul>



Op maandag 26 maart 2012 20:33:19 UTC+2 schreef Eduardo Santos het volgende:

Eduardo Santos

unread,
Mar 26, 2012, 3:14:00 PM3/26/12
to play-fr...@googlegroups.com
Thank you for your help, but it didnt work.

Now i gost this error:

render(java.lang.String,play.data.Form<models.ContatoFRM>) in views.html.index cannot be applied to (java.util.List<models.Regiao>)

The first render that error msg says is the one im using in the method index() to render the template.

Eduardo

Leonard Punt

unread,
Mar 26, 2012, 3:18:26 PM3/26/12
to play-fr...@googlegroups.com
The variables that you're providing in your controller method are not the same as the ones you declared in your template.

Please post your Controller method and the first line of your template.

Leonard

Op maandag 26 maart 2012 21:14:00 UTC+2 schreef Eduardo Santos het volgende:

Eduardo Santos

unread,
Mar 26, 2012, 3:53:44 PM3/26/12
to play-fr...@googlegroups.com
Controller:

public class Application extends Controller {
   
    private static String strAno;

    /**
     * Renderiza a pagina principal.
     * @return Result Action
     */
    public static Result index() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date ano = new Date();
        setStrAno(sdf.format(ano));
        return ok(index.render(strAno, Contato.contatoFrm));
    }

    /**
     * Pega as regiões cadastradas
     */

    public static void obterRegioes() {
        Regiao regiaoDAO = new Regiao();
        List<Regiao> lstRegioes = regiaoDAO.findAll();
        //render(lstRegioes);
    }

    public static String getStrAno() {
        return strAno;
    }

    public static void setStrAno(String strAno) {
        Application.strAno = strAno;
    }
}


Template:

@(strAno: String, contatoFrm: Form[ContatoFRM])

@main(".: Terapias RJ :.") {
//layout
}

If i add the line @(regioes: List[Regiao]) its gives me an error too.

Thanks for your time!
Eduardo

Eduardo Santos

unread,
Mar 27, 2012, 2:51:49 PM3/27/12
to play-fr...@googlegroups.com
Somebody can help me?

Eduardo

Robert Shin

unread,
May 4, 2012, 6:15:05 PM5/4/12
to play-fr...@googlegroups.com
Did you ever solve this?  I'm facing the same issue....

Eduardo Santos

unread,
May 5, 2012, 7:09:06 AM5/5/12
to play-fr...@googlegroups.com
Yes, u can pass any kind of variables u want to the template. Just send it to the send method ok the template and at the template u declare the variables.

Eduardo 

biesior

unread,
May 5, 2012, 9:13:49 AM5/5/12
to play-fr...@googlegroups.com
As each view is just a scala function you need to declare its parameters in first line AND pass the parameters of the same type from controller to view while view rendering.

Documentation describes default values for views parameters BUT they doesn't when rendering from Java controller (what a pity), instead you can pass ie. null in the place of the required parameter and check in template if the desired value isn't null:

@if(regioes != null){
   @for(regio <- regioes) {@regio.name <br/>}
}
 
Reply all
Reply to author
Forward
0 new messages