HTML DSL

HTML DSL integrates the kotlinx.html library into Ktor and allows you to respond to a client with HTML blocks. With HTML DSL, you can write pure HTML in Kotlin, interpolate variables into views, and even build complex HTML layouts using templates.

Add Dependencies

HTML DSL doesn’t need installation but requires the ktor-html-builder artifact. You can include it in the build script as follows:

HTML DSL - 图1

Gradle (Groovy)

Gradle (Kotlin)

Maven

HTML DSL - 图2

  1. implementation "io.ktor:ktor-html-builder:$ktor_version"

Send HTML in Response

To send an HTML response, call the ApplicationCall.respondHtml method inside the required route:

  1. get("/") {
  2. val name = "Ktor"
  3. call.respondHtml {
  4. head {
  5. title {
  6. +name
  7. }
  8. }
  9. body {
  10. h1 {
  11. +"Hello from $name!"
  12. }
  13. }
  14. }
  15. }

In this case, the following HTML will be sent to the client:

  1. <head>
  2. <title>Ktor</title>
  3. </head>
  4. <body>
  5. <h1>Hello from Ktor!</h1>
  6. </body>

To learn more about generating HTML using kotlinx.html, see the kotlinx.html wiki.

Templates

In addition to generating plain HTML, Ktor provides a template engine that can be used to build complex layouts. You can create a hierarchy of templates for different parts of an HTML page, for example, a root template for the entire page, child templates for a page header and footer, and so on. Ktor exposes the following API for working with templates:

  1. To respond with an HTML built based on a specified template, call the ApplicationCall.respondHtmlTemplate method.

  2. To create a template, you need to implement the Template interface and override the Template.apply method providing HTML.

  3. Inside a created template class, you can define placeholders for different content types:

    • Placeholder is used to insert the content. PlaceholderList can be used to insert the content that appears multiple times (for example, list items).

    • TemplatePlaceholder can be used to insert child templates and create nested layouts.

Example

Let’s see the example of how to create a hierarchical layout using templates. Imagine we have the following HTML:

  1. <body>
  2. <h1>Ktor</h1>
  3. <article>
  4. <h2>Hello from Ktor!</h2>
  5. <p>Kotlin Framework for creating connected systems.</p>
  6. </article>
  7. </body>

We can split the layout of this page into two parts:

  • A root layout template for a page header and a child template for an article.

  • A child template for the article content.

Let’s implement these layouts step-by-step:

  1. Call the respondHtmlTemplate method and pass a template class as a parameter. In our case, this is the LayoutTemplate class that should implement the Template interface:
  1. get("/") {
  2. call.respondHtmlTemplate(LayoutTemplate()) {
  3. // ...
  4. }
  5. }

Inside the block, we will be able to access a template and specify its property values. These values will substitute placeholders specified in a template class. We’ll create LayoutTemplate and define its properties in the next step.

  1. A root layout template will look in the following way:
  1. class LayoutTemplate: Template<HTML> {
  2. val header = Placeholder<FlowContent>()
  3. val content = TemplatePlaceholder<ContentTemplate>()
  4. override fun HTML.apply() {
  5. body {
  6. h1 {
  7. insert(header)
  8. }
  9. insert(ContentTemplate(), content)
  10. }
  11. }
  12. }

The class exposes two properties:

  • The header property specifies a content inserted within the h1 tag.

  • The content property specifies a child template for article content.

  1. A child template will look as follows:
  1. class ContentTemplate: Template<FlowContent> {
  2. val articleTitle = Placeholder<FlowContent>()
  3. val articleText = Placeholder<FlowContent>()
  4. override fun FlowContent.apply() {
  5. article {
  6. h2 {
  7. insert(articleTitle)
  8. }
  9. p {
  10. insert(articleText)
  11. }
  12. }
  13. }
  14. }

This template exposes the articleTitle and articleText properties, whose values will be inserted inside the article.

  1. Now we are ready to send HTML built using the specified property values:
  1. get("/") {
  2. call.respondHtmlTemplate(LayoutTemplate()) {
  3. header {
  4. +"Ktor"
  5. }
  6. content {
  7. articleTitle {
  8. +"Hello from Ktor!"
  9. }
  10. articleText {
  11. +"Kotlin Framework for creating connected systems."
  12. }
  13. }
  14. }
  15. }