Skip to content

Commit

Permalink
Add example of disable element extension
Browse files Browse the repository at this point in the history
  • Loading branch information
godsarmy committed Aug 2, 2024
1 parent e46a6da commit 62c5ce7
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
47 changes: 47 additions & 0 deletions disable-element/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"embed"
"html/template"
"net/http"
"time"

"github.com/gin-gonic/gin"
)

var htmx_version = "latest"
var htmx_ext_version = "latest"
var nunjucks_version = "3.2.4"
var bootstrap_version = "latest"

//go:embed templates/*
var embed_fs embed.FS

func main() {

router := gin.Default()
templ := template.Must(
template.New("").Delims("{[{", "}]}").ParseFS(embed_fs, "templates/*.tmpl"),
)
router.SetHTMLTemplate(templ)
router.GET("/", func(c *gin.Context) {
c.HTML(
http.StatusOK,
"index.html.tmpl",
gin.H{
"htmx_version": htmx_version,
"htmx_ext_version": htmx_ext_version,
"nunjucks_version": nunjucks_version,
"bootstrap_version": bootstrap_version,
},
)
})

router.GET("/whatever", func(c *gin.Context) {
// sleep one second before sending data to simulate slow server
time.Sleep(time.Second)
c.JSON(http.StatusOK, gin.H{"message": "disable element example"})
})

router.Run(":8080")
}
52 changes: 52 additions & 0 deletions disable-element/templates/index.html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html lang='en' class=''>

<head>

<meta charset='UTF-8'>
<title>disable-element</title>

<script src="https://unpkg.com/htmx.org@{[{ .htmx_version }]}"></script>
<script src="https://unpkg.com/htmx-ext-client-side-templates@{[{ .htmx_ext_version }]}/client-side-templates.js"></script>
<script src="https://unpkg.com/htmx-ext-disable-element@latest/disable-element.js"></script>
<script src="https://unpkg.com/nunjucks@{[{ .nunjucks_version }]}/browser/nunjucks.js"></script>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@{[{ .bootstrap_version }]}/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@{[{ .bootstrap_version }]}/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body hx-ext="debug">
<div class="container" hx-ext="client-side-templates">
<a href="https://github.com/bigskysoftware/htmx-extensions/blob/main/src/disable-element/README.md" class="link-primary">htmx disable element extension example</a>
<p>
<button class="btn btn-primary"
hx-get="/whatever"
hx-ext="disable-element"
hx-disable-element="self"
hx-target="#show"
nunjucks-template="show-template">
Click Me to disable
</button>

<button class="btn btn-primary"
hx-get="/whatever"
hx-ext="disable-element"
hx-disable-element="#to-disable"
hx-target="#show"
nunjucks-template="show-template">
Click Me to disable sibling
</button>
<button class="btn btn-primary" id="to-disable">Watch me being disabled</button>

<p>
<div id="show"></div>

<script id="show-template" type="x-tmpl-nunjucks">
<div class="text-success">{{ message }}</div>
</script>

</div>
</body>

</html>

0 comments on commit 62c5ce7

Please sign in to comment.