Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: May 5, 2024
Developing web applications can be challenging, especially when handling dynamic content. However, Kotlin offers a solution with its HTML builder library, which is part of kotlinx.html. With this library, developers can easily create HTML documents using Kotlin’s expressive syntax.
In this tutorial, we’ll explore how kotlinx.html simplifies HTML generation in Kotlin.
The kotlinx.html library offers a Domain-Specific Language (DSL) for building HTML structures in Kotlin. Generally, this DSL allows us to create HTML elements and attributes in a type-safe and concise manner, resembling the structure of HTML itself.
Before diving into kotlinx.html, let’s set up the appropriate dependencies in our project. Whether we’re using Maven or Gradle as our build tool, integrating kotlinx.html is straightforward.
To include kotlinx.html in a Maven project, we’ll add it to our pom.xml file:
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-html-jvm</artifactId>
<version>0.7.2</version>
</dependency>
Similarly, for our Gradle-based project, we’ll add it to our build.gradle file:
dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-html-jvm:0.7.2"
}
Now, let’s explore kotlinx.html to understand how this library simplifies HTML generation in Kotlin. Specifically, we’ll create a simple HTML page with a header, a paragraph, and a list of items:
fun buildHTML(): String {
return createHTML().html {
head {
title { +"My Kotlin HTML Page" }
}
body {
h1 { +"Welcome to Kotlin HTML Builder" }
p { +"This is a demonstration of kotlinx.html." }
ul {
repeat(5) {
li { +"Item $it" }
}
}
}
}.toString().trim()
}
We start with the createHTML().html() function, which then initializes the HTML builder, marking the beginning of our document. Within the lambda expression, we delineate the sections of our HTML document.
While building our HTML document, notice that we use functions that match common HTML tags, such as body(), h1(), and p(). Accordingly, the kotlinx.html library provides functions for all standard HTML tags we might want to use.
Next, let’s use the kotlinx.html library to customize HTML attributes and add an id and styling to a p() tag:
fun buildParagraphWithAttributes(): String {
return createHTML().p {
id = "intro-paragraph"
classes = setOf("intro")
style = "color: red; font-size: 16px;"
+"This is a demonstration of kotlinx.html."
}.toString().trim()
}
Finally, we should test our HTML generation to ensure it works as expected. Let’s write unit tests for both HTML generation functions:
class HtmlBuilderUnitTest {
@Test
fun testBuildHtml() {
val expectedHtml = """
<html>
<head>
<title>My Kotlin HTML Page</title>
</head>
<body>
<h1>Welcome to Kotlin HTML Builder</h1>
<p>This is a demonstration of kotlinx.html.</p>
<ul>
<li>Item 0</li>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</body>
</html>
""".trimIndent()
val actualHtml = buildHTML()
assertEquals(expectedHtml, actualHtml)
}
@Test
fun testParagraphAttributes() {
val expectedHtml = """
<p id="intro-paragraph" class="intro" style="color: red; font-size: 16px;">This is a demonstration of kotlinx.html.</p>
""".trimIndent()
val actualHtml = buildParagraphWithAttributes()
assertEquals(expectedHtml, actualHtml)
}
}
With these tests, we’ve shown that we were able to generate the expected HTML strings.
In this article, we’ve explored kotlinx.html, a powerful tool for simplifying HTML generation in Kotlin.
We’ve explored practical usage and showcased how to construct a simple HTML page using Kotlin, emphasizing the library’s concise and intuitive DSL.