Required dependencies: io.ktor:%artifact_name%
Ktor allows you to use Thymeleaf templates as views within your application by installing the Thymeleaf plugin.
Inside the install
block, you can configure the ClassLoaderTemplateResolver
. For example, the code snippet below enables Ktor to look up *.html
templates in the templates
package relative to the current classpath:
{src="snippets/thymeleaf/src/main/kotlin/com/example/Application.kt" lines="12-18"}
Imagine you have the index.html
template in resources/templates
:
{src="snippets/thymeleaf/src/main/resources/templates/index.html"}
A data model for a user looks as follows:
{src="snippets/thymeleaf/src/main/kotlin/com/example/Application.kt" lines="27"}
To use the template for the specified route, pass ThymeleafContent to the call.respond
method in the following way:
{src="snippets/thymeleaf/src/main/kotlin/com/example/Application.kt" lines="20-23"}