Skip to content

Latest commit

 

History

History
52 lines (38 loc) · 2.13 KB

mustache.md

File metadata and controls

52 lines (38 loc) · 2.13 KB

Required dependencies: io.ktor:%artifact_name%

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

Add dependencies {id="add_dependencies"}

Install Mustache {id="install_plugin"}

Inside the install block, you can configure the MustacheFactory for loading Mustache templates.

Configure Mustache {id="configure"}

Configure template loading {id="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:

{src="snippets/mustache/src/main/kotlin/com/example/Application.kt" lines="12-14"}

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

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