generated from ElnuDev/go-project
parent
a48a97a7f7
commit
ed11a84dca
@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func plural(singular, plural string, count uint) string {
|
||||
if count == 1 {
|
||||
return singular
|
||||
} else {
|
||||
return plural
|
||||
}
|
||||
}
|
||||
|
||||
templ Click(clicks uint) {
|
||||
<div>The button has been clicked { fmt.Sprintf("%d", clicks) }{ " " }{ plural("time", "times", clicks) }.</div>
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
// Code generated by templ@v0.2.304 DO NOT EDIT.
|
||||
|
||||
package main
|
||||
|
||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||
|
||||
import "github.com/a-h/templ"
|
||||
import "context"
|
||||
import "io"
|
||||
import "bytes"
|
||||
|
||||
// GoExpression
|
||||
import "fmt"
|
||||
|
||||
func plural(singular, plural string, count uint) string {
|
||||
if count == 1 {
|
||||
return singular
|
||||
} else {
|
||||
return plural
|
||||
}
|
||||
}
|
||||
|
||||
func Click(clicks uint) templ.Component {
|
||||
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) {
|
||||
templBuffer, templIsBuffer := w.(*bytes.Buffer)
|
||||
if !templIsBuffer {
|
||||
templBuffer = templ.GetBuffer()
|
||||
defer templ.ReleaseBuffer(templBuffer)
|
||||
}
|
||||
ctx = templ.InitializeContext(ctx)
|
||||
var_1 := templ.GetChildren(ctx)
|
||||
if var_1 == nil {
|
||||
var_1 = templ.NopComponent
|
||||
}
|
||||
ctx = templ.ClearChildren(ctx)
|
||||
// Element (standard)
|
||||
_, err = templBuffer.WriteString("<div>")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Text
|
||||
var_2 := `The button has been clicked `
|
||||
_, err = templBuffer.WriteString(var_2)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// StringExpression
|
||||
var var_3 string = fmt.Sprintf("%d", clicks)
|
||||
_, err = templBuffer.WriteString(templ.EscapeString(var_3))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// StringExpression
|
||||
var var_4 string = " "
|
||||
_, err = templBuffer.WriteString(templ.EscapeString(var_4))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// StringExpression
|
||||
var var_5 string = plural("time", "times", clicks)
|
||||
_, err = templBuffer.WriteString(templ.EscapeString(var_5))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Text
|
||||
var_6 := `.`
|
||||
_, err = templBuffer.WriteString(var_6)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = templBuffer.WriteString("</div>")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !templIsBuffer {
|
||||
_, err = io.Copy(w, templBuffer)
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
module ElnuDev/shiritori-go
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/a-h/templ v0.2.304 // indirect
|
||||
|
@ -0,0 +1,2 @@
|
||||
github.com/a-h/templ v0.2.304 h1:vIgCNazkW6NiYifFIGYNRfBkoBzOMZMO1NibIayzihE=
|
||||
github.com/a-h/templ v0.2.304/go.mod h1:3oc37WS5rpDvFGi6yeknvTKt50xCu67ywQsM43Wr4PU=
|
@ -1,7 +1,27 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
)
|
||||
|
||||
var clicks uint = 0
|
||||
|
||||
func generateHandler(template func() templ.Component, handler func()) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
template().Render(context.Background(), w)
|
||||
handler()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
http.Handle("/", http.FileServer(http.Dir("static")))
|
||||
http.HandleFunc("/api/click", generateHandler(
|
||||
func() templ.Component { return Click(clicks) },
|
||||
func() { clicks++ },
|
||||
))
|
||||
http.ListenAndServe(":3333", nil)
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Shiritori</title>
|
||||
<script src="https://unpkg.com/htmx.org@1.9.3"></script>
|
||||
</head>
|
||||
<body>
|
||||
<button hx-get="/api/click" hx-swap="innerHTML">Click me!</button>
|
||||
</body>
|
||||
</html>
|
Reference in new issue