Velocity
To enable support, you need to include the ktor-velocity
artifact in the build script:
Gradle (Groovy)
Maven
To install the Velocity
feature, pass it to the install
function in the application initialization code. This can be the main
function …
import io.ktor.features.*
fun Application.main() {
// ...
}
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
:
import io.ktor.velocity.*
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
install(Velocity) {
setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath")
setProperty("classpath.resource.loader.class", ClasspathResourceLoader::class.java.name)
}
Send a Template in Response
Imagine you have the index.vl
template in resources/templates
:
A data model for a user looks as follows:
data class User(val id: Int, val name: String)