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>


You can install Velocity, and configure the VelocityEngine.

install(Velocity) { // this: VelocityEngine
    setProperty("resource.loader", "string");
    addProperty("string.resource.loader.repository.static", "false")
    init() // need to call `init` before trying to retrieve string repository
    (getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT) as StringResourceRepository).apply {
        putStringResource("test.vl", "<p>Hello, \$id</p><h1>\$title</h1>")


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

routing {
    val model = mapOf("id" to 1, "title" to "Hello, World!")

    get("/") {
        call.respond(VelocityContent("test.vl", model, "e"))