Using Freemarker Templates

Estimated reading time: 1 minute

Ktor includes support for FreeMarker templates through the FreeMarker feature. Initialize the FreeMarker feature with a TemplateLoader:

    install(FreeMarker) {
        templateLoader = ClassTemplateLoader(, "templates")

This TemplateLoader sets up FreeMarker to look for the template files on the classpath in the “templates” package, relative to the current class path. A basic template looks like this:

This feature is defined in the class io.ktor.freemarker.FreeMarker in the artifact io.ktor:ktor-freemarker:$ktor_version.
<h2>Hello ${}!</h2>

Your email address is ${}

With that template in resources/templates it is accessible elsewhere in the the application using the call.respond() method:

    get("/{...}") {
        val user = User("user name", "")
        call.respond(FreeMarkerContent("index.ftl", mapOf("user" to user), "e"))