Skip to content

Instantly share code, notes, and snippets.

@ppurang
Forked from keynmol/README.md
Last active February 18, 2024 04:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ppurang/c2c8b22a6bf27dcf1665721f7e0b7cd9 to your computer and use it in GitHub Desktop.
Save ppurang/c2c8b22a6bf27dcf1665721f7e0b7cd9 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")
val hxTarget = attr("hx-target")
val id = attr("id")
val showList = button(hxPost := "/clicked", hxSwap := "innerHTML", hxTarget := "#contents", "Show list")
val resetList = button(hxPost := "/clicked-back", hxSwap := "innerHTML", hxTarget := "#contents", "Reset")
val reset = p(id := "reset", "Reset...")
object MinimalApplication extends cask.MainRoutes:
override def port: Int = 9991
@cask.get("/")
def hello() =
html(
body(
h1("Scala :loves: htmx"),
div(p(id := "contents", """Hit "Show list" to show a list and "Reset" to reset the list. Hit, Repeat, Enjoy!""")),
showList,
resetList
),
script(src := "https://unpkg.com/htmx.org@1.9.6")
)
@cask.post("/clicked")
def clicked(request: cask.Request) =
div(
for i <- 1 to 5 yield p(s"Item ${i}")
)
@cask.post("/clicked-back")
def clickedback(request: cask.Request) =
reset
initialize()
end MinimalApplication
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment