FreeMarker
Ktor allows you to use FreeMarker templates as views within your application by installing the FreeMarker plugin.
Add dependencies
To use FreeMarker, add the ktor-server-freemarker artifact to your build script:
Install FreeMarker
To install the FreeMarker plugin to your application, pass it to the install function in the specified module. The following examples show how to install FreeMarker:
In an
embeddedServer()function call.In an explicitly defined
module()extension function on theApplicationclass.
Inside the install block, you can configure the desired TemplateLoader for loading FreeMarker templates.
Configure FreeMarker
Configure template loading
To load templates, you need to assign the desired TemplateLoader type to the templateLoader property. For example, the code snippet below enables Ktor to look up templates in the templates package relative to the current classpath:
Send a template in response
Imagine you have the index.ftl template in resources/templates:
A data model for a user looks as follows:
To use the template for the specified route, pass FreeMarkerContent to the call.respond method in the following way: