generated from ElnuDev/go-project
parent
20db5cdaad
commit
3fc30c0d3a
@ -1,15 +0,0 @@
|
|||||||
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>
|
|
||||||
}
|
|
@ -1,80 +0,0 @@
|
|||||||
// 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,2 +0,0 @@
|
|||||||
github.com/a-h/templ v0.2.304 h1:vIgCNazkW6NiYifFIGYNRfBkoBzOMZMO1NibIayzihE=
|
|
||||||
github.com/a-h/templ v0.2.304/go.mod h1:3oc37WS5rpDvFGi6yeknvTKt50xCu67ywQsM43Wr4PU=
|
|
@ -1,28 +1,36 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/a-h/templ"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var clicks uint = 0
|
type handler = func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
func generateHandler(template func() templ.Component, handler func()) func(http.ResponseWriter, *http.Request) {
|
func generateHandler(file string, handler func(), data func() any) handler {
|
||||||
|
tmpl := template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file)))
|
||||||
return func(w http.ResponseWriter, r *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()
|
handler()
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
tmpl.Execute(w, data())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func generateClick() handler {
|
||||||
|
var clicks uint = 0
|
||||||
|
return generateHandler(
|
||||||
|
"click.html",
|
||||||
|
func() { clicks++ },
|
||||||
|
func() any { return clicks },
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.Handle("/", http.FileServer(http.Dir("static")))
|
http.Handle("/", http.FileServer(http.Dir("static")))
|
||||||
http.HandleFunc("/api/click", generateHandler(
|
http.HandleFunc("/api/click1", generateClick())
|
||||||
func() templ.Component { return Click(clicks) },
|
http.HandleFunc("/api/click2", generateClick())
|
||||||
func() { clicks++ },
|
http.HandleFunc("/api/click3", generateClick())
|
||||||
))
|
|
||||||
log.Fatal(http.ListenAndServe(":3333", nil))
|
log.Fatal(http.ListenAndServe(":3333", nil))
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
The button has been clicked <b>{{ . }} time{{ if ne . 1 }}s{{ end }}</b>!
|
Reference in new issue