Small follow-up on my journey of using `HTMX` and ...
# kotlinx-html
p
Small follow-up on my journey of using
HTMX
and
kotlinx-html
. Turns out it is not difficult to create inner html snippets using the
filter
option. Simple example:
Copy code
fun createInnerUL(block: UL.() -> Unit): String {
    var first = true
    return createHTML().filter {
        if (first && it.tagName == "ul") {
            first = false
            SKIP
        } else {
            PASS
        }
    }.ul { block() }
}
Copy code
val a = createInnerUL {
    li { +"one" }
    li { +"two" }
    li {
        ul {
            li {
                +"two-a"
            }
        }
    }
}
Copy code
println(a)

<li>one</li>
<li>two</li>
<li>
  <ul>
    <li>two-a</li>
  </ul>
</li>
a
I actually made an Integration with KTor for something like that. I can push it later to GitHub since I didn't manage to upload stuff to Sonatype yet (idk somehow it's bugged and nothing works)
p
Sounds great. Have been using it also with KTor, but so far mostly prototyping and few pages. Just to understands the pros and cons better.
a
I'm on my computer now. That's what I did
Copy code
suspend fun ApplicationCall.respondHTMX(status: HttpStatusCode = HttpStatusCode.OK, block: BODY.() -> Unit) {
    val text = createHTML(true).filter {
            if (it.tagName == "body") SKIP else PASS
        }.body(block = block)

    if (text.isEmpty()) {
        throw Exception("Provided HTML was empty")
    }

    respond(TextContent(text, ContentType.Text.Html.withCharset(Charsets.UTF_8), status))
}
👍 1
p
Indeed very similar. Did you find anything related to htmx that were difficult to accomplish in ktor?
a
The only thing is ensuring that HTMX is included. I created an includeHtmx function for the head, but no reliable way to validate if it has been called or Htmx included in another way
r
Hey, digging up this old thread. How did you manage to get the custom htmx attributes (hx-swap, hx-trigger, etc) to be included?
Ah I see. Something like this
433 Views