Using Velocity Templates

预计阅读时间: 1 分钟

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

本特性在构件 io.ktor:ktor-velocity:$ktor_version 中的 io.ktor.velocity.Velocity 类中定义
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))
}