开发者

renderArgs is null in template in play! framework

开发者 https://www.devze.com 2023-04-11 16:47 出处:网络
I have a controller function like this public static void renderCustomersList() { System.out.println(\"renderArgs\"+renderArgs.toString());

I have a controller function like this

public static void renderCustomersList() {
        System.out.println("renderArgs"+renderArgs.toString());
        开发者_如何学Crender(renderArgs);
    }

In my groovy template

renderArgs ${renderArgs.toString()}

I have renderArgs with value printed in my console but when i pass to the template its printing as null. I wanted those values in my main.html. I printed renderArgs in Both my renderCustomersList.html and main.html the values were null on both templates. What could be the error.


You do not need to pass renderArgs as a parameter to the render function - all the variables in the renderArgs scope will be available in the template anyway. But instead of accessing the renderArgs object itself in the template, go for the independent values instead - see below.

Controller:

public static void renderCustomersList() {
    ... // some code to initialize yourVariable
    renderArgs.put("yourVariable", yourVariable);
    render(); // renderArgs not needed as a parameter
}

And in the template:

${yourVariable}


Note also that you don't have to user renderArgs if you don't want to. You can also pass some parameters to render() like this:

public static void renderCustomersList() {
    ...
    render(customers, products, foobar); 
}

and then you can get at those parameters in your template with the variable names:

#{list items:customers, as:cust}
...
Foobar: ${foobar}

The only case in which you need to use renderArgs is if you want to use different names for your parameters in the template than in the controller. This is potentially confusing though, so I try not to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消