0
0
Kotlinprogramming~10 mins

HTML builder example pattern in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HTML builder example pattern
Start HTML builder function
Create root element (html)
Add child elements (head, body)
Add nested elements (title, h1, p)
Set text content and attributes
Return built HTML string
End
The builder function creates HTML elements step-by-step, nesting children and setting content, then returns the full HTML string.
Execution Sample
Kotlin
fun html(block: HTML.() -> Unit): String {
  val html = HTML()
  html.block()
  return html.render()
}

val page = html {
  head { title { text = "My Page" } }
  body { h1 { text = "Welcome" } p { text = "Hello, world!" } }
}
This code builds a simple HTML page with head and body using a Kotlin HTML builder pattern.
Execution Table
StepActionElement CreatedParent ElementContent/Attributes
1Call html builder functionHTML rootNoneStart building
2Invoke head blockheadhtmlEmpty
3Invoke title block inside headtitleheadtext = "My Page"
4Invoke body blockbodyhtmlEmpty
5Invoke h1 block inside bodyh1bodytext = "Welcome"
6Invoke p block inside bodypbodytext = "Hello, world!"
7Render HTML to stringN/AN/AFull HTML string returned
8EndN/AN/ABuilder function returns
💡 All elements created and nested correctly, builder returns final HTML string.
Variable Tracker
VariableStartAfter headAfter titleAfter bodyAfter h1After pFinal
htmlempty HTML objecthtml with headhtml with head and titlehtml with head/title and bodyhtml with head/title/body and h1html with head/title/body/h1 and pfinal HTML structure
current elementhtmlheadtitlebodyh1pN/A
Key Moments - 3 Insights
Why does the 'title' element get added inside 'head' and not directly inside 'html'?
Because the 'title' block is called inside the 'head' block (see execution_table step 3), so it becomes a child of 'head'.
How does the builder know where to add each new element?
Each block is an extension function on the current element, so nested calls add children to the current element (see variable_tracker 'current element' changes).
What does the 'render' function do at the end?
It converts the built HTML object tree into a string representation (see execution_table step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, which element is created and where is it added?
AA p element added inside the head element
BA title element added inside the html element
CAn h1 element added inside the body element
DA body element added inside the h1 element
💡 Hint
Check the 'Element Created' and 'Parent Element' columns at step 5 in execution_table.
At which step does the builder add the text 'Hello, world!'?
AStep 3
BStep 6
CStep 2
DStep 4
💡 Hint
Look at the 'Content/Attributes' column for the p element in execution_table.
If we removed the 'body' block call, what would happen to the final HTML structure?
AThe HTML would have no body element or its children
BThe head element would be missing
CThe title text would be empty
DThe h1 and p elements would be added directly under html
💡 Hint
Refer to variable_tracker and execution_table steps 4-6 about body and its children.
Concept Snapshot
Kotlin HTML builder pattern:
- Use a function with a lambda receiver (block: HTML.() -> Unit)
- Create root element object
- Call block to add nested elements
- Each nested block adds children to current element
- Render final HTML tree to string
- Enables readable, nested HTML construction in code
Full Transcript
This example shows how a Kotlin HTML builder function works step-by-step. First, the html function creates a root HTML object. Then it calls the block lambda, which adds child elements like head and body. Inside head, a title element is added with text. Inside body, h1 and p elements are added with text content. Each nested block adds elements as children of the current element. Finally, the render function converts the built object tree into a string. The execution table traces each step, showing which element is created and where it is added. The variable tracker shows how the current element changes as nested blocks run. Key moments clarify why elements nest as they do and how rendering works. The visual quiz tests understanding of element creation and nesting. This pattern helps write HTML in Kotlin code clearly and safely.