Has*_*sef 1 kotlin kotlinx-html kotlin-html-builder
在Kotlin 页面中,HTML-Builder我可以看到下面的代码,如何在简单的 .tk 文件中使用它?如何开始?
val data = mapOf(1 to "one", 2 to "two")
createHTML().table {
for ((num, string) in data) {
Iterate over data
tr {
Functions to create HTML tags
td { +"$num" }
td { +string }
}
}
}
Run Code Online (Sandbox Code Playgroud)
您指的是用 Kotlin 编写的用于通过构建器构建 HTML的DSL。该库可以在这里找到:https : //github.com/Kotlin/kotlinx.html
这是一个正在运行的示例:
fun main(args: Array<String>) {
val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
val html = document.create.html {
head {
title("Hello world")
}
body {
h1("h1Class"){
+"My header1"
}
p("pClass"){
+"paragraph1"
}
}
}
intoStream(html, System.out)
}
fun intoStream(doc: Element, out: OutputStream) {
with(TransformerFactory.newInstance().newTransformer()){
setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no")
setOutputProperty(OutputKeys.METHOD, "xml")
setOutputProperty(OutputKeys.INDENT, "yes")
setOutputProperty(OutputKeys.ENCODING, "UTF-8")
setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4")
transform(DOMSource(doc),
StreamResult(OutputStreamWriter(out, "UTF-8")))
}
}
Run Code Online (Sandbox Code Playgroud)
最后是相应的输出:
<?xml version="1.0" encoding="UTF-8"?><html>
<head>
<title>Hello world</title>
</head>
<body>
<h1 class="h1Class">My header1</h1>
<p class="pClass">paragraph1</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)