generated from ElnuDev/go-project
Compare commits
No commits in common. "ed11a84dca08ec3b0b114142e66811545e9c2257" and "f433ce30c30317dc0281f79734a4207754c88de5" have entirely different histories.
ed11a84dca
...
f433ce30c3
7 changed files with 3 additions and 139 deletions
15
click.templ
15
click.templ
|
@ -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
|
|
||||||
})
|
|
||||||
}
|
|
4
go.mod
4
go.mod
|
@ -1,5 +1,3 @@
|
||||||
module ElnuDev/shiritori-go
|
module ElnuDev/go-project
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require github.com/a-h/templ v0.2.304 // indirect
|
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -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=
|
|
24
main.go
24
main.go
|
@ -1,27 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"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() {
|
func main() {
|
||||||
http.Handle("/", http.FileServer(http.Dir("static")))
|
fmt.Println("Hello, World!")
|
||||||
http.HandleFunc("/api/click", generateHandler(
|
|
||||||
func() templ.Component { return Click(clicks) },
|
|
||||||
func() { clicks++ },
|
|
||||||
))
|
|
||||||
http.ListenAndServe(":3333", nil)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,5 @@
|
||||||
pkgs.mkShell {
|
pkgs.mkShell {
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
go
|
go
|
||||||
gopls
|
|
||||||
];
|
];
|
||||||
shellHook = ''
|
|
||||||
export PATH="$HOME/go/bin:$PATH"
|
|
||||||
go install github.com/a-h/templ/cmd/templ@latest
|
|
||||||
'';
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
<!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 a new issue