Hi, is there an example available anywhere how to ...
# kotlinx-html
j
Hi, is there an example available anywhere how to include data from variables in a template with kotlinx.html? If e.g., I have a list of DokumentIds, the following works but I don't think it is really the way it ought to be done:
Copy code
class ContentTemplate(idList: List<DokumentId>): Template<HTML> {
    val ids = idList
    override fun HTML.apply() {
        body {
            ul {
                ids.forEach { it ->
                    li {
                        a(UriRenderer.render(it).toString()) { +it.shortString() }
                    }
                }
            }
        }
    }
}
c
Looks good to me. I'd not re-assign
idList
to
ids
. It seems ytou are making a sub-template, so the
body
tag was unexpected. To make a subtemplate I'd not overwrite
apply
in a class, but simply make it a function like:
Copy code
@HtmlTagMarker
fun FlowContent.contentTemplate(idList: List<DokumentId>) {
    ul {
        ids.forEach { it ->
            li {
                a(UriRenderer.render(it).toString()) { +it.shortString() }
            }
        }
    }
}
j
Cies, thanks a lot for the reply. If I don't re-assign idList but try to access it directly from the template, Intellij complains that it can't find it. I did not try to compile it with gradle, will do that this evening. You're right, the ultimate idea is to have a subtemplate that displays a list of Ids and to include it in a full main template or a bare-bones one depending on the use case. I just wanted to get a simple example working before I try more complex stuff. I'll try your suggestion with the extension function.
I just changed the code to use your suggestion with the extension function and that works. I also don't need to assign the parameter to a val inside the function. If I try to do the same in a template, I get "Unresolved reference: idList". Any idea why that is the case? I'm still pretty sure there is lots of stuff I'm missing. Right now I'm trying to figure out how I can declare an instance of FlowContent.contentTemplate and pass it to a template but it seems I'm hindered by my limited understanding of how the DSL actually works. Do you have any pointers where I could gain more insight? Would it maybe help to read Venkat Subramaniam's book on Kotlin DSLs?
c
I've not read it (Venkat's). We've learned all there is from reaaaally carefully reading everything there is on kotlinx.html. We have slowly developed a whole system of template, sub-templates (like your FlowContent.contentTemplate function), and super-templates (a.k.a. layouts). A super-template is called explicitly by a template (or another super-template): it's mostly just (extension) function calls.
j
A super-template is called explicitly by a template (or another super-template):
That sounds very interesting. Is this public somewhere?
c
I've basically pasted it in response to your last question. Beyond that it's not public (also not worthy of being public, it's too simple)