json script with kotlinx.html

409 Views Asked by At

building html page using kotlinx.html

lots of content built without problems, but stumbling on adding json script tag with an id so the code on the client can retrieve the json data.

what to have the code something like..

<script id="jsondata" type="application/json">
   { "jsondata": "is here" }
</script>

using kotlinx.html i can have

            script(type="application/json"){
                    +"""{ "jsondata": "is here"}"""
            }

but unlike other tags, script does not seem to have an id property. Any ideas on how to get set the id for the script tag?

1

There are 1 best solutions below

0
On

All you need is Tag.attributes (mutable map with tag attributes).

script(type = "application/json") {
    attributes["id"] = "jsondata"
    unsafe { +"""{ "jsondata": "is here" }""" }
}

will give you this result:

<script type="application/json" id="jsondata">{ "jsondata": "is here" }</script>