https://kotlinlang.org logo
Title
s

SiebelsTim

04/12/2018, 6:48 AM
generateCmponent
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.
z

zucen.co

04/12/2018, 8:04 AM
Thanks Tim, hmm I know methods do not return anythig but operate on the writer.. Somehow I thought it is good idea to do like this without creating all dom in memory... rather stream it.. I really tried to understand kolinx more as i like the DSL approach.. just could not find good example explaining how to do it ..
s

SiebelsTim

04/13/2018, 8:02 AM
The StreamBuilder already immediatly appends to the stream, and does not contain a collection of tags in memory. So I guess this already works the way you intend it to work.
z

zucen.co

04/14/2018, 6:19 AM
Well... i did what I thought you meant... = Generate table into String writer and return String from generateComponent but it resulted in this: <html> <body>&lt;table&gt; &lt;tr&gt; &lt;td&gt;xxx&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;qf9e48t9u23849g&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;uidddd00&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; d</body> </html> I maen including string into html structure gets escaped.. Could you please hint me more how to generate part of page and include into layout if you have a minute? Thanks a lot
s

SiebelsTim

04/14/2018, 7:18 AM
Okay, I looked more into it. It turns out you don't need to return anything from it, as it does not build a DOM tree in memory. You can add your HTML elements using a
HtmlBlockTag
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())
}
z

zucen.co

04/15/2018, 5:26 AM
Tim 🙂 thanks so much! I slowly get into new Kotlin habbits after decades of java.. Btw I owe you a jar of caffe or beer depends on your preference, I may jump to see KotlinConf in Budapest so I can payback there if you join too 🙂
s

SiebelsTim

04/15/2018, 6:43 AM
Jar of coffee. Is that a pun? :D I won't be in Budapest unfortunately, but don't worry. I'm glad I could help. :)
z

zucen.co

04/15/2018, 7:09 AM
well.. unintentionall pun you actually discovered 😄 what can i do then..