Using Velocity Templates

预计阅读时间:1分钟

Ktor通过Velocity功能包括对Velocity模板的支持. 初始化用的测速功能VelocityEngine

此功能定义在类io.ktor.velocity.Velocity在神器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

您可以安装Velocity,并配置VelocityEngine .

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

Usage

配置了Velocity后,可以使用VelocityContent实例调用call.respond方法:

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))
}

by  ICOPY.SITE