Velocity

    To enable support, you need to include the ktor-velocity artifact in the build script:

    Gradle (Groovy)

    Maven

    Velocity - 图2

    To install the Velocity feature, pass it to the install function in the application initialization code. This can be the main function …

    1. import io.ktor.features.*
    2. fun Application.main() {
    3. // ...
    4. }

    Inside the install block, you can configure the VelocityEngine. For example, if you want to use templates from the classpath, use a resource loader for classpath:

    1. import io.ktor.velocity.*
    2. import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
    3. install(Velocity) {
    4. setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath")
    5. setProperty("classpath.resource.loader.class", ClasspathResourceLoader::class.java.name)
    6. }

    Send a Template in Response

    Imagine you have the index.vl template in resources/templates:

    A data model for a user looks as follows:

    1. data class User(val id: Int, val name: String)