Mustache
Ktor allows you to use Mustache templates as views within your application by installing the Mustache plugin (previously known as feature).
Add dependencies
To enable Mustache
support, you need to include the ktor-mustache
artifact in the build script:
Install Mustache
To install the Mustache
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.
Inside the install
block, you can configure the MustacheFactory for loading Mustache templates.
Configure Mustache
Configure template loading
To load templates, you need to assign the MustacheFactory to the mustacheFactory
property. For example, the code snippet below enables Ktor to look up templates in the templates
package relative to the current classpath:
Send a template in response
Imagine you have the index.hbs
template in resources/templates
:
A data model for a user looks as follows:
To use the template for the specified route, pass MustacheContent
to the call.respond
method in the following way: