Using Velocity Templates

Estimated reading time: 1 minute

Ktor includes support for Velocity templates through the Velocity feature. Initialize the Velocity feature with the VelocityEngine:

This feature is defined in the class io.ktor.velocity.Velocity in the artifact io.ktor:ktor-velocity:$ktor_version.
dependencies { implementation "io.ktor:ktor-velocity:$ktor_version" }
dependencies { implementation("io.ktor:ktor-velocity:$ktor_version") }
<project> ... <dependencies> <dependency> <groupId>io.ktor</groupId> <artifactId>ktor-velocity</artifactId> <version>${ktor.version}</version> <scope>compile</scope> </dependency> </dependencies> </project>

Installation

You can install Velocity, and configure the VelocityEngine.

install(Velocity) {
    setProperty("resource.loader", "classpath")
    setProperty("classpath.resource.loader.class", ClasspathResourceLoader::class.java.name)
    }
}

Usage

When Velocity is configured, you can call the call.respond method with a VelocityContent instance:

data class User(val name: String, val email: String)

get("/") {
	 val user = User("user name", "user@example.com")
    call.respond(VelocityContent("templates/hello.vl", user))
}