Required dependencies: io.ktor:%artifact_name%
Ktor allows you to use Velocity templates as views within your application by installing the Velocity plugin.
Optionally, you can install the VelocityTools
plugin to have the capability to add standard and custom Velocity tools.
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
:
{src="snippets/velocity/src/main/kotlin/com/example/Application.kt" lines="13-16"}
Imagine you have the index.vl
template in resources/templates
:
{src="snippets/velocity/src/main/resources/templates/index.vl"}
A data model for a user looks as follows:
{src="snippets/velocity/src/main/kotlin/com/example/Application.kt" lines="25"}
To use the template for the specified route, pass VelocityContent
to the call.respond
method in the following way:
{src="snippets/velocity/src/main/kotlin/com/example/Application.kt" lines="18-21"}
If you've installed the VelocityTools
plugin, you can access the EasyFactoryConfiguration
instance inside the install
block to add standard and custom Velocity tools, for example:
install(VelocityTools) {
engine {
// Engine configuration
setProperty("resource.loader", "string")
addProperty("resource.loader.string.name", "myRepo")
addProperty("resource.loader.string.class", StringResourceLoader::class.java.name)
addProperty("resource.loader.string.repository.name", "myRepo")
}
addDefaultTools() // Add a default tool
tool("foo", MyCustomTool::class.java) // Add a custom tool
}