Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/silas00301/kotlin-htmx
A Kotlin Multiplatform htmx library
https://github.com/silas00301/kotlin-htmx
htmx kotlin kotlin-multiplatform kotlinx-html ktor
Last synced: 10 days ago
JSON representation
A Kotlin Multiplatform htmx library
- Host: GitHub
- URL: https://github.com/silas00301/kotlin-htmx
- Owner: silas00301
- License: mit
- Created: 2024-07-07T00:22:29.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-08-11T14:27:38.000Z (6 months ago)
- Last Synced: 2024-08-11T19:38:53.930Z (6 months ago)
- Topics: htmx, kotlin, kotlin-multiplatform, kotlinx-html, ktor
- Language: Kotlin
- Homepage:
- Size: 286 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Kotlin-Htmx
Kotlin-Htmx is a Kotlin DSL for the [htmx](https://htmx.org) library. It provides a type-safe way to generate htmx
attributes and configuration.# Usage
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.- [kotlinx-html](#kotlinx-html)
## kotlinx-html
If you are using kotlinx-html the following snippets can be used to integrate Kotlin-Htmx into your project.
### Attributes
If you want to use a scope function to set the headers, you can use the following code snippet.
```kotlin
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.
```kotlin
val HTMLTag.hx
get() = HtmxHtmlAttributes(attributes)
```### Configuration
```kotlin
fun HEAD.htmxConfig(block: HtmxConfiguration.() -> Unit) = meta {
name = "htmx-config"
content = HtmxConfiguration().apply(block).toConfigString()
}
```### CSS Classes
```kotlin
fun HTMLTag.hxClasses(block: HtmxCSSClasses.() -> Unit) {
attributes["class"] += HtmxCSSClasses().apply(block).classString
}
```### Headers
#### Response Headers
```kotlin
fun RoutingResponse.hx(block: HtmxResponseHeaders.() -> Unit) = mutableMapOf().also {
HtmxResponseHeaders(it).apply(block)
}.forEach(headers::append)
```#### Request Headers
```kotlin
val RoutingRequest.hx
get() = HtmxRequestHeaders(headers.flattenEntries().toMap())
```