使用KotlinDSL在HTML meta数据中使用“property

sz81bmfz  于 2023-02-13  发布在  Kotlin
关注(0)|答案(2)|浏览(130)

嘿,我怎么可以使用HTML内容,如:
<meta property="og:locale" content="en"/>
使用KotlinHTML类型安全生成器?我在 meta函数中找不到类似属性变量的内容

k97glaaz

k97glaaz1#

meta(content = "en") {
    attributes["property"] = "og:locale"
}
nc1teljy

nc1teljy2#

如果你需要使用很多这样的标签,对@М и х а и л Н а ф т а л ь的答案进行类型安全扩展如下:

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

然后,您可以像这样使用它:

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

相关问题