Using "property" in HTML meta data with kotlin DSL

342 views Asked by At

Hey how can i use html content like:

<meta property="og:locale" content="en"/>

with the Kotlin HTML Type Safe builder? I cannot find something like the property variable in the meta function

2

There are 2 answers

0
Михаил Нафталь On BEST ANSWER
meta(content = "en") {
    attributes["property"] = "og:locale"
}
0
Devabc On

A typesafe extension to the answer of @Михаил Нафталь, in case you need to use a lot of those tags, is this:

fun HEAD.metaProperty(propertyName: String, content: String) = meta(content = content) {
    attributes["property"] = propertyName
}

Then you can use it like this:

val htmlCode = buildString {
    appendHTML().html {
        head {
            metaProperty("og:locale", "en")
        }
        body {
            h1 { +"Hello" }
        }
    }
}
println(htmlCode)