Thymeleaf
Ktor allows you to use Thymeleaf templates as views within your application by installing the Thymeleaf plugin (previously known as feature).
Add dependencies
To enable Thymeleaf
support, you need to include the ktor-thymeleaf
artifact in the build script:
Install Thymeleaf
To install the Thymeleaf
plugin, pass it to the install
function in the application initialization code. Depending on the way used to create a server, this can be the embeddedServer
function call ...
... or a specified module.
Configure Thymeleaf
Configure template loading
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:
Send a template in response
Imagine you have the index.html
template in resources/templates
:
A data model for a user looks as follows:
To use the template for the specified route, pass ThymeleafContent
to the call.respond
method in the following way: