add simple htmx example
This commit is contained in:
+21
@@ -9,6 +9,22 @@ import (
|
|||||||
var x = 2
|
var x = 2
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
http.HandleFunc("/data", func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
w.WriteHeader(200)
|
||||||
|
const tpl = `
|
||||||
|
<p>Hey check out the data</p>
|
||||||
|
`
|
||||||
|
t, err := template.New("default").Parse(tpl)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = t.Execute(w, nil)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
// fmt.Fprintf(w, "Welcome to the home page!")
|
// fmt.Fprintf(w, "Welcome to the home page!")
|
||||||
@@ -19,9 +35,14 @@ func main() {
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>{{.Title}}</title>
|
<title>{{.Title}}</title>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
|
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/htmx.min.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{range .Items}}<div class="bg-red-200">{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
|
{{range .Items}}<div class="bg-red-200">{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
|
||||||
|
|
||||||
|
<button hx-post="/data" hx-swap="outerHTML" class="bg-pink-500">
|
||||||
|
Click Me
|
||||||
|
</button>
|
||||||
</body>
|
</body>
|
||||||
</html>`
|
</html>`
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user