# Dynamic DOM rendering with kotlinx.html

**URL:** <https://discuss.kotlinlang.org/t/dynamic-dom-rendering-with-kotlinx-html/8472>\
**Category:** Libraries\
**Created:** [July 8, 2018, 3:58pm UTC](https://discuss.kotlinlang.org/t/dynamic-dom-rendering-with-kotlinx-html/8472 "2018-07-08T15:58:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 8, 2018, 3:58pm UTC](https://discuss.kotlinlang.org/t/dynamic-dom-rendering-with-kotlinx-html/8472/1 "2018-07-08T15:58:47Z")

</div>

HTML seems to be the most universal way to perform visualization of the data and `kotlinx.html` library allows to use it with style, but sadly, the documentation is still in early stage, so I would like to ask for advice.

What I want to do is to dynamically (in runtime) add blocks to existing html template (like adding nodes to `body` block) and then on each change render the whole document and send somewhere (for example to javafx `WebView`).

I have a function that consumes `TagConsumer<*>` and does required modifications. How can I actually dynamically modify the content inside the node and notify somebody to render the document?

My best attempt so far looked like something like this:

```kotlin

        lateinit var out: HTMLOutput

        val document = createHTMLDocument().onFinalize { from, partial ->
            println(from.serialize(true))
        }.html {
            head { }
            body {
                out = HTMLOutput(consumer)
            }
        }

        out.render("this is my text")
        out.render("this is another text")

```

But it obviously did not work. It looks I have to create the document and the node separately and then reapply node to the document each time, something is changed. Is there a better way?

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 8, 2018, 5:02pm UTC](https://discuss.kotlinlang.org/t/dynamic-dom-rendering-with-kotlinx-html/8472/2 "2018-07-08T17:02:20Z")

</div>

The solution that actually worked:

```kotlin
        val document = createHTMLDocument().html {
            head { }
            body { }
        }

        val node = document.getElementsByTagName("body").item(0)

        val appender = node.append.onFinalize { from, partial ->
            println(from.ownerDocument.serialize(true))
        }

        val out = HTMLOutput(appender)

        out.render("this is my text")
        out.render("this is another text")

```

Still, it seems a little bit off. I wonder if there is something better.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [July 9, 2018, 5:40am UTC](https://discuss.kotlinlang.org/t/dynamic-dom-rendering-with-kotlinx-html/8472/3 "2018-07-09T05:40:07Z")

</div>

Maybe decorating TagConsumer can help.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 9, 2018, 5:44am UTC](https://discuss.kotlinlang.org/t/dynamic-dom-rendering-with-kotlinx-html/8472/4 "2018-07-09T05:44:09Z")

</div>

Could you provide an example?
