Required dependencies: io.ktor:%artifact_name%
Ktor allows you to use Mustache templates as views within your application by installing the Mustache plugin.
Inside the install
block, you can configure the MustacheFactory for loading Mustache templates.
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:
{src="snippets/mustache/src/main/kotlin/com/example/Application.kt" lines="12-14"}
Imagine you have the index.hbs
template in resources/templates
:
{src="snippets/mustache/src/main/resources/templates/index.hbs"}
A data model for a user looks as follows:
{src="snippets/mustache/src/main/kotlin/com/example/Application.kt" lines="23"}
To use the template for the specified route, pass MustacheContent
to the call.respond
method in the following way:
{src="snippets/mustache/src/main/kotlin/com/example/Application.kt" lines="16-19"}