Skip to content

Instantly share code, notes, and snippets.

@keynmol
Last active June 18, 2024 13:13
Show Gist options
  • Select an option

  • Save keynmol/678797c714480333912c7656ae5aa7ea to your computer and use it in GitHub Desktop.

Select an option

Save keynmol/678797c714480333912c7656ae5aa7ea to your computer and use it in GitHub Desktop.
Scala example of using htmx
//> using dep com.lihaoyi::cask::0.9.1
//> using dep com.lihaoyi::scalatags::0.12.0
import scalatags.Text.all.*
val hxPost = attr("hx-post")
val hxSwap = attr("hx-swap")
object MinimalApplication extends cask.MainRoutes:
override def port: Int = 9991
@cask.get("/")
def hello() =
html(
body(
h1("Scala :love: htmx"),
listLogic(0)
),
script(src := "https://unpkg.com/htmx.org@1.9.6")
)
def listLogic(n: Int) =
div(
for i <- 0 to n yield p(s"Item ${i}"),
button(hxPost := "/clicked-back", hxSwap := "outerHTML", "Reset")
)
@cask.post("/clicked")
def clicked(request: cask.Request) =
div(
for i <- 0 to 5 yield p(s"Item ${i}"),
button(hxPost := "/clicked-back", hxSwap := "outerHTML", "Reset")
)
@cask.post("/clicked-back")
def clickedback(request: cask.Request) =
button(hxPost := "/clicked", hxSwap := "outerHTML", "Show list")
initialize()
end MinimalApplication
@mbesida

mbesida commented Oct 7, 2023

Copy link
Copy Markdown

seems like there should be hxTarget, otherwise it doesn't "swap" properly

@ppurang

ppurang commented Oct 9, 2023

Copy link
Copy Markdown

@mbesida I took a shot at it: https://gist.github.com/ppurang/c2c8b22a6bf27dcf1665721f7e0b7cd9 Might not be what @keynmol intended...

@windymelt

Copy link
Copy Markdown

Cool. Since Scala.js can be run in Cloudflare Workers, it can be very fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment