SiebelsTim
04/12/2018, 6:48 AMgenerateCmponent
does not return anything. Then look at the call-order. generateCmponent
is called first, adding the table. After that, the body is generated. I would suggest that you return your table from the generateCmponent
function and not append the writer.zucen.co
04/12/2018, 8:04 AMSiebelsTim
04/13/2018, 8:02 AMzucen.co
04/14/2018, 6:19 AMSiebelsTim
04/14/2018, 7:18 AMHtmlBlockTag
as parent. The follow code was refactored quite a bit, so let's run through the changes:
* I excluded the writer from the parameters where it is not needed anymore.
* generateCmponent
is now an extension function to HtmlBlockTag
.
* here (https://github.com/Kotlin/kotlinx.html/wiki/Micro-templating-and-DSL-customizing) you have a little documentation on how to build functions that append the HTML. HtmlBlockTag
functions as parent to all (?) of the HTML Tags.
* I made the this
calls explicit, so you see immediatly when I'm using an extension function.
import kotlinx.html.*
import kotlinx.html.stream.appendHTML
import java.io.StringWriter
import java.io.Writer
fun HtmlBlockTag.generateCmponent(codes: List<String>) {
this.table {
for (code in codes) {
tr {
td {
+code
}
}
}
}
}
fun generateLayout(writer: Writer, componentGenerator: HtmlBlockTag.() -> Unit) {
writer
.appendHTML()
.html {
body {
this.componentGenerator()
}
}
}
fun pageGenerate(codes: List<String>, writer: Writer) {
generateLayout(writer) {
this.generateCmponent(codes)
}
}
fun main(args: Array<String>) {
val codes = listOf("xxx", "qf9e48t9u23849g", "uidddd00")
val writer = StringWriter()
pageGenerate(codes, writer)
println(writer.toString())
}
zucen.co
04/15/2018, 5:26 AMSiebelsTim
04/15/2018, 6:43 AMzucen.co
04/15/2018, 7:09 AM