Render template

The web module also integrates the template module with the Freemarker rendering engine by default. This allows controllers to return html content easily.

Although you can manually use the template engine, in most cases this would not be handy. However, you can use the TemplateResult class which will make it easier to use the renderer.


Built-in methods & variables

By default, the module voidframework-template-freemarker provide a set of methods and variables that will be accessible in templates.

MethodDescription
configRetrieves a value from the configuration
displaySizeDisplays a formatted number with the correct unit (Kio, Mio, Gio, …)
i18n / _Translates a message
urlforRetrieves an URL from the router (reverse router)
VariableDescription
flashContains temporary messages
sessionContains the current session data
isDevModeIndicates whether the application is in dev mode
langContains the current language
languagesContains all available languages
csrfTokenContains the CSRF token
voidFrameworkVersionContains the current Void Framework version

Templates location

Templates should be placed in the resources/views directory.


Static files

If you need to include static files in your HTML page (e.g. css, javascript, image, …), you can use the urlfor method with the first parameter static_file or static_webjar and the second parameter the path to the file.


Example

controller/HomeController.java

@Singleton
@WebController
public class HomeController {

    @RequestRouting
    public Result homepage(final Context ctx) {
        return Result.ok(
            TemplateResult.of("homepage.ftl", Map.of("greating", "Hello World")));
    }
}

resources/views/homepage.ftl

<!DOCTYPE html>
<html lang="${lang!}">
    <body>
      <h1>${greating}</h1>
      <img src="${urlfor('static_file', 'img/logo.png')}" alt="image"/>
    </body>
</html>