KtorはVelocityテンプレートをVelocity Featureを通じてサポートしています。 VelocityEngineとともにVelocity Featureを初期化します:
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>
Velocityをインストールし、VelocityEngine
を設定します:
install(Velocity) {
setProperty("resource.loader", "classpath")
setProperty("classpath.resource.loader.class", ClasspathResourceLoader::class.java.name)
}
Velocityが設定されていたら、call.respond
をVelocityContent
インスタンスとともに呼び出します:
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))
}