Kotlin-Htmx is a Kotlin DSL for the htmx library. It provides a type-safe way to generate htmx attributes and configuration.
As the goal of this library is to be usable with any Kotlin web framework, it does not provide any specific integration. Instead, it provides a few code snippets that have to be integrated into your project.
If you are using kotlinx-html the following snippets can be used to integrate Kotlin-Htmx into your project.
If you want to use a scope function to set the headers, you can use the following code snippet.
fun HTMLTag.hx(block: HtmxHtmlAttributes.() -> Unit) = HtmxHtmlAttributes(attributes).block()
If you want to set the headers using a property, you can use the following code snippet.
val HTMLTag.hx
get() = HtmxHtmlAttributes(attributes)
fun HEAD.htmxConfig(block: HtmxConfiguration.() -> Unit) = meta {
name = "htmx-config"
content = HtmxConfiguration().apply(block).toConfigString()
}
fun HTMLTag.hxClasses(block: HtmxCSSClasses.() -> Unit) {
attributes["class"] += HtmxCSSClasses().apply(block).classString
}
fun RoutingResponse.hx(block: HtmxResponseHeaders.() -> Unit) = mutableMapOf<String, String>().also {
HtmxResponseHeaders(it).apply(block)
}.forEach(headers::append)
val RoutingRequest.hx
get() = HtmxRequestHeaders(headers.flattenEntries().toMap())