Kotlin HTML-Builder [英] Kotlin HTML-Builder
本文介绍了Kotlin HTML-Builder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Kotlin页面中,在HTML-Builder
下,我可以看到以下代码,该如何简单地使用它.tk文件?该怎么开始呢?
In Kotlin page, under HTML-Builder
I can see the below code, how can I use it in simple .tk file? how t get started here?
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 }
}
}
}
推荐答案
您指的是用Kotlin编写的 DSL ,用于通过构建器构造HTML.可以在以下位置找到该库: https://github.com/Kotlin/kotlinx.html
You’re referring to a DSL written in Kotlin for constructing HTML through the builder. The library can be found here: https://github.com/Kotlin/kotlinx.html
这是一个正在运行的示例:
Here's a running example:
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")))
}
}
最后是相应的输出:
<?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>
这篇关于Kotlin HTML-Builder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文