Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 2.78 KB

velocity.md

File metadata and controls

71 lines (53 loc) · 2.78 KB

Required dependencies: io.ktor:%artifact_name%

Ktor allows you to use Velocity templates as views within your application by installing the Velocity plugin.

Add dependencies {id="add_dependencies"}

Install Velocity {id="install_plugin"}

Optionally, you can install the VelocityTools plugin to have the capability to add standard and custom Velocity tools.

Configure Velocity {id="configure"}

Configure template loading {id="template_loading"}

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

Send a template in response {id="use_template"}

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

Add Velocity tools {id="velocity_tools"}

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
}