Thymeleaf

    To enable support, you need to include the ktor-thymeleaf artifact in the build script:

    Gradle (Groovy)

    Maven

    Thymeleaf - 图2

    To install the Thymeleaf feature, pass it to the install function in the application initialization code. This can be the main function …

    1. import io.ktor.features.*
    2. fun Application.main() {
    3. install(Thymeleaf)
    4. }

    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:

    1. import io.ktor.thymeleaf.*
    2. prefix = "templates/"
    3. suffix = ".html"
    4. characterEncoding = "utf-8"
    5. })
    6. }

    Send a Template in Response

    Imagine you have the index.html template in resources/templates:

    A data model for a user looks as follows: