Typesafe HTML DSL
The kotlinx.html library provides the ability to generate DOM elements using statically typed HTML builders (and besides JavaScript, it is even available on the JVM target!) To use the library, include the corresponding repository and dependency to our build.gradle.kts
file:
Once the dependency is included, you can access the different interfaces provided to generate the DOM. To render a headline, some text, and a link, the following snippet would be sufficient, for example:
When running this example in the browser, the DOM will be assembled in a straightforward way. This is easily confirmed by checking the Elements of the website using the developer tools of our browser:
To learn more about the kotlinx.html
library, check out the GitHub Wiki, where you can find more information about how to create elements without adding them to the DOM, binding to events like onClick
, and examples on how to apply CSS classes to your HTML elements, to name just a few.