Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@keynmol
Last active October 9, 2023 20:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keynmol/678797c714480333912c7656ae5aa7ea to your computer and use it in GitHub Desktop.
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
Copy link

mbesida commented Oct 7, 2023

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

@ppurang
Copy link

ppurang commented Oct 9, 2023

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

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