liveBook · Manning

Html builder

As explained in Kotlin in Action, 2E

An HTML builder is a type-safe builder API that constructs an HTML document or fragment through nested declarations.

An HTML builder is a type-safe Kotlin DSL for declaring an HTML document or fragment as a nested object hierarchy. Tags are ordinary functions receiving lambdas with receivers, so the receiver type controls which tags are available at each nesting level. section 13.2.2

A nested table declaration

fun createSimpleTable() = createHTML().table { tr { td { +"cell" } } } Here, table exposes tr, tr exposes td, and unary plus adds text to the surrounding cell. The API therefore mirrors HTML grammar while remaining regular Kotlin code. section 13.2.2

How Kotlin in Action, 2E covers it

How deep it goes
Explains receiver-based name resolution, tag constraints, rendering, dynamic repetition, and DSL-marker scope control.
Examples
Uses nested tables, text cells, explicit receivers, generated strings, and loops producing repeated rows.
What you can do after
Build it from scratch as a small Kotlin HTML generator with constrained nesting and string rendering.
How it connects
Connects Kotlin DSLs with lambdas with receivers, higher-order functions, operator overloading, and DSL markers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.