Locals
Locals are good for storing variables for the CURRENT request/response cycle. These variables are store in Response and will be available automatically to all templates for the current request/response cycle.
GET("/contact/{id}", routeContext -> {
/*
// variant 1 (with model)
Map<String, Object> model = new HashMap<>();
model.put("contacts", contactService.getContacts());
routeContext.render("contacts", model);
*/
// variant 2 (with locals)
routeContext.setLocal("contacts", contactService.getContacts());
routeContext.render("contacts");
});
where:
routeContext.setLocal()
is the shortcut forgetResponse().getLocals().put()
routeContext.render()
is the shortcut forgetResponse().render()
Another scenario for locals:
// filter that injects 'contacts' in locals and implicit in all templates
ANY("/contact.*", routeContext -> routeContext.setLocal("contacts", contactService.getContacts()));
// just consume 'contacts' in template
GET("/contacts", routeContext -> routeContext.render("crud/contacts"));
The snippet for contacts template (freemarker engine) shows a list with all contacts’ name:
<html>
<body>
<ul>
<#list contacts as contact>
<li>${contact.name}</li>
</#list>
</ul>
</body>
</html>