June 5, 2025

Kotlin DSL by Example

Summary One of the more interesting features of Kotlin is the ability to create your own DSL (domain-specific-language). There are many use cases for this: UI elements: Jetpack compose, kotlinx.html… Configurations: Ktor configuration Builder pattern: In fact, anywhere we can use the builder pattern, we can build a DSL around it. This way we can provide a clear and expressive syntax while tucking away validation/transformation/etc logic. An Example Let’s say we wanted to build a structure to represent our CI pipeline, because maybe we want to generate our CI pipeline configuration dynamically. We also want to use a builder and do some validation on the values that the user can provide for this structure. Read more

March 30, 2024

Tinkering With Ktor 2: Using HTML DSL for page templates

Photo by Pankaj Patel on Unsplash Summary There are a bunch of templating engines available for Ktor, each one with its own strengths and weaknesses. But, I don’t like that I have to add yet another engine to the runtime (especially when I plan to run React or Vue anyway) so why not try using a simple feature of Kotlin to build our html? Kotlin DSL (Domain Specific Language) capabilities, while having a steep learning curve if you never used them before can be used structures that closely mimic html. This is not a new idea, there have been several attempts at this and the most popular one is the kotlinx.html library. Ktor team already created a convenient way to plug this library into your Ktor project, called ktor-server-html-builder. The kotlinx.html library gives us the basic components of HTML and we can extend that further if we want. Let’s see what can be accomplished with these. Read more

© Ilya Nemtsev 2025

Powered by Hugo.