JTE
Ktor allows you to use JTE templates as views within your application by installing the Jte plugin.
Add dependencies
To use Jte, you need to include the ktor-server-jte artifact in the build script:
Install Jte
To install the Jte plugin to the application, pass it to the install function in the specified module. The code snippets below show how to install Jte...
... inside the
embeddedServerfunction call.... inside the explicitly defined
module, which is an extension function of theApplicationclass.
Inside the install block, you can configure how to load JTE templates.
Configure Jte
Configure template loading
To load JTE templates, you need to:
Create a
CodeResolverused to resolve template code. For example, you can configureDirectoryCodeResolverto load templates from a given directory orResourceCodeResolverto load templates from application resources.Use the
templateEngineproperty to specify a template engine, which uses a createdCodeResolverto transfer templates into native Java/Kotlin code.
For instance, the code snippet below enables Ktor to look up JTE templates in the templates directory:
Send a template in response
Suppose you have the index.kte template in the templates directory:
To use the template for the specified route, pass JteContent to the call.respond method in the following way: