I wrap up all my "handlebars around" code in the module
play-handlebars.
You can use it as easy as
public class HomeController extends Controller {
@Inject
private HandlebarsApi handlebarsApi;
public Result index() {
// Data.
final Map data = new HashMap<>();
data.put("title", "Page Title");
data.put("header", "Header");
data.put("main", ImmutableMap.of("article", "Main Article"));
data.put("footer", "Footer");
// Fill it with the data.
final Content page = handlebarsApi.html("page", data, Context.current().lang().code());
// Return the page to the client.
return ok(page);
}
}
Or
class HomeController @Inject() (val handlebarsApi: HandlebarsApi)extends Controller with HandlebarsSupport{
def index = Action { implicit request =>{
val jsonData =
Json.obj("users" -> Json.arr(
Json.obj(
"name" -> "Jhon",
"age" -> 4,
"role" -> "Worker"
),
Json.obj(
"name" -> "Duck",
"age" -> 6,
"role" -> "Administrator"
)))
val page = render("page", jsonData)
Ok(page)
}}
}
No comments:
Post a Comment