From 9aac799a55e3b59f04ba2a6bc90a8bffd519fb6b Mon Sep 17 00:00:00 2001 From: Bastian Wegge Date: Fri, 9 Aug 2024 10:01:26 +0200 Subject: [PATCH 1/2] chore: update gofiber example with new rendering --- examples/integration-gofiber/README.md | 21 ++++++++++++ examples/integration-gofiber/go.mod | 2 +- examples/integration-gofiber/home.templ | 12 +++++++ examples/integration-gofiber/home_templ.go | 37 ++++++++++++++++++---- examples/integration-gofiber/main.go | 15 ++++----- 5 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 examples/integration-gofiber/README.md diff --git a/examples/integration-gofiber/README.md b/examples/integration-gofiber/README.md new file mode 100644 index 000000000..38a9337d6 --- /dev/null +++ b/examples/integration-gofiber/README.md @@ -0,0 +1,21 @@ +## Example + +This example demonstrates the usage of templ with gofiber. +As soon as you start the server you can access http://localhost:3000/ and see the rendered page. +If you change the URL to http://localhost:3000/john you will see your parameter printed on the page. +This happens both through parameter passing into the templ component and through context using fiber locals. + +## Tasks + +### build-templ + +``` +templ generate +``` + +### run + +``` +go run . +``` + diff --git a/examples/integration-gofiber/go.mod b/examples/integration-gofiber/go.mod index f8393618e..dd5718406 100644 --- a/examples/integration-gofiber/go.mod +++ b/examples/integration-gofiber/go.mod @@ -3,7 +3,7 @@ module github.com/a-h/templ/examples/integration-gofiber go 1.21 require ( - github.com/a-h/templ v0.2.501 + github.com/a-h/templ v0.2.747 github.com/gofiber/fiber/v2 v2.52.5 ) diff --git a/examples/integration-gofiber/home.templ b/examples/integration-gofiber/home.templ index cadbb23cb..9dc7f5dcd 100644 --- a/examples/integration-gofiber/home.templ +++ b/examples/integration-gofiber/home.templ @@ -1,7 +1,19 @@ package main +import ( + "context" +) + +func NameFromContext(ctx context.Context) string { + if name, ok := ctx.Value("name").(string); ok && name != "" { + return name + } + return "World" +} + templ Home(name string) {
Hello { name }
+
Hello { NameFromContext(ctx) } (from context)
} templ NotFound() { diff --git a/examples/integration-gofiber/home_templ.go b/examples/integration-gofiber/home_templ.go index fbbaf1e64..866872c31 100644 --- a/examples/integration-gofiber/home_templ.go +++ b/examples/integration-gofiber/home_templ.go @@ -1,5 +1,6 @@ // Code generated by templ - DO NOT EDIT. +// templ: version: v0.2.747 package main //lint:file-ignore SA4006 This context is only used if a nested component is present. @@ -7,6 +8,17 @@ package main import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" +import ( + "context" +) + +func NameFromContext(ctx context.Context) string { + if name, ok := ctx.Value("name").(string); ok && name != "" { + return name + } + return "World" +} + func Home(name string) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context @@ -32,13 +44,26 @@ func Home(name string) templ.Component { var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/integration-gofiber/home.templ`, Line: 4, Col: 18} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `home.templ`, Line: 15, Col: 18} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Hello ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(NameFromContext(ctx)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `home.templ`, Line: 16, Col: 34} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(" (from context)
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -59,9 +84,9 @@ func NotFound() templ.Component { }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var3 := templ.GetChildren(ctx) - if templ_7745c5c3_Var3 == nil { - templ_7745c5c3_Var3 = templ.NopComponent + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + if templ_7745c5c3_Var4 == nil { + templ_7745c5c3_Var4 = templ.NopComponent } ctx = templ.ClearChildren(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
404
") @@ -71,5 +96,3 @@ func NotFound() templ.Component { return templ_7745c5c3_Err }) } - -var _ = templruntime.GeneratedTemplate diff --git a/examples/integration-gofiber/main.go b/examples/integration-gofiber/main.go index 82b96a7e0..c66349655 100644 --- a/examples/integration-gofiber/main.go +++ b/examples/integration-gofiber/main.go @@ -4,8 +4,6 @@ import ( "github.com/a-h/templ" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/log" - "github.com/gofiber/fiber/v2/middleware/adaptor" - "net/http" ) func main() { @@ -13,6 +11,7 @@ func main() { app.Get("/:name?", func(c *fiber.Ctx) error { name := c.Params("name") + c.Locals("name", name) if name == "" { name = "World" } @@ -24,13 +23,11 @@ func main() { } func NotFoundMiddleware(c *fiber.Ctx) error { - return Render(c, NotFound(), templ.WithStatus(http.StatusNotFound)) + c.Status(fiber.StatusNotFound) + return Render(c, NotFound()) } -func Render(c *fiber.Ctx, component templ.Component, options ...func(*templ.ComponentHandler)) error { - componentHandler := templ.Handler(component) - for _, o := range options { - o(componentHandler) - } - return adaptor.HTTPHandler(componentHandler)(c) +func Render(c *fiber.Ctx, component templ.Component) error { + c.Set("Content-Type", "text/html") + return component.Render(c.Context(), c.Response().BodyWriter()) } From 5a68e7ad2ac7c53958e343506aefd6745496814a Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sun, 11 Aug 2024 13:58:40 -0700 Subject: [PATCH 2/2] chore: update generated --- examples/integration-gofiber/README.md | 3 +++ examples/integration-gofiber/home_templ.go | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/examples/integration-gofiber/README.md b/examples/integration-gofiber/README.md index 38a9337d6..12711398c 100644 --- a/examples/integration-gofiber/README.md +++ b/examples/integration-gofiber/README.md @@ -1,8 +1,11 @@ ## Example This example demonstrates the usage of templ with gofiber. + As soon as you start the server you can access http://localhost:3000/ and see the rendered page. + If you change the URL to http://localhost:3000/john you will see your parameter printed on the page. + This happens both through parameter passing into the templ component and through context using fiber locals. ## Tasks diff --git a/examples/integration-gofiber/home_templ.go b/examples/integration-gofiber/home_templ.go index 866872c31..c9545f3b5 100644 --- a/examples/integration-gofiber/home_templ.go +++ b/examples/integration-gofiber/home_templ.go @@ -1,6 +1,5 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.747 package main //lint:file-ignore SA4006 This context is only used if a nested component is present. @@ -44,7 +43,7 @@ func Home(name string) templ.Component { var templ_7745c5c3_Var2 string templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `home.templ`, Line: 15, Col: 18} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/integration-gofiber/home.templ`, Line: 15, Col: 18} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -57,7 +56,7 @@ func Home(name string) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(NameFromContext(ctx)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `home.templ`, Line: 16, Col: 34} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/integration-gofiber/home.templ`, Line: 16, Col: 34} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -96,3 +95,5 @@ func NotFound() templ.Component { return templ_7745c5c3_Err }) } + +var _ = templruntime.GeneratedTemplate