From 703a12bff91293df59e2e360029840f6e09251d3 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 13:42:18 +0100 Subject: [PATCH 01/15] feat: use buffered io for template rendering --- flush.go | 29 +++++++++++++++ generator/generator.go | 84 +++++++++++++++++++++++------------------- runtime.go | 4 +- runtime/buffer.go | 49 ++++++++++++++++++++++++ runtime/bufferpool.go | 38 +++++++++++++++++++ runtime/builder.go | 8 ++++ runtime/runtime.go | 12 ------ 7 files changed, 172 insertions(+), 52 deletions(-) create mode 100644 flush.go create mode 100644 runtime/buffer.go create mode 100644 runtime/bufferpool.go create mode 100644 runtime/builder.go diff --git a/flush.go b/flush.go new file mode 100644 index 000000000..8df8016b3 --- /dev/null +++ b/flush.go @@ -0,0 +1,29 @@ +package templ + +import ( + "context" + "fmt" + "io" +) + +func Flush() FlushComponent { + return FlushComponent{} +} + +type FlushComponent struct { +} + +type flusher interface { + Flush() error +} + +func (f FlushComponent) Render(ctx context.Context, w io.Writer) (err error) { + if err = GetChildren(ctx).Render(ctx, w); err != nil { + return err + } + b, isTemplBuffer := w.(flusher) + if !isTemplBuffer { + return fmt.Errorf("unable to flush, writer is not flushable") + } + return b.Flush() +} diff --git a/generator/generator.go b/generator/generator.go index c952c6f0f..c19990abc 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -221,11 +221,8 @@ func (g *generator) writeCSS(n parser.CSSTemplate) error { } { indentLevel++ - // templ_7745c5c3_CSSBuilder, _, release := templruntime.WriterToBuffer(nil) - if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil)\n"); err != nil { - return err - } - if _, err = g.w.WriteIndent(indentLevel, "defer templ_7745c5c3_release()\n"); err != nil { + // templ_7745c5c3_CSSBuilder := templruntim.GetBuilder() + if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_CSSBuilder := templruntime.GetBuilder()\n"); err != nil { return err } for i := 0; i < len(n.Properties); i++ { @@ -304,11 +301,52 @@ func (g *generator) writeGoExpression(n parser.TemplateFileGoExpression) (err er } func (g *generator) writeTemplBuffer(indentLevel int) (err error) { - // templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W)\n"); err != nil { + // templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)\n"); err != nil { return err } - if _, err = g.w.WriteIndent(indentLevel, "defer templ_7745c5c3_Release()\n"); err != nil { + // if !templ_7745c5c3_IsBuffer { + // defer func() { + // templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + // if templ_7745c5c3_Err == nil { + // templ_7745c5c3_Err = templ_7745c5c3_BufErr + // } + // }() + // } + if _, err = g.w.WriteIndent(indentLevel, "if !templ_7745c5c3_IsBuffer {\n"); err != nil { + return err + } + { + indentLevel++ + if _, err = g.w.WriteIndent(indentLevel, "defer func() {\n"); err != nil { + return err + } + { + indentLevel++ + if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)\n"); err != nil { + return err + } + if _, err = g.w.WriteIndent(indentLevel, "if templ_7745c5c3_Err == nil {\n"); err != nil { + return err + } + { + indentLevel++ + if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ_7745c5c3_BufErr\n"); err != nil { + return err + } + indentLevel-- + } + if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil { + return err + } + indentLevel-- + } + if _, err = g.w.WriteIndent(indentLevel, "}()\n"); err != nil { + return err + } + indentLevel-- + } + if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil { return err } return @@ -378,21 +416,6 @@ func (g *generator) writeTemplate(nodeIdx int, t parser.HTMLTemplate) error { if err = g.writeNodes(indentLevel, stripWhitespace(t.Children), nil); err != nil { return err } - // Return the buffer. - if _, err = g.w.WriteIndent(indentLevel, "if !templ_7745c5c3_IsBuffer {\n"); err != nil { - return err - } - { - indentLevel++ - // _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)\n"); err != nil { - return err - } - indentLevel-- - } - if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil { - return err - } // return templ_7745c5c3_Err if _, err = g.w.WriteIndent(indentLevel, "return templ_7745c5c3_Err\n"); err != nil { return err @@ -700,21 +723,6 @@ func (g *generator) writeBlockTemplElementExpression(indentLevel int, n parser.T if err = g.writeNodes(indentLevel, stripLeadingAndTrailingWhitespace(n.Children), nil); err != nil { return err } - // Return the buffer. - if _, err = g.w.WriteIndent(indentLevel, "if !templ_7745c5c3_IsBuffer {\n"); err != nil { - return err - } - { - indentLevel++ - // _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - if _, err = g.w.WriteIndent(indentLevel, "_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)\n"); err != nil { - return err - } - indentLevel-- - } - if _, err = g.w.WriteIndent(indentLevel, "}\n"); err != nil { - return err - } // return nil if _, err = g.w.WriteIndent(indentLevel, "return templ_7745c5c3_Err\n"); err != nil { return err diff --git a/runtime.go b/runtime.go index 9f6df504d..adcf515ac 100644 --- a/runtime.go +++ b/runtime.go @@ -837,7 +837,7 @@ func ToGoHTML(ctx context.Context, c Component) (s template.HTML, err error) { // WriteWatchModeString is used when rendering templates in development mode. // the generator would have written non-go code to the _templ.txt file, which // is then read by this function and written to the output. -func WriteWatchModeString(w *bytes.Buffer, lineNum int) error { +func WriteWatchModeString(w io.Writer, lineNum int) error { _, path, _, _ := runtime.Caller(1) if !strings.HasSuffix(path, "_templ.go") { return errors.New("templ: WriteWatchModeString can only be called from _templ.go") @@ -857,7 +857,7 @@ func WriteWatchModeString(w *bytes.Buffer, lineNum int) error { if err != nil { return err } - _, err = io.WriteString(io.Writer(w), unquoted) + _, err = io.WriteString(w, unquoted) return err } diff --git a/runtime/buffer.go b/runtime/buffer.go new file mode 100644 index 000000000..502e8a7cb --- /dev/null +++ b/runtime/buffer.go @@ -0,0 +1,49 @@ +package runtime + +import ( + "bufio" + "io" + "net/http" +) + +type Buffer struct { + Underlying io.Writer + b *bufio.Writer +} + +func (b *Buffer) Write(p []byte) (n int, err error) { + return b.b.Write(p) +} + +func (b *Buffer) Flush() error { + if err := b.b.Flush(); err != nil { + return err + } + if f, ok := b.Underlying.(http.Flusher); ok { + f.Flush() + } + return nil +} + +func (b *Buffer) Close() error { + if c, ok := b.Underlying.(io.Closer); ok { + return c.Close() + } + return nil +} + +func (b *Buffer) Reset(w io.Writer) { + if b.b == nil { + b.b = bufio.NewWriterSize(b, DefaultBufferSize) + } + b.Underlying = w + b.b.Reset(w) +} + +func (b *Buffer) Size() int { + return b.b.Size() +} + +func (b *Buffer) WriteString(s string) (n int, err error) { + return b.b.WriteString(s) +} diff --git a/runtime/bufferpool.go b/runtime/bufferpool.go new file mode 100644 index 000000000..74e7c8d3d --- /dev/null +++ b/runtime/bufferpool.go @@ -0,0 +1,38 @@ +package runtime + +import ( + "io" + "sync" +) + +var bufferPool = sync.Pool{ + New: func() any { + return new(Buffer) + }, +} + +func GetBuffer(w io.Writer) (b *Buffer, existing bool) { + b, ok := w.(*Buffer) + if ok { + return b, true + } + b = bufferPool.Get().(*Buffer) + b.Reset(w) + return b, false +} + +var DefaultBufferSize = 4 * 1024 // 4KB +var MaxBufferSize = 64 * 1024 // 64KB + +func ReleaseBuffer(w io.Writer) (err error) { + b, ok := w.(*Buffer) + if !ok { + return nil + } + err = b.Flush() + if b.Size() > MaxBufferSize { + b.Reset(nil) + } + bufferPool.Put(b) + return err +} diff --git a/runtime/builder.go b/runtime/builder.go new file mode 100644 index 000000000..0f4c9d4a3 --- /dev/null +++ b/runtime/builder.go @@ -0,0 +1,8 @@ +package runtime + +import "strings" + +// GetBuilder returns a strings.Builder. +func GetBuilder() (sb strings.Builder) { + return sb +} diff --git a/runtime/runtime.go b/runtime/runtime.go index 7fd2d24fd..fdfd917d2 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -1,24 +1,12 @@ package runtime import ( - "bytes" "context" "io" "github.com/a-h/templ" ) -func WriterToBuffer(w io.Writer) (buf *bytes.Buffer, ok bool, release func()) { - buf, ok = w.(*bytes.Buffer) - if !ok { - buf = templ.GetBuffer() - return buf, false, func() { - templ.ReleaseBuffer(buf) - } - } - return buf, true, func() {} -} - type GeneratedComponentInput struct { Context context.Context Writer io.Writer From 62d2d1a9b7843ab3270309c2550c0dee9a890062 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 13:42:34 +0100 Subject: [PATCH 02/15] chore: re-generate content --- benchmarks/templ/template_templ.go | 14 +- .../testwatch/testdata/templates_templ.go | 14 +- cmd/templ/lspcmd/httpdebug/list_templ.go | 14 +- .../testproject/testdata/templates_templ.go | 14 +- .../visualize/sourcemapvisualisation_templ.go | 37 +++-- examples/blog/posts_templ.go | 126 +++++++++----- .../templates_templ.go | 14 +- examples/counter-basic/components_templ.go | 42 +++-- .../counter/components/components_templ.go | 31 ++-- .../external-libraries/components_templ.go | 14 +- examples/hello-world-ssr/hello_templ.go | 14 +- examples/hello-world-static/hello_templ.go | 14 +- examples/integration-chi/home_templ.go | 14 +- examples/integration-echo/home_templ.go | 14 +- examples/integration-gin/home_templ.go | 14 +- .../components_templ.go | 14 +- examples/integration-gofiber/home_templ.go | 28 ++-- .../integration-react/components_templ.go | 28 ++-- .../internationalization/components_templ.go | 14 +- examples/static-generator/blog_templ.go | 56 ++++--- .../components/templsyntax_templ.go | 14 +- examples/typescript/components/index_templ.go | 14 +- generator/test-a-href/template_templ.go | 14 +- .../test-attribute-errors/template_templ.go | 14 +- .../test-attribute-escaping/template_templ.go | 14 +- generator/test-call/template_templ.go | 126 +++++++++----- .../test-complex-attributes/template_templ.go | 14 +- .../template_templ.go | 14 +- generator/test-context/template_templ.go | 14 +- .../test-css-expression/template_templ.go | 3 +- .../test-css-middleware/template_templ.go | 17 +- generator/test-css-usage/template_templ.go | 155 +++++++++++------- generator/test-doctype/template_templ.go | 14 +- .../test-element-attributes/template_templ.go | 20 ++- generator/test-elseif/template_templ.go | 14 +- generator/test-for/template_templ.go | 14 +- generator/test-form-action/template_templ.go | 14 +- generator/test-go-comments/template_templ.go | 14 +- .../template_templ.go | 14 +- generator/test-html-comment/template_templ.go | 28 ++-- generator/test-html/template_templ.go | 14 +- generator/test-if/template_templ.go | 14 +- generator/test-ifelse/template_templ.go | 14 +- generator/test-import/template_templ.go | 98 +++++++---- generator/test-method/template_templ.go | 14 +- generator/test-once/template_templ.go | 42 +++-- generator/test-raw-elements/template_templ.go | 14 +- .../test-script-inline/template_templ.go | 14 +- .../test-script-usage-nonce/template_templ.go | 42 +++-- generator/test-script-usage/template_templ.go | 56 ++++--- .../test-spread-attributes/template_templ.go | 14 +- .../test-string-errors/template_templ.go | 14 +- generator/test-string/template_templ.go | 14 +- generator/test-switch/template_templ.go | 14 +- .../test-switchdefault/template_templ.go | 14 +- .../test-templ-element/template_templ.go | 70 +++++--- .../template_templ.go | 14 +- .../test-text-whitespace/template_templ.go | 70 +++++--- generator/test-text/template_templ.go | 14 +- generator/test-void/template_templ.go | 14 +- .../template_templ.go | 70 +++++--- turbo/stream_templ.go | 28 ++-- 62 files changed, 1111 insertions(+), 636 deletions(-) diff --git a/benchmarks/templ/template_templ.go b/benchmarks/templ/template_templ.go index ad414f69c..1e587c8ca 100644 --- a/benchmarks/templ/template_templ.go +++ b/benchmarks/templ/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Render(p Person) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -100,9 +107,6 @@ func Render(p Person) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/cmd/templ/generatecmd/testwatch/testdata/templates_templ.go b/cmd/templ/generatecmd/testwatch/testdata/templates_templ.go index cad86b9b3..2a0bff2a1 100644 --- a/cmd/templ/generatecmd/testwatch/testdata/templates_templ.go +++ b/cmd/templ/generatecmd/testwatch/testdata/templates_templ.go @@ -12,8 +12,15 @@ import "fmt" func Page(count int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -37,9 +44,6 @@ func Page(count int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/cmd/templ/lspcmd/httpdebug/list_templ.go b/cmd/templ/lspcmd/httpdebug/list_templ.go index f684f5138..b93f1984f 100644 --- a/cmd/templ/lspcmd/httpdebug/list_templ.go +++ b/cmd/templ/lspcmd/httpdebug/list_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func list(uris []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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -81,9 +88,6 @@ func list(uris []string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/cmd/templ/testproject/testdata/templates_templ.go b/cmd/templ/testproject/testdata/templates_templ.go index d3f956ef0..340369a3c 100644 --- a/cmd/templ/testproject/testdata/templates_templ.go +++ b/cmd/templ/testproject/testdata/templates_templ.go @@ -12,8 +12,15 @@ import "fmt" func Page(count int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -37,9 +44,6 @@ func Page(count int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/cmd/templ/visualize/sourcemapvisualisation_templ.go b/cmd/templ/visualize/sourcemapvisualisation_templ.go index b38f52f84..eff0937ae 100644 --- a/cmd/templ/visualize/sourcemapvisualisation_templ.go +++ b/cmd/templ/visualize/sourcemapvisualisation_templ.go @@ -8,8 +8,7 @@ import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" func row() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`display:flex;`) templ_7745c5c3_CSSID := templ.CSSID(`row`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -19,8 +18,7 @@ func row() templ.CSSClass { } func column() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`flex:50%;`) templ_7745c5c3_CSSBuilder.WriteString(`overflow-y:scroll;`) templ_7745c5c3_CSSBuilder.WriteString(`max-height:100vh;`) @@ -32,8 +30,7 @@ func column() templ.CSSClass { } func code() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`font-family:monospace;`) templ_7745c5c3_CSSID := templ.CSSID(`code`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -45,8 +42,15 @@ func code() templ.CSSClass { func combine(templFileName string, left, right templ.Component) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -165,9 +169,6 @@ func combine(templFileName string, left, right templ.Component) templ.Component if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -209,8 +210,15 @@ func removeHighlight(sourceId, targetId string) templ.ComponentScript { func mappedCharacter(s string, sourceID, targetID 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var10 := templ.GetChildren(ctx) if templ_7745c5c3_Var10 == nil { @@ -274,9 +282,6 @@ func mappedCharacter(s string, sourceID, targetID string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/blog/posts_templ.go b/examples/blog/posts_templ.go index b451a3de1..ba4599830 100644 --- a/examples/blog/posts_templ.go +++ b/examples/blog/posts_templ.go @@ -15,8 +15,15 @@ import ( func headerTemplate(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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -40,9 +47,6 @@ func headerTemplate(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -50,8 +54,15 @@ func headerTemplate(name string) templ.Component { func footerTemplate() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -75,9 +86,6 @@ func footerTemplate() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -85,8 +93,15 @@ func footerTemplate() templ.Component { func navTemplate() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -97,9 +112,6 @@ func navTemplate() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -107,8 +119,15 @@ func navTemplate() templ.Component { func layout(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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var6 := templ.GetChildren(ctx) if templ_7745c5c3_Var6 == nil { @@ -160,9 +179,6 @@ func layout(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -170,8 +186,15 @@ func layout(name string) templ.Component { func postsTemplate(posts []Post) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var8 := templ.GetChildren(ctx) if templ_7745c5c3_Var8 == nil { @@ -218,9 +241,6 @@ func postsTemplate(posts []Post) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -228,8 +248,15 @@ func postsTemplate(posts []Post) templ.Component { func home() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var11 := templ.GetChildren(ctx) if templ_7745c5c3_Var11 == nil { @@ -238,25 +265,26 @@ func home() templ.Component { ctx = templ.ClearChildren(ctx) templ_7745c5c3_Var12 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Welcome to my website.
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = layout("Home").Render(templ.WithChildren(ctx, templ_7745c5c3_Var12), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -264,8 +292,15 @@ func home() templ.Component { func posts(posts []Post) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var13 := templ.GetChildren(ctx) if templ_7745c5c3_Var13 == nil { @@ -274,25 +309,26 @@ func posts(posts []Post) templ.Component { ctx = templ.ClearChildren(ctx) templ_7745c5c3_Var14 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Err = postsTemplate(posts).Render(ctx, templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = layout("Posts").Render(templ.WithChildren(ctx, templ_7745c5c3_Var14), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/content-security-policy/templates_templ.go b/examples/content-security-policy/templates_templ.go index 6d32326b2..d989389b5 100644 --- a/examples/content-security-policy/templates_templ.go +++ b/examples/content-security-policy/templates_templ.go @@ -20,8 +20,15 @@ func sayHello() templ.ComponentScript { func template() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -32,9 +39,6 @@ func template() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/counter-basic/components_templ.go b/examples/counter-basic/components_templ.go index 425fb8800..f8541f7ac 100644 --- a/examples/counter-basic/components_templ.go +++ b/examples/counter-basic/components_templ.go @@ -12,8 +12,15 @@ import "strconv" func counts(global, user int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -50,9 +57,6 @@ func counts(global, user int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -60,8 +64,15 @@ func counts(global, user int) templ.Component { func form() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var4 := templ.GetChildren(ctx) if templ_7745c5c3_Var4 == nil { @@ -72,9 +83,6 @@ func form() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -82,8 +90,15 @@ func form() templ.Component { func page(global, user int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -106,9 +121,6 @@ func page(global, user int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/counter/components/components_templ.go b/examples/counter/components/components_templ.go index 9ac4101ce..f015d4a66 100644 --- a/examples/counter/components/components_templ.go +++ b/examples/counter/components/components_templ.go @@ -10,8 +10,7 @@ import templruntime "github.com/a-h/templ/runtime" import "strconv" func border() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`border:1px solid #eeeeee;`) templ_7745c5c3_CSSBuilder.WriteString(`border-radius:4px;`) templ_7745c5c3_CSSBuilder.WriteString(`margin:10px;`) @@ -27,8 +26,15 @@ func border() templ.CSSClass { func counts(global, session int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -109,9 +115,6 @@ func counts(global, session int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -119,8 +122,15 @@ func counts(global, session int) templ.Component { func Page(global, session int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var8 := templ.GetChildren(ctx) if templ_7745c5c3_Var8 == nil { @@ -139,9 +149,6 @@ func Page(global, session int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/external-libraries/components_templ.go b/examples/external-libraries/components_templ.go index 77ca91503..8801e1d57 100644 --- a/examples/external-libraries/components_templ.go +++ b/examples/external-libraries/components_templ.go @@ -22,8 +22,15 @@ func graph(data []TimeValue) templ.ComponentScript { func page(data []TimeValue) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -51,9 +58,6 @@ func page(data []TimeValue) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/hello-world-ssr/hello_templ.go b/examples/hello-world-ssr/hello_templ.go index 03a3db613..9baa9dae3 100644 --- a/examples/hello-world-ssr/hello_templ.go +++ b/examples/hello-world-ssr/hello_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func hello(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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -35,9 +42,6 @@ func hello(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/hello-world-static/hello_templ.go b/examples/hello-world-static/hello_templ.go index 1b49a328c..ca907a350 100644 --- a/examples/hello-world-static/hello_templ.go +++ b/examples/hello-world-static/hello_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func hello(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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -35,9 +42,6 @@ func hello(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/integration-chi/home_templ.go b/examples/integration-chi/home_templ.go index 6b986df8e..5bfe5d108 100644 --- a/examples/integration-chi/home_templ.go +++ b/examples/integration-chi/home_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Home() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,9 +29,6 @@ func Home() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/integration-echo/home_templ.go b/examples/integration-echo/home_templ.go index 6b986df8e..5bfe5d108 100644 --- a/examples/integration-echo/home_templ.go +++ b/examples/integration-echo/home_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Home() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,9 +29,6 @@ func Home() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/integration-gin/home_templ.go b/examples/integration-gin/home_templ.go index b42272f93..2739db271 100644 --- a/examples/integration-gin/home_templ.go +++ b/examples/integration-gin/home_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Home() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,9 +29,6 @@ func Home() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/integration-go-echarts/components_templ.go b/examples/integration-go-echarts/components_templ.go index c2175aa0d..d05bad528 100644 --- a/examples/integration-go-echarts/components_templ.go +++ b/examples/integration-go-echarts/components_templ.go @@ -12,8 +12,15 @@ import "github.com/go-echarts/go-echarts/v2/charts" func Home(chart *charts.Bar) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -32,9 +39,6 @@ func Home(chart *charts.Bar) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/integration-gofiber/home_templ.go b/examples/integration-gofiber/home_templ.go index be5a2b3f7..34c241f07 100644 --- a/examples/integration-gofiber/home_templ.go +++ b/examples/integration-gofiber/home_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -35,9 +42,6 @@ func Home(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -45,8 +49,15 @@ func Home(name string) templ.Component { func NotFound() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -57,9 +68,6 @@ func NotFound() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/integration-react/components_templ.go b/examples/integration-react/components_templ.go index 385fefc51..7f72925f5 100644 --- a/examples/integration-react/components_templ.go +++ b/examples/integration-react/components_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Hello(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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -35,9 +42,6 @@ func Hello(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -45,8 +49,15 @@ func Hello(name string) templ.Component { func page() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -67,9 +78,6 @@ func page() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/internationalization/components_templ.go b/examples/internationalization/components_templ.go index 21c5cb91c..e45ec0c24 100644 --- a/examples/internationalization/components_templ.go +++ b/examples/internationalization/components_templ.go @@ -12,8 +12,15 @@ import "github.com/invopop/ctxi18n/i18n" func page() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -63,9 +70,6 @@ func page() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/static-generator/blog_templ.go b/examples/static-generator/blog_templ.go index 0415b75e1..5710d86da 100644 --- a/examples/static-generator/blog_templ.go +++ b/examples/static-generator/blog_templ.go @@ -15,8 +15,15 @@ import ( func headerComponent(title 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -40,9 +47,6 @@ func headerComponent(title string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -50,8 +54,15 @@ func headerComponent(title string) templ.Component { func contentComponent(title string, body templ.Component) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -83,9 +94,6 @@ func contentComponent(title string, body templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -93,8 +101,15 @@ func contentComponent(title string, body templ.Component) templ.Component { func contentPage(title string, body templ.Component) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -117,9 +132,6 @@ func contentPage(title string, body templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -127,8 +139,15 @@ func contentPage(title string, body templ.Component) templ.Component { func indexPage(posts []Post) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var6 := templ.GetChildren(ctx) if templ_7745c5c3_Var6 == nil { @@ -179,9 +198,6 @@ func indexPage(posts []Post) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/syntax-and-usage/components/templsyntax_templ.go b/examples/syntax-and-usage/components/templsyntax_templ.go index 620d3ecec..60edfa6fa 100644 --- a/examples/syntax-and-usage/components/templsyntax_templ.go +++ b/examples/syntax-and-usage/components/templsyntax_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func list(items []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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -45,9 +52,6 @@ func list(items []string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/examples/typescript/components/index_templ.go b/examples/typescript/components/index_templ.go index aea5937c1..2e2668a08 100644 --- a/examples/typescript/components/index_templ.go +++ b/examples/typescript/components/index_templ.go @@ -14,8 +14,15 @@ type Data struct { func Page(attributeData Data, scriptData Data) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -47,9 +54,6 @@ func Page(attributeData Data, scriptData Data) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-a-href/template_templ.go b/generator/test-a-href/template_templ.go index 4d3567fbc..b6a389fed 100644 --- a/generator/test-a-href/template_templ.go +++ b/generator/test-a-href/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -40,9 +47,6 @@ func render() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-attribute-errors/template_templ.go b/generator/test-attribute-errors/template_templ.go index a4d356fec..e3fb24c01 100644 --- a/generator/test-attribute-errors/template_templ.go +++ b/generator/test-attribute-errors/template_templ.go @@ -21,8 +21,15 @@ func funcWithError(in error) (s string, err error) { func TestComponent(err error) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -72,9 +79,6 @@ func TestComponent(err error) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-attribute-escaping/template_templ.go b/generator/test-attribute-escaping/template_templ.go index 470ac7509..f3e3e7bdc 100644 --- a/generator/test-attribute-escaping/template_templ.go +++ b/generator/test-attribute-escaping/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func BasicTemplate(url 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -31,9 +38,6 @@ func BasicTemplate(url string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-call/template_templ.go b/generator/test-call/template_templ.go index b45a79316..d9d4650d7 100644 --- a/generator/test-call/template_templ.go +++ b/generator/test-call/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func showAll() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -36,25 +43,26 @@ func showAll() templ.Component { } templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Child content
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = wrapChildren().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -62,8 +70,15 @@ func showAll() templ.Component { func a() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -74,9 +89,6 @@ func a() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -84,8 +96,15 @@ func a() templ.Component { func b(child templ.Component) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var4 := templ.GetChildren(ctx) if templ_7745c5c3_Var4 == nil { @@ -100,9 +119,6 @@ func b(child templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -110,8 +126,15 @@ func b(child templ.Component) templ.Component { func c(text 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -135,9 +158,6 @@ func c(text string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -145,8 +165,15 @@ func c(text string) templ.Component { func d() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var7 := templ.GetChildren(ctx) if templ_7745c5c3_Var7 == nil { @@ -157,9 +184,6 @@ func d() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -167,8 +191,15 @@ func d() templ.Component { func e() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var8 := templ.GetChildren(ctx) if templ_7745c5c3_Var8 == nil { @@ -179,9 +210,6 @@ func e() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -189,8 +217,15 @@ func e() templ.Component { func showOne(component templ.Component) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var9 := templ.GetChildren(ctx) if templ_7745c5c3_Var9 == nil { @@ -209,9 +244,6 @@ func showOne(component templ.Component) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -219,8 +251,15 @@ func showOne(component templ.Component) templ.Component { func wrapChildren() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var10 := templ.GetChildren(ctx) if templ_7745c5c3_Var10 == nil { @@ -239,9 +278,6 @@ func wrapChildren() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-complex-attributes/template_templ.go b/generator/test-complex-attributes/template_templ.go index 9b2dcff86..24096872e 100644 --- a/generator/test-complex-attributes/template_templ.go +++ b/generator/test-complex-attributes/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func ComplexAttributes() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,9 +29,6 @@ func ComplexAttributes() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-constant-attribute-escaping/template_templ.go b/generator/test-constant-attribute-escaping/template_templ.go index 57900e0aa..557c1a172 100644 --- a/generator/test-constant-attribute-escaping/template_templ.go +++ b/generator/test-constant-attribute-escaping/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func BasicTemplate() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,9 +29,6 @@ func BasicTemplate() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-context/template_templ.go b/generator/test-context/template_templ.go index bfe0aea7e..5910ef928 100644 --- a/generator/test-context/template_templ.go +++ b/generator/test-context/template_templ.go @@ -14,8 +14,15 @@ var contextKeyName contextKey = "name" func render() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -60,9 +67,6 @@ func render() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-css-expression/template_templ.go b/generator/test-css-expression/template_templ.go index 9b99087a4..ff55362ce 100644 --- a/generator/test-css-expression/template_templ.go +++ b/generator/test-css-expression/template_templ.go @@ -8,8 +8,7 @@ import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" func className() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`background-color:#ffffff;`) templ_7745c5c3_CSSBuilder.WriteString(`max-height:calc(100vh - 170px);`) templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`color`, red))) diff --git a/generator/test-css-middleware/template_templ.go b/generator/test-css-middleware/template_templ.go index 3f228ce5d..6e4d9ff93 100644 --- a/generator/test-css-middleware/template_templ.go +++ b/generator/test-css-middleware/template_templ.go @@ -8,8 +8,7 @@ import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" func red() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`color:red;`) templ_7745c5c3_CSSID := templ.CSSID(`red`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -21,8 +20,15 @@ func red() templ.CSSClass { func render(s 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -64,9 +70,6 @@ func render(s string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-css-usage/template_templ.go b/generator/test-css-usage/template_templ.go index 2ec3f3b88..75e38012c 100644 --- a/generator/test-css-usage/template_templ.go +++ b/generator/test-css-usage/template_templ.go @@ -15,8 +15,15 @@ import ( func StyleTagsAreSupported() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -27,9 +34,6 @@ func StyleTagsAreSupported() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -39,8 +43,7 @@ func StyleTagsAreSupported() templ.Component { const red = "#00ff00" func cssComponentGreen() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`color`, red))) templ_7745c5c3_CSSID := templ.CSSID(`cssComponentGreen`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -52,8 +55,15 @@ func cssComponentGreen() templ.CSSClass { func CSSComponentsAreSupported() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var2 := templ.GetChildren(ctx) if templ_7745c5c3_Var2 == nil { @@ -82,9 +92,6 @@ func CSSComponentsAreSupported() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -94,8 +101,15 @@ func CSSComponentsAreSupported() templ.Component { func CSSComponentsAndConstantsAreSupported() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -146,9 +160,6 @@ func CSSComponentsAndConstantsAreSupported() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -157,8 +168,15 @@ func CSSComponentsAndConstantsAreSupported() templ.Component { func MapsCanBeUsedToConditionallySetClasses() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var10 := templ.GetChildren(ctx) if templ_7745c5c3_Var10 == nil { @@ -187,17 +205,13 @@ func MapsCanBeUsedToConditionallySetClasses() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } // The templ.KV function can be used to add a class if a condition is true. func d() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`font-size:12pt;`) templ_7745c5c3_CSSID := templ.CSSID(`d`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -207,8 +221,7 @@ func d() templ.CSSClass { } func e() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`font-size:14pt;`) templ_7745c5c3_CSSID := templ.CSSID(`e`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -220,8 +233,15 @@ func e() templ.CSSClass { func KVCanBeUsedToConditionallySetClasses() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var13 := templ.GetChildren(ctx) if templ_7745c5c3_Var13 == nil { @@ -250,9 +270,6 @@ func KVCanBeUsedToConditionallySetClasses() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -261,8 +278,15 @@ func KVCanBeUsedToConditionallySetClasses() templ.Component { func PsuedoAttributesAndComplexClassNamesAreSupported() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var16 := templ.GetChildren(ctx) if templ_7745c5c3_Var16 == nil { @@ -291,9 +315,6 @@ func PsuedoAttributesAndComplexClassNamesAreSupported() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -302,8 +323,15 @@ func PsuedoAttributesAndComplexClassNamesAreSupported() templ.Component { func ClassNamesAreHTMLEscaped() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var19 := templ.GetChildren(ctx) if templ_7745c5c3_Var19 == nil { @@ -332,17 +360,13 @@ func ClassNamesAreHTMLEscaped() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } // CSS components can be used with arguments. func loading(percent int) templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`width`, fmt.Sprintf("%d%%", percent)))) templ_7745c5c3_CSSID := templ.CSSID(`loading`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -354,8 +378,15 @@ func loading(percent int) templ.CSSClass { func CSSComponentsCanBeUsedWithArguments() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var22 := templ.GetChildren(ctx) if templ_7745c5c3_Var22 == nil { @@ -406,16 +437,12 @@ func CSSComponentsCanBeUsedWithArguments() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } func windVaneRotation(degrees float64) templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(string(templ.SanitizeCSS(`transform`, templ.SafeCSSProperty(fmt.Sprintf("rotate(%ddeg)", int(math.Round(degrees))))))) templ_7745c5c3_CSSID := templ.CSSID(`windVaneRotation`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -427,8 +454,15 @@ func windVaneRotation(degrees float64) templ.CSSClass { func Rotate(degrees float64) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var27 := templ.GetChildren(ctx) if templ_7745c5c3_Var27 == nil { @@ -457,9 +491,6 @@ func Rotate(degrees float64) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -468,8 +499,15 @@ func Rotate(degrees float64) templ.Component { func TestComponent() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var30 := templ.GetChildren(ctx) if templ_7745c5c3_Var30 == nil { @@ -512,9 +550,6 @@ func TestComponent() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-doctype/template_templ.go b/generator/test-doctype/template_templ.go index 2afbc6ef6..49b73478a 100644 --- a/generator/test-doctype/template_templ.go +++ b/generator/test-doctype/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Layout(title, content 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -48,9 +55,6 @@ func Layout(title, content string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-element-attributes/template_templ.go b/generator/test-element-attributes/template_templ.go index b43aaf8bc..3c17f587f 100644 --- a/generator/test-element-attributes/template_templ.go +++ b/generator/test-element-attributes/template_templ.go @@ -8,8 +8,7 @@ import "github.com/a-h/templ" import templruntime "github.com/a-h/templ/runtime" func important() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`width:100;`) templ_7745c5c3_CSSID := templ.CSSID(`important`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -19,8 +18,7 @@ func important() templ.CSSClass { } func unimportant() templ.CSSClass { - templ_7745c5c3_CSSBuilder, _, templ_7745c5c3_release := templruntime.WriterToBuffer(nil) - defer templ_7745c5c3_release() + templ_7745c5c3_CSSBuilder := templruntime.GetBuilder() templ_7745c5c3_CSSBuilder.WriteString(`width:50;`) templ_7745c5c3_CSSID := templ.CSSID(`unimportant`, templ_7745c5c3_CSSBuilder.String()) return templ.ComponentCSSClass{ @@ -32,8 +30,15 @@ func unimportant() templ.CSSClass { func render(p person) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -159,9 +164,6 @@ func render(p person) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-elseif/template_templ.go b/generator/test-elseif/template_templ.go index 6aad436b2..84f7c645d 100644 --- a/generator/test-elseif/template_templ.go +++ b/generator/test-elseif/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(d data) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -127,9 +134,6 @@ func render(d data) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-for/template_templ.go b/generator/test-for/template_templ.go index 383925aec..23e31d375 100644 --- a/generator/test-for/template_templ.go +++ b/generator/test-for/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(items []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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -37,9 +44,6 @@ func render(items []string) templ.Component { return templ_7745c5c3_Err } } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-form-action/template_templ.go b/generator/test-form-action/template_templ.go index a1207c419..2359eeec2 100644 --- a/generator/test-form-action/template_templ.go +++ b/generator/test-form-action/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -40,9 +47,6 @@ func render() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-go-comments/template_templ.go b/generator/test-go-comments/template_templ.go index 0f777aeef..02862dfb8 100644 --- a/generator/test-go-comments/template_templ.go +++ b/generator/test-go-comments/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(content 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -35,9 +42,6 @@ func render(content string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-go-template-in-templ/template_templ.go b/generator/test-go-template-in-templ/template_templ.go index 8e050078a..9b6b637db 100644 --- a/generator/test-go-template-in-templ/template_templ.go +++ b/generator/test-go-template-in-templ/template_templ.go @@ -14,8 +14,15 @@ var goTemplate = template.Must(template.New("example").Parse("
{{ . }}
func Example() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -34,9 +41,6 @@ func Example() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-html-comment/template_templ.go b/generator/test-html-comment/template_templ.go index 532a21c44..0178afc30 100644 --- a/generator/test-html-comment/template_templ.go +++ b/generator/test-html-comment/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(content 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -59,9 +66,6 @@ func render(content string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -69,8 +73,15 @@ func render(content string) templ.Component { func paragraph(content 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -94,9 +105,6 @@ func paragraph(content string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-html/template_templ.go b/generator/test-html/template_templ.go index 87038f59d..674afc80e 100644 --- a/generator/test-html/template_templ.go +++ b/generator/test-html/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(p person) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -100,9 +107,6 @@ func render(p person) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-if/template_templ.go b/generator/test-if/template_templ.go index 36bcc55ba..45f70c17a 100644 --- a/generator/test-if/template_templ.go +++ b/generator/test-if/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(d data) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -39,9 +46,6 @@ func render(d data) templ.Component { return templ_7745c5c3_Err } } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-ifelse/template_templ.go b/generator/test-ifelse/template_templ.go index 9d4ac893d..0eb1ccea4 100644 --- a/generator/test-ifelse/template_templ.go +++ b/generator/test-ifelse/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(d data) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -39,9 +46,6 @@ func render(d data) templ.Component { return templ_7745c5c3_Err } } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-import/template_templ.go b/generator/test-import/template_templ.go index a26fa2463..de4d777a1 100644 --- a/generator/test-import/template_templ.go +++ b/generator/test-import/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func listItem() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -30,9 +37,6 @@ func listItem() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -40,8 +44,15 @@ func listItem() templ.Component { func list() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var2 := templ.GetChildren(ctx) if templ_7745c5c3_Var2 == nil { @@ -60,9 +71,6 @@ func list() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -70,8 +78,15 @@ func list() templ.Component { func main() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -80,21 +95,32 @@ func main() templ.Component { ctx = templ.ClearChildren(ctx) templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Item 1") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = listItem().Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer) @@ -107,16 +133,20 @@ func main() templ.Component { } templ_7745c5c3_Var6 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Item 2") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = listItem().Render(templ.WithChildren(ctx, templ_7745c5c3_Var6), templ_7745c5c3_Buffer) @@ -129,34 +159,32 @@ func main() templ.Component { } templ_7745c5c3_Var7 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Item 3") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = listItem().Render(templ.WithChildren(ctx, templ_7745c5c3_Var7), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = list().Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-method/template_templ.go b/generator/test-method/template_templ.go index e54f8cc48..dc8d33753 100644 --- a/generator/test-method/template_templ.go +++ b/generator/test-method/template_templ.go @@ -14,8 +14,15 @@ type Data struct { func (d Data) Method() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -39,9 +46,6 @@ func (d Data) Method() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-once/template_templ.go b/generator/test-once/template_templ.go index c7d8b1d49..6def9f5d2 100644 --- a/generator/test-once/template_templ.go +++ b/generator/test-once/template_templ.go @@ -12,8 +12,15 @@ var helloHandle = templ.NewOnceHandle() func hello(label, 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,16 +29,20 @@ func hello(label, name string) templ.Component { ctx = templ.ClearChildren(ctx) templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = helloHandle.Once().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) @@ -68,9 +79,6 @@ func hello(label, name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -78,8 +86,15 @@ func hello(label, name string) templ.Component { func render() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -94,9 +109,6 @@ func render() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-raw-elements/template_templ.go b/generator/test-raw-elements/template_templ.go index 3031f4873..8a2309aff 100644 --- a/generator/test-raw-elements/template_templ.go +++ b/generator/test-raw-elements/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func Example() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -30,9 +37,6 @@ func Example() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-script-inline/template_templ.go b/generator/test-script-inline/template_templ.go index 236f1873e..9f129c924 100644 --- a/generator/test-script-inline/template_templ.go +++ b/generator/test-script-inline/template_templ.go @@ -30,8 +30,15 @@ func withoutParameters() templ.ComponentScript { func InlineJavascript(a 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -54,9 +61,6 @@ func InlineJavascript(a string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-script-usage-nonce/template_templ.go b/generator/test-script-usage-nonce/template_templ.go index 06b80791d..f1c92ec16 100644 --- a/generator/test-script-usage-nonce/template_templ.go +++ b/generator/test-script-usage-nonce/template_templ.go @@ -40,8 +40,15 @@ func onClick() templ.ComponentScript { func Button(text 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -87,9 +94,6 @@ func Button(text string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -107,8 +111,15 @@ func withComment() templ.ComponentScript { func ThreeButtons() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -148,9 +159,6 @@ func ThreeButtons() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -168,8 +176,15 @@ func conditionalScript() templ.ComponentScript { func Conditional(show bool) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var7 := templ.GetChildren(ctx) if templ_7745c5c3_Var7 == nil { @@ -203,9 +218,6 @@ func Conditional(show bool) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-script-usage/template_templ.go b/generator/test-script-usage/template_templ.go index ec0e995f6..13de67016 100644 --- a/generator/test-script-usage/template_templ.go +++ b/generator/test-script-usage/template_templ.go @@ -40,8 +40,15 @@ func onClick() templ.ComponentScript { func Button(text 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -87,9 +94,6 @@ func Button(text string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -107,8 +111,15 @@ func withComment() templ.ComponentScript { func ThreeButtons() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -152,9 +163,6 @@ func ThreeButtons() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -172,8 +180,15 @@ func conditionalScript() templ.ComponentScript { func Conditional(show bool) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var7 := templ.GetChildren(ctx) if templ_7745c5c3_Var7 == nil { @@ -207,9 +222,6 @@ func Conditional(show bool) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -227,8 +239,15 @@ func alertTest() templ.ComponentScript { func ScriptOnLoad() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var9 := templ.GetChildren(ctx) if templ_7745c5c3_Var9 == nil { @@ -252,9 +271,6 @@ func ScriptOnLoad() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-spread-attributes/template_templ.go b/generator/test-spread-attributes/template_templ.go index a6607e2e7..1c77e82c1 100644 --- a/generator/test-spread-attributes/template_templ.go +++ b/generator/test-spread-attributes/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func BasicTemplate(spread templ.Attributes) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -50,9 +57,6 @@ func BasicTemplate(spread templ.Attributes) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-string-errors/template_templ.go b/generator/test-string-errors/template_templ.go index c3aacc83f..2ad48fc6a 100644 --- a/generator/test-string-errors/template_templ.go +++ b/generator/test-string-errors/template_templ.go @@ -21,8 +21,15 @@ func funcWithError(in error) (s string, err error) { func TestComponent(err error) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -72,9 +79,6 @@ func TestComponent(err error) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-string/template_templ.go b/generator/test-string/template_templ.go index 69d604e5e..067cf22b8 100644 --- a/generator/test-string/template_templ.go +++ b/generator/test-string/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(s 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -74,9 +81,6 @@ func render(s string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-switch/template_templ.go b/generator/test-switch/template_templ.go index 82c6a2015..56c06ba46 100644 --- a/generator/test-switch/template_templ.go +++ b/generator/test-switch/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render(input 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -40,9 +47,6 @@ func render(input string) templ.Component { return templ_7745c5c3_Err } } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-switchdefault/template_templ.go b/generator/test-switchdefault/template_templ.go index ad48caa7a..176525314 100644 --- a/generator/test-switchdefault/template_templ.go +++ b/generator/test-switchdefault/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func template(input 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -40,9 +47,6 @@ func template(input string) templ.Component { return templ_7745c5c3_Err } } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-templ-element/template_templ.go b/generator/test-templ-element/template_templ.go index 355f1d503..66cae64f5 100644 --- a/generator/test-templ-element/template_templ.go +++ b/generator/test-templ-element/template_templ.go @@ -12,8 +12,15 @@ import "fmt" func wrapper(index int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -45,9 +52,6 @@ func wrapper(index int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -55,8 +59,15 @@ func wrapper(index int) templ.Component { func template() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -65,8 +76,15 @@ func template() templ.Component { ctx = templ.ClearChildren(ctx) templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("child1") if templ_7745c5c3_Err != nil { @@ -74,8 +92,15 @@ func template() templ.Component { } templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("child2") if templ_7745c5c3_Err != nil { @@ -83,8 +108,15 @@ func template() templ.Component { } templ_7745c5c3_Var6 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("child3") if templ_7745c5c3_Err != nil { @@ -94,36 +126,24 @@ func template() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = wrapper(3).Render(templ.WithChildren(ctx, templ_7745c5c3_Var6), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = wrapper(2).Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) templ_7745c5c3_Err = wrapper(1).Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-templ-in-go-template/template_templ.go b/generator/test-templ-in-go-template/template_templ.go index 60849c638..6544abcd7 100644 --- a/generator/test-templ-in-go-template/template_templ.go +++ b/generator/test-templ-in-go-template/template_templ.go @@ -20,8 +20,15 @@ var example = template.Must(template.New("example").Parse(` func greeting() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -32,9 +39,6 @@ func greeting() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-text-whitespace/template_templ.go b/generator/test-text-whitespace/template_templ.go index 476dbc83a..3330aafce 100644 --- a/generator/test-text-whitespace/template_templ.go +++ b/generator/test-text-whitespace/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func WhitespaceIsAddedWithinTemplStatements() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -32,9 +39,6 @@ func WhitespaceIsAddedWithinTemplStatements() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -44,8 +48,15 @@ const WhitespaceIsAddedWithinTemplStatementsExpected = `

This is some text. So func InlineElementsAreNotPadded() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var2 := templ.GetChildren(ctx) if templ_7745c5c3_Var2 == nil { @@ -56,9 +67,6 @@ func InlineElementsAreNotPadded() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -68,8 +76,15 @@ const InlineElementsAreNotPaddedExpected = `

Inline text is spaced properly func WhiteSpaceInHTMLIsNormalised() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var3 := templ.GetChildren(ctx) if templ_7745c5c3_Var3 == nil { @@ -80,9 +95,6 @@ func WhiteSpaceInHTMLIsNormalised() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -92,8 +104,15 @@ const WhiteSpaceInHTMLIsNormalisedExpected = `

newlines and other whitespace a func WhiteSpaceAroundValues() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var4 := templ.GetChildren(ctx) if templ_7745c5c3_Var4 == nil { @@ -117,9 +136,6 @@ func WhiteSpaceAroundValues() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -131,8 +147,15 @@ const WhiteSpaceAroundTemplatedValuesExpected = `

templ allows whitespace ar func WhiteSpaceAroundTemplatedValues(prefix, statement 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var6 := templ.GetChildren(ctx) if templ_7745c5c3_Var6 == nil { @@ -169,9 +192,6 @@ func WhiteSpaceAroundTemplatedValues(prefix, statement string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-text/template_templ.go b/generator/test-text/template_templ.go index 4a9a9e216..f13270a5b 100644 --- a/generator/test-text/template_templ.go +++ b/generator/test-text/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func BasicTemplate(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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -48,9 +55,6 @@ func BasicTemplate(name string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-void/template_templ.go b/generator/test-void/template_templ.go index b3a0b909c..3de0226ec 100644 --- a/generator/test-void/template_templ.go +++ b/generator/test-void/template_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func render() 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -22,9 +29,6 @@ func render() templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/generator/test-whitespace-around-go-keywords/template_templ.go b/generator/test-whitespace-around-go-keywords/template_templ.go index c26d2abcf..40f05b9a8 100644 --- a/generator/test-whitespace-around-go-keywords/template_templ.go +++ b/generator/test-whitespace-around-go-keywords/template_templ.go @@ -12,8 +12,15 @@ import "fmt" func WhitespaceIsConsistentInIf(firstIf, secondIf bool) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -44,9 +51,6 @@ func WhitespaceIsConsistentInIf(firstIf, secondIf bool) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -58,8 +62,15 @@ const WhitespaceIsConsistentInTrueElseExpected = ` Start func WhitespaceIsConsistentInFor(i int) 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var5 := templ.GetChildren(ctx) if templ_7745c5c3_Var5 == nil { @@ -203,9 +226,6 @@ func WhitespaceIsConsistentInFor(i int) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } diff --git a/turbo/stream_templ.go b/turbo/stream_templ.go index 1ceff03b0..1d12676e0 100644 --- a/turbo/stream_templ.go +++ b/turbo/stream_templ.go @@ -10,8 +10,15 @@ import templruntime "github.com/a-h/templ/runtime" func actionTemplate(action string, target 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var1 := templ.GetChildren(ctx) if templ_7745c5c3_Var1 == nil { @@ -56,9 +63,6 @@ func actionTemplate(action string, target string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } @@ -66,8 +70,15 @@ func actionTemplate(action string, target string) templ.Component { func removeTemplate(action string, target 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 - templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer, templ_7745c5c3_Release := templruntime.WriterToBuffer(templ_7745c5c3_W) - defer templ_7745c5c3_Release() + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } ctx = templ.InitializeContext(ctx) templ_7745c5c3_Var4 := templ.GetChildren(ctx) if templ_7745c5c3_Var4 == nil { @@ -104,9 +115,6 @@ func removeTemplate(action string, target string) templ.Component { if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - if !templ_7745c5c3_IsBuffer { - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W) - } return templ_7745c5c3_Err }) } From 2eb05e7218089afff41218a05565c0f8f90719db Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 14:59:48 +0100 Subject: [PATCH 03/15] chore: add tests --- .version | 2 +- runtime/buffer.go | 13 +++++++ runtime/buffer_test.go | 79 ++++++++++++++++++++++++++++++++++++++ runtime/bufferpool.go | 12 +++--- runtime/bufferpool_test.go | 59 ++++++++++++++++++++++++++++ runtime/builder_test.go | 11 ++++++ runtime/runtime.go | 2 + runtime/runtime_test.go | 22 +++++++++++ 8 files changed, 193 insertions(+), 7 deletions(-) create mode 100644 runtime/buffer_test.go create mode 100644 runtime/bufferpool_test.go create mode 100644 runtime/builder_test.go create mode 100644 runtime/runtime_test.go diff --git a/.version b/.version index 4b175c0af..d1bfc105e 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.726 \ No newline at end of file +0.2.727 \ No newline at end of file diff --git a/runtime/buffer.go b/runtime/buffer.go index 502e8a7cb..63e4acd8c 100644 --- a/runtime/buffer.go +++ b/runtime/buffer.go @@ -6,15 +6,24 @@ import ( "net/http" ) +// DefaultBufferSize is the default size of buffers. It is set to 4KB by default, which is the +// same as the default buffer size of bufio.Writer. +var DefaultBufferSize = 4 * 1024 // 4KB + +// Buffer is a wrapper around bufio.Writer that enables flushing and closing of +// the underlying writer. type Buffer struct { Underlying io.Writer b *bufio.Writer } +// Write the contents of p into the buffer. func (b *Buffer) Write(p []byte) (n int, err error) { return b.b.Write(p) } +// Flush writes any buffered data to the underlying io.Writer and +// calls the Flush method of the underlying http.Flusher if it implements it. func (b *Buffer) Flush() error { if err := b.b.Flush(); err != nil { return err @@ -25,6 +34,7 @@ func (b *Buffer) Flush() error { return nil } +// Close closes the buffer and the underlying io.Writer if it implements io.Closer. func (b *Buffer) Close() error { if c, ok := b.Underlying.(io.Closer); ok { return c.Close() @@ -32,6 +42,7 @@ func (b *Buffer) Close() error { return nil } +// Reset sets the underlying io.Writer to w and resets the buffer. func (b *Buffer) Reset(w io.Writer) { if b.b == nil { b.b = bufio.NewWriterSize(b, DefaultBufferSize) @@ -40,10 +51,12 @@ func (b *Buffer) Reset(w io.Writer) { b.b.Reset(w) } +// Size returns the size of the underlying buffer in bytes. func (b *Buffer) Size() int { return b.b.Size() } +// WriteString writes the contents of s into the buffer. func (b *Buffer) WriteString(s string) (n int, err error) { return b.b.WriteString(s) } diff --git a/runtime/buffer_test.go b/runtime/buffer_test.go new file mode 100644 index 000000000..920f659f2 --- /dev/null +++ b/runtime/buffer_test.go @@ -0,0 +1,79 @@ +package runtime + +import ( + "errors" + "net/http/httptest" + "testing" +) + +var wasClosed bool + +type closable struct { + *httptest.ResponseRecorder +} + +func (c *closable) Close() error { + wasClosed = true + return nil +} + +func TestBuffer(t *testing.T) { + underlying := httptest.NewRecorder() + w, _ := GetBuffer(&closable{underlying}) + t.Run("can write to a buffer", func(t *testing.T) { + if _, err := w.Write([]byte("A")); err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + t.Run("can write a string to a buffer", func(t *testing.T) { + if _, err := w.WriteString("A"); err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + t.Run("can flush a buffer", func(t *testing.T) { + if err := w.Flush(); err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + t.Run("can close a buffer", func(t *testing.T) { + if err := w.Close(); err != nil { + t.Errorf("unexpected error: %v", err) + } + if !wasClosed { + t.Error("expected the underlying writer to be closed") + } + }) + t.Run("can get the size of a buffer", func(t *testing.T) { + if w.Size() != DefaultBufferSize { + t.Errorf("expected %d, got %d", DefaultBufferSize, w.Size()) + } + }) + t.Run("can reset a buffer", func(t *testing.T) { + w.Reset(underlying) + }) + if underlying.Body.String() != "AA" { + t.Errorf("expected %q, got %q", "AA", underlying.Body.String()) + } +} + +type failStream struct { +} + +var errTest = errors.New("test error") + +func (f *failStream) Write(p []byte) (n int, err error) { + return 0, errTest +} + +func (f *failStream) Close() error { + return errTest +} + +func TestBufferErrors(t *testing.T) { + w, _ := GetBuffer(&failStream{}) + t.Run("close errors are returned", func(t *testing.T) { + if err := w.Close(); err != errTest { + t.Errorf("expected %v, got %v", errTest, err) + } + }) +} diff --git a/runtime/bufferpool.go b/runtime/bufferpool.go index 74e7c8d3d..ca2a13196 100644 --- a/runtime/bufferpool.go +++ b/runtime/bufferpool.go @@ -11,7 +11,12 @@ var bufferPool = sync.Pool{ }, } +// GetBuffer creates and returns a new buffer if the writer is not already a buffer, +// or returns the existing buffer if it is. func GetBuffer(w io.Writer) (b *Buffer, existing bool) { + if w == nil { + return nil, false + } b, ok := w.(*Buffer) if ok { return b, true @@ -21,18 +26,13 @@ func GetBuffer(w io.Writer) (b *Buffer, existing bool) { return b, false } -var DefaultBufferSize = 4 * 1024 // 4KB -var MaxBufferSize = 64 * 1024 // 64KB - +// ReleaseBuffer flushes the buffer and returns it to the pool. func ReleaseBuffer(w io.Writer) (err error) { b, ok := w.(*Buffer) if !ok { return nil } err = b.Flush() - if b.Size() > MaxBufferSize { - b.Reset(nil) - } bufferPool.Put(b) return err } diff --git a/runtime/bufferpool_test.go b/runtime/bufferpool_test.go new file mode 100644 index 000000000..1724825e5 --- /dev/null +++ b/runtime/bufferpool_test.go @@ -0,0 +1,59 @@ +package runtime + +import ( + "bytes" + "testing" +) + +func TestBufferPool(t *testing.T) { + t.Run("can get a buffer from the pool", func(t *testing.T) { + w, existing := GetBuffer(new(bytes.Buffer)) + if w == nil { + t.Error("expected a buffer, got nil") + } + if existing { + t.Error("expected a new buffer, got an existing buffer") + } + err := ReleaseBuffer(w) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + t.Run("can get an existing buffer from the pool", func(t *testing.T) { + w, existing := GetBuffer(new(bytes.Buffer)) + if w == nil { + t.Error("expected a buffer, got nil") + } + if existing { + t.Error("expected a new buffer, got an existing buffer") + } + + w, existing = GetBuffer(w) + if w == nil { + t.Error("expected a buffer, got nil") + } + if !existing { + t.Error("expected an existing buffer, got a new buffer") + } + + err := ReleaseBuffer(w) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + t.Run("can release any writer without error", func(t *testing.T) { + err := ReleaseBuffer(new(bytes.Buffer)) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + }) + t.Run("attempting to buffer a nil writer returns nil", func(t *testing.T) { + w, existing := GetBuffer(nil) + if w != nil { + t.Error("expected nil, got a buffer") + } + if existing { + t.Error("expected nil, got an existing buffer") + } + }) +} diff --git a/runtime/builder_test.go b/runtime/builder_test.go new file mode 100644 index 000000000..f7649970b --- /dev/null +++ b/runtime/builder_test.go @@ -0,0 +1,11 @@ +package runtime + +import "testing" + +func TestGetBuilder(t *testing.T) { + sb := GetBuilder() + sb.WriteString("test") + if sb.String() != "test" { + t.Errorf("expected \"test\", got %q", sb.String()) + } +} diff --git a/runtime/runtime.go b/runtime/runtime.go index fdfd917d2..aaa4a2c48 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -7,11 +7,13 @@ import ( "github.com/a-h/templ" ) +// GeneratedComponentInput is used to avoid generated code needing to import the `context` and `io` packages. type GeneratedComponentInput struct { Context context.Context Writer io.Writer } +// GeneratedTemplate is used to avoid generated code needing to import the `context` and `io` packages. func GeneratedTemplate(f func(GeneratedComponentInput) error) templ.Component { return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { return f(GeneratedComponentInput{ctx, w}) diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go new file mode 100644 index 000000000..d5bb07026 --- /dev/null +++ b/runtime/runtime_test.go @@ -0,0 +1,22 @@ +package runtime + +import ( + "context" + "strings" + "testing" +) + +func TestGeneratedTemplate(t *testing.T) { + f := func(input GeneratedComponentInput) error { + _, err := input.Writer.Write([]byte("Hello, World!")) + return err + } + sb := new(strings.Builder) + err := GeneratedTemplate(f).Render(context.Background(), sb) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + if sb.String() != "Hello, World!" { + t.Errorf("expected \"Hello, World!\", got %q", sb.String()) + } +} From b7645fde8f2f138ec9b64edb59214d7542ae94a8 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 15:15:26 +0100 Subject: [PATCH 04/15] chore: add tests for flush component --- flush.go | 18 ++++++-- flush_test.go | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 flush_test.go diff --git a/flush.go b/flush.go index 8df8016b3..ae195d661 100644 --- a/flush.go +++ b/flush.go @@ -6,6 +6,7 @@ import ( "io" ) +// Flush flushes the output buffer after all its child components have been rendered. func Flush() FlushComponent { return FlushComponent{} } @@ -13,17 +14,24 @@ func Flush() FlushComponent { type FlushComponent struct { } -type flusher interface { +type flusherError interface { Flush() error } +type flusher interface { + Flush() +} + func (f FlushComponent) Render(ctx context.Context, w io.Writer) (err error) { if err = GetChildren(ctx).Render(ctx, w); err != nil { return err } - b, isTemplBuffer := w.(flusher) - if !isTemplBuffer { - return fmt.Errorf("unable to flush, writer is not flushable") + switch w := w.(type) { + case flusher: + w.Flush() + return nil + case flusherError: + return w.Flush() } - return b.Flush() + return fmt.Errorf("unable to flush, writer is not flushable") } diff --git a/flush_test.go b/flush_test.go new file mode 100644 index 000000000..4f5608c60 --- /dev/null +++ b/flush_test.go @@ -0,0 +1,126 @@ +package templ + +import ( + "context" + "fmt" + "io" + "strings" + "testing" +) + +type flushableErrorWriter struct { + lastFlushPos int + pos int + sb strings.Builder + flushedSections []string +} + +func (f *flushableErrorWriter) Write(p []byte) (n int, err error) { + n, err = f.sb.Write(p) + if err != nil { + return + } + if n < len(p) { + err = io.ErrShortWrite + } + f.pos += n + return +} + +func (f *flushableErrorWriter) Flush() error { + f.flushedSections = append(f.flushedSections, f.sb.String()[f.lastFlushPos:f.pos]) + f.lastFlushPos = f.pos + return nil +} + +type flushableWriter struct { + lastFlushPos int + pos int + sb strings.Builder + flushedSections []string +} + +func (f *flushableWriter) Write(p []byte) (n int, err error) { + n, err = f.sb.Write(p) + if err != nil { + return + } + if n < len(p) { + err = io.ErrShortWrite + } + f.pos += n + return +} + +func (f *flushableWriter) Flush() { + f.flushedSections = append(f.flushedSections, f.sb.String()[f.lastFlushPos:f.pos]) + f.lastFlushPos = f.pos +} + +func TestFlush(t *testing.T) { + t.Run("errors in child components are propagated", func(t *testing.T) { + expectedErr := fmt.Errorf("test error") + child := ComponentFunc(func(ctx context.Context, w io.Writer) error { + return expectedErr + }) + + sb := new(strings.Builder) + ctx := WithChildren(context.Background(), child) + + err := Flush().Render(ctx, sb) + if err == nil { + t.Fatalf("expected an error, got nil") + } + if err != expectedErr { + t.Fatalf("expected error to be %v, got %v", expectedErr, err) + } + }) + t.Run("can render to a flushable error writer", func(t *testing.T) { + child := ComponentFunc(func(ctx context.Context, w io.Writer) error { + _, err := w.Write([]byte("hello")) + return err + }) + + b := &flushableErrorWriter{} + ctx := WithChildren(context.Background(), child) + + // Render the FlushComponent to the buffer + if err := Flush().Render(ctx, b); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(b.flushedSections) != 1 { + t.Fatalf("expected 1 flushed section, got %d", len(b.flushedSections)) + } + if b.flushedSections[0] != "hello" { + t.Fatalf("expected flushed section to be 'hello', got %q", b.flushedSections[0]) + } + }) + t.Run("can render to a flushable writer", func(t *testing.T) { + child := ComponentFunc(func(ctx context.Context, w io.Writer) error { + _, err := w.Write([]byte("hello")) + return err + }) + + b := &flushableWriter{} + ctx := WithChildren(context.Background(), child) + + // Render the FlushComponent to the buffer + if err := Flush().Render(ctx, b); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if len(b.flushedSections) != 1 { + t.Fatalf("expected 1 flushed section, got %d", len(b.flushedSections)) + } + if b.flushedSections[0] != "hello" { + t.Fatalf("expected flushed section to be 'hello', got %q", b.flushedSections[0]) + } + }) + t.Run("returns an error if the writer is not flushable", func(t *testing.T) { + sb := new(strings.Builder) + if err := Flush().Render(context.Background(), sb); err == nil { + t.Fatalf("expected an error, got nil") + } + }) +} From f483d273f62e48edb8d0243a1e7f3ce396cc251b Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 17:24:50 +0100 Subject: [PATCH 05/15] feat: support streaming responses in the component handler --- handler.go | 103 ++++++++++++++++++++++++ handler_test.go | 205 ++++++++++++++++++++++++++++++++++++++++++++++++ runtime.go | 68 ---------------- runtime_test.go | 96 ----------------------- 4 files changed, 308 insertions(+), 164 deletions(-) create mode 100644 handler.go create mode 100644 handler_test.go diff --git a/handler.go b/handler.go new file mode 100644 index 000000000..2e48592b6 --- /dev/null +++ b/handler.go @@ -0,0 +1,103 @@ +package templ + +import "net/http" + +// ComponentHandler is a http.Handler that renders components. +type ComponentHandler struct { + Component Component + Status int + ContentType string + ErrorHandler func(r *http.Request, err error) http.Handler + StreamResponse bool +} + +const componentHandlerErrorMessage = "templ: failed to render template" + +func (ch *ComponentHandler) ServeHTTPBuffered(w http.ResponseWriter, r *http.Request) { + // Since the component may error, write to a buffer first. + // This prevents partial responses from being written to the client. + buf := GetBuffer() + defer ReleaseBuffer(buf) + err := ch.Component.Render(r.Context(), buf) + if err != nil { + if ch.ErrorHandler != nil { + w.Header().Set("Content-Type", ch.ContentType) + ch.ErrorHandler(r, err).ServeHTTP(w, r) + return + } + http.Error(w, componentHandlerErrorMessage, http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", ch.ContentType) + if ch.Status != 0 { + w.WriteHeader(ch.Status) + } + // Ignore write error like http.Error() does, because there is + // no way to recover at this point. + _, _ = w.Write(buf.Bytes()) +} + +func (ch *ComponentHandler) ServeHTTPStreamed(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", ch.ContentType) + if ch.Status != 0 { + w.WriteHeader(ch.Status) + } + if err := ch.Component.Render(r.Context(), w); err != nil { + if ch.ErrorHandler != nil { + w.Header().Set("Content-Type", ch.ContentType) + ch.ErrorHandler(r, err).ServeHTTP(w, r) + return + } + http.Error(w, componentHandlerErrorMessage, http.StatusInternalServerError) + } + return +} + +// ServeHTTP implements the http.Handler interface. +func (ch ComponentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if ch.StreamResponse { + ch.ServeHTTPStreamed(w, r) + return + } + ch.ServeHTTPBuffered(w, r) +} + +// Handler creates a http.Handler that renders the template. +func Handler(c Component, options ...func(*ComponentHandler)) *ComponentHandler { + ch := &ComponentHandler{ + Component: c, + ContentType: "text/html; charset=utf-8", + } + for _, o := range options { + o(ch) + } + return ch +} + +// WithStatus sets the HTTP status code returned by the ComponentHandler. +func WithStatus(status int) func(*ComponentHandler) { + return func(ch *ComponentHandler) { + ch.Status = status + } +} + +// WithContentType sets the Content-Type header returned by the ComponentHandler. +func WithContentType(contentType string) func(*ComponentHandler) { + return func(ch *ComponentHandler) { + ch.ContentType = contentType + } +} + +// WithErrorHandler sets the error handler used if rendering fails. +func WithErrorHandler(eh func(r *http.Request, err error) http.Handler) func(*ComponentHandler) { + return func(ch *ComponentHandler) { + ch.ErrorHandler = eh + } +} + +// WithStreaming sets the ComponentHandler to stream the response instead of buffering it. +func WithStreaming() func(*ComponentHandler) { + return func(ch *ComponentHandler) { + ch.StreamResponse = true + } +} diff --git a/handler_test.go b/handler_test.go new file mode 100644 index 000000000..187ecf262 --- /dev/null +++ b/handler_test.go @@ -0,0 +1,205 @@ +package templ_test + +import ( + "context" + "errors" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/a-h/templ" + "github.com/google/go-cmp/cmp" +) + +func TestHandler(t *testing.T) { + hello := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + if _, err := io.WriteString(w, "Hello"); err != nil { + t.Fatalf("failed to write string: %v", err) + } + return nil + }) + errorComponent := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + if _, err := io.WriteString(w, "Hello"); err != nil { + t.Fatalf("failed to write string: %v", err) + } + return errors.New("handler error") + }) + + tests := []struct { + name string + input *templ.ComponentHandler + expectedStatus int + expectedMIMEType string + expectedBody string + }{ + { + name: "handlers return OK by default", + input: templ.Handler(hello), + expectedStatus: http.StatusOK, + expectedMIMEType: "text/html; charset=utf-8", + expectedBody: "Hello", + }, + { + name: "handlers return OK by default", + input: templ.Handler(templ.Raw(`♠ ‘ ♠ ‘`)), + expectedStatus: http.StatusOK, + expectedMIMEType: "text/html; charset=utf-8", + expectedBody: "♠ ‘ ♠ ‘", + }, + { + name: "handlers can be configured to return an alternative status code", + input: templ.Handler(hello, templ.WithStatus(http.StatusNotFound)), + expectedStatus: http.StatusNotFound, + expectedMIMEType: "text/html; charset=utf-8", + expectedBody: "Hello", + }, + { + name: "handlers can be configured to return an alternative status code and content type", + input: templ.Handler(hello, templ.WithStatus(http.StatusOK), templ.WithContentType("text/csv")), + expectedStatus: http.StatusOK, + expectedMIMEType: "text/csv", + expectedBody: "Hello", + }, + { + name: "handlers that fail return a 500 error", + input: templ.Handler(errorComponent), + expectedStatus: http.StatusInternalServerError, + expectedMIMEType: "text/plain; charset=utf-8", + expectedBody: "templ: failed to render template\n", + }, + { + name: "error handling can be customised", + input: templ.Handler(errorComponent, templ.WithErrorHandler(func(r *http.Request, err error) http.Handler { + // Because the error is received, it's possible to log the detail of the request. + // log.Printf("template render error for %v %v: %v", r.Method, r.URL.String(), err) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusBadRequest) + if _, err := io.WriteString(w, "custom body"); err != nil { + t.Fatalf("failed to write string: %v", err) + } + }) + })), + expectedStatus: http.StatusBadRequest, + expectedMIMEType: "text/html; charset=utf-8", + expectedBody: "custom body", + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/test", nil) + tt.input.ServeHTTP(w, r) + if got := w.Result().StatusCode; tt.expectedStatus != got { + t.Errorf("expected status %d, got %d", tt.expectedStatus, got) + } + if mimeType := w.Result().Header.Get("Content-Type"); tt.expectedMIMEType != mimeType { + t.Errorf("expected content-type %s, got %s", tt.expectedMIMEType, mimeType) + } + body, err := io.ReadAll(w.Result().Body) + if err != nil { + t.Errorf("failed to read body: %v", err) + } + if diff := cmp.Diff(tt.expectedBody, string(body)); diff != "" { + t.Error(diff) + } + }) + } + + t.Run("streaming mode allows responses to be flushed", func(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/test", nil) + + component := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + // Write part 1. + if _, err := io.WriteString(w, "Part 1"); err != nil { + return err + } + // Flush. + if f, ok := w.(http.Flusher); ok { + f.Flush() + } + // Check partial response. + wr := w.(*httptest.ResponseRecorder) + actualBody := wr.Body.String() + if diff := cmp.Diff("Part 1", actualBody); diff != "" { + t.Error(diff) + } + // Write part 2. + if _, err := io.WriteString(w, "\nPart 2"); err != nil { + return err + } + return nil + }) + + templ.Handler(component, templ.WithStatus(http.StatusCreated), templ.WithStreaming()).ServeHTTP(w, r) + if got := w.Result().StatusCode; http.StatusCreated != got { + t.Errorf("expected status %d, got %d", http.StatusCreated, got) + } + if mimeType := w.Result().Header.Get("Content-Type"); "text/html; charset=utf-8" != mimeType { + t.Errorf("expected content-type %s, got %s", "text/html; charset=utf-8", mimeType) + } + body, err := io.ReadAll(w.Result().Body) + if err != nil { + t.Errorf("failed to read body: %v", err) + } + if diff := cmp.Diff("Part 1\nPart 2", string(body)); diff != "" { + t.Error(diff) + } + }) + t.Run("streaming mode handles errors", func(t *testing.T) { + w := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/test", nil) + + expectedErr := errors.New("streaming error") + + component := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { + if _, err := io.WriteString(w, "Body"); err != nil { + return err + } + return expectedErr + }) + + var errorHandlerCalled bool + errorHandler := func(r *http.Request, err error) http.Handler { + if expectedErr != err { + t.Errorf("expected error %v, got %v", expectedErr, err) + } + errorHandlerCalled = true + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // This will be ignored, because the header has already been written. + w.WriteHeader(http.StatusBadRequest) + // This will be written, but will be appended to the written body. + io.WriteString(w, "Error message") + }) + } + + h := templ.Handler(component, + templ.WithStatus(http.StatusCreated), + templ.WithStreaming(), + templ.WithErrorHandler(errorHandler), + ) + h.ServeHTTP(w, r) + + if !errorHandlerCalled { + t.Error("expected error handler to be called") + } + // Expect the status code to be 201, not 400, because in streaming mode, + // we have to write the header before we can call the error handler. + if actualRepsonseCode := w.Result().StatusCode; http.StatusCreated != actualRepsonseCode { + t.Errorf("expected status %d, got %d", http.StatusCreated, actualRepsonseCode) + } + // Expect the body to be "BodyError message", not just "Error message" because + // in streaming mode, we've already written part of the body to the response, unlike in + // standard mode where the body is written to a buffer before the response is written, + // ensuring that partial responses are not sent. + actualBody, err := io.ReadAll(w.Result().Body) + if err != nil { + t.Errorf("failed to read body: %v", err) + } + if diff := cmp.Diff("BodyError message", string(actualBody)); diff != "" { + t.Error(diff) + } + }) +} diff --git a/runtime.go b/runtime.go index adcf515ac..d4d5aa093 100644 --- a/runtime.go +++ b/runtime.go @@ -82,74 +82,6 @@ func GetChildren(ctx context.Context) Component { return *v.children } -// ComponentHandler is a http.Handler that renders components. -type ComponentHandler struct { - Component Component - Status int - ContentType string - ErrorHandler func(r *http.Request, err error) http.Handler -} - -const componentHandlerErrorMessage = "templ: failed to render template" - -// ServeHTTP implements the http.Handler interface. -func (ch ComponentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - // Since the component may error, write to a buffer first. - // This prevents partial responses from being written to the client. - buf := GetBuffer() - defer ReleaseBuffer(buf) - err := ch.Component.Render(r.Context(), buf) - if err != nil { - if ch.ErrorHandler != nil { - w.Header().Set("Content-Type", ch.ContentType) - ch.ErrorHandler(r, err).ServeHTTP(w, r) - return - } - http.Error(w, componentHandlerErrorMessage, http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", ch.ContentType) - if ch.Status != 0 { - w.WriteHeader(ch.Status) - } - // Ignore write error like http.Error() does, because there is - // no way to recover at this point. - _, _ = w.Write(buf.Bytes()) -} - -// Handler creates a http.Handler that renders the template. -func Handler(c Component, options ...func(*ComponentHandler)) *ComponentHandler { - ch := &ComponentHandler{ - Component: c, - ContentType: "text/html; charset=utf-8", - } - for _, o := range options { - o(ch) - } - return ch -} - -// WithStatus sets the HTTP status code returned by the ComponentHandler. -func WithStatus(status int) func(*ComponentHandler) { - return func(ch *ComponentHandler) { - ch.Status = status - } -} - -// WithContentType sets the Content-Type header returned by the ComponentHandler. -func WithContentType(contentType string) func(*ComponentHandler) { - return func(ch *ComponentHandler) { - ch.ContentType = contentType - } -} - -// WithErrorHandler sets the error handler used if rendering fails. -func WithErrorHandler(eh func(r *http.Request, err error) http.Handler) func(*ComponentHandler) { - return func(ch *ComponentHandler) { - ch.ErrorHandler = eh - } -} - // EscapeString escapes HTML text within templates. func EscapeString(s string) string { return html.EscapeString(s) diff --git a/runtime_test.go b/runtime_test.go index 8fcd1f8a4..956f8738d 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -377,102 +377,6 @@ func TestClassesFunction(t *testing.T) { } } -func TestHandler(t *testing.T) { - hello := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { - if _, err := io.WriteString(w, "Hello"); err != nil { - t.Fatalf("failed to write string: %v", err) - } - return nil - }) - errorComponent := templ.ComponentFunc(func(ctx context.Context, w io.Writer) error { - if _, err := io.WriteString(w, "Hello"); err != nil { - t.Fatalf("failed to write string: %v", err) - } - return errors.New("handler error") - }) - - tests := []struct { - name string - input *templ.ComponentHandler - expectedStatus int - expectedMIMEType string - expectedBody string - }{ - { - name: "handlers return OK by default", - input: templ.Handler(hello), - expectedStatus: http.StatusOK, - expectedMIMEType: "text/html; charset=utf-8", - expectedBody: "Hello", - }, - { - name: "handlers return OK by default", - input: templ.Handler(templ.Raw(`♠ ‘ ♠ ‘`)), - expectedStatus: http.StatusOK, - expectedMIMEType: "text/html; charset=utf-8", - expectedBody: "♠ ‘ ♠ ‘", - }, - { - name: "handlers can be configured to return an alternative status code", - input: templ.Handler(hello, templ.WithStatus(http.StatusNotFound)), - expectedStatus: http.StatusNotFound, - expectedMIMEType: "text/html; charset=utf-8", - expectedBody: "Hello", - }, - { - name: "handlers can be configured to return an alternative status code and content type", - input: templ.Handler(hello, templ.WithStatus(http.StatusOK), templ.WithContentType("text/csv")), - expectedStatus: http.StatusOK, - expectedMIMEType: "text/csv", - expectedBody: "Hello", - }, - { - name: "handlers that fail return a 500 error", - input: templ.Handler(errorComponent), - expectedStatus: http.StatusInternalServerError, - expectedMIMEType: "text/plain; charset=utf-8", - expectedBody: "templ: failed to render template\n", - }, - { - name: "error handling can be customised", - input: templ.Handler(errorComponent, templ.WithErrorHandler(func(r *http.Request, err error) http.Handler { - // Because the error is received, it's possible to log the detail of the request. - // log.Printf("template render error for %v %v: %v", r.Method, r.URL.String(), err) - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusBadRequest) - if _, err := io.WriteString(w, "custom body"); err != nil { - t.Fatalf("failed to write string: %v", err) - } - }) - })), - expectedStatus: http.StatusBadRequest, - expectedMIMEType: "text/html; charset=utf-8", - expectedBody: "custom body", - }, - } - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - w := httptest.NewRecorder() - r := httptest.NewRequest("GET", "/test", nil) - tt.input.ServeHTTP(w, r) - if got := w.Result().StatusCode; tt.expectedStatus != got { - t.Errorf("expected status %d, got %d", tt.expectedStatus, got) - } - if mimeType := w.Result().Header.Get("Content-Type"); tt.expectedMIMEType != mimeType { - t.Errorf("expected content-type %s, got %s", tt.expectedMIMEType, mimeType) - } - body, err := io.ReadAll(w.Result().Body) - if err != nil { - t.Errorf("failed to read body: %v", err) - } - if diff := cmp.Diff(tt.expectedBody, string(body)); diff != "" { - t.Error(diff) - } - }) - } -} - func TestRenderScriptItems(t *testing.T) { s1 := templ.ComponentScript{ Name: "s1", From 1ee2794a4c6357698f37fe5289d7639e461d9fdb Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 17:33:47 +0100 Subject: [PATCH 06/15] chore: fix linter nits --- handler_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/handler_test.go b/handler_test.go index 187ecf262..7e7d6cea8 100644 --- a/handler_test.go +++ b/handler_test.go @@ -171,7 +171,9 @@ func TestHandler(t *testing.T) { // This will be ignored, because the header has already been written. w.WriteHeader(http.StatusBadRequest) // This will be written, but will be appended to the written body. - io.WriteString(w, "Error message") + if _, err := io.WriteString(w, "Error message"); err != nil { + t.Errorf("failed to write error message: %v", err) + } }) } From 06d3a8730872659fdccd987b45f1b1ca1062f987 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Sat, 22 Jun 2024 19:41:31 +0100 Subject: [PATCH 07/15] chore: linter nits --- handler.go | 1 - 1 file changed, 1 deletion(-) diff --git a/handler.go b/handler.go index 2e48592b6..a28d561e2 100644 --- a/handler.go +++ b/handler.go @@ -50,7 +50,6 @@ func (ch *ComponentHandler) ServeHTTPStreamed(w http.ResponseWriter, r *http.Req } http.Error(w, componentHandlerErrorMessage, http.StatusInternalServerError) } - return } // ServeHTTP implements the http.Handler interface. From b4f0d6a01c04bce32151faee0ff0d90d75fbf73b Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 09:09:05 +0100 Subject: [PATCH 08/15] fix: removal of comments during import processing --- cmd/templ/import/process.go | 61 +++++++++---------- cmd/templ/import/testdata/comments.txtar | 12 ++++ .../import/testdata/namedimportsadd.txtar | 19 ++++++ .../import/testdata/namedimportsremoved.txtar | 16 +++++ 4 files changed, 77 insertions(+), 31 deletions(-) create mode 100644 cmd/templ/import/testdata/comments.txtar create mode 100644 cmd/templ/import/testdata/namedimportsadd.txtar create mode 100644 cmd/templ/import/testdata/namedimportsremoved.txtar diff --git a/cmd/templ/import/process.go b/cmd/templ/import/process.go index 7bc7c07ce..70a6775f6 100644 --- a/cmd/templ/import/process.go +++ b/cmd/templ/import/process.go @@ -6,14 +6,15 @@ import ( "go/ast" "go/format" "go/token" - "log" "path" "slices" + "strconv" "strings" goparser "go/parser" "golang.org/x/sync/errgroup" + "golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/imports" "github.com/a-h/templ/generator" @@ -88,60 +89,58 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) { return nil }) - var gofile *ast.File + var firstGoNodeInTemplate *ast.File // Update the template with the imports. // Ensure that there is a Go expression to add the imports to as the first node. eg.Go(func() (err error) { - gofile, err = goparser.ParseFile(fset, fileName, t.Package.Expression.Value+"\n"+importsNode.Expression.Value, goparser.AllErrors) + firstGoNodeInTemplate, err = goparser.ParseFile(fset, fileName, t.Package.Expression.Value+"\n"+importsNode.Expression.Value, goparser.AllErrors|goparser.ParseComments) if err != nil { - log.Printf("failed to parse go code: %v", importsNode.Expression.Value) return fmt.Errorf("failed to parse imports section: %w", err) } return nil }) + + // Wait for completion of both parts. if err := eg.Wait(); err != nil { return t, err } - slices.SortFunc(updatedImports, func(a, b *ast.ImportSpec) int { - return strings.Compare(a.Path.Value, b.Path.Value) - }) - newImportDecl := &ast.GenDecl{ - Tok: token.IMPORT, - Specs: convertSlice(updatedImports), - } + // Delete all the existing imports. - var indicesToDelete []int - for i, decl := range gofile.Decls { - if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT { - indicesToDelete = append(indicesToDelete, i) + for _, imp := range firstGoNodeInTemplate.Imports { + var name string + if imp.Name != nil { + name = imp.Name.Name } + var path string + if imp.Path != nil { + path, _ = strconv.Unquote(imp.Path.Value) + } + astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path) } - for i := len(indicesToDelete) - 1; i >= 0; i-- { - gofile.Decls = append(gofile.Decls[:indicesToDelete[i]], gofile.Decls[indicesToDelete[i]+1:]...) - } - if len(updatedImports) > 0 { - gofile.Imports = updatedImports - gofile.Decls = append([]ast.Decl{newImportDecl}, gofile.Decls...) + // Add imports, if there are any to add. + for _, imp := range updatedImports { + var name string + if imp.Name != nil { + name = imp.Name.Name + } + var path string + if imp.Path != nil { + path, _ = strconv.Unquote(imp.Path.Value) + } + astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path) } // Write out the Go code with the imports. updatedGoCode := new(strings.Builder) - err := format.Node(updatedGoCode, fset, gofile) + err := format.Node(updatedGoCode, fset, firstGoNodeInTemplate) if err != nil { return t, fmt.Errorf("failed to write updated go code: %w", err) } + // Remove the package statement from the node, by cutting the first line of the file. importsNode.Expression.Value = strings.TrimSpace(strings.SplitN(updatedGoCode.String(), "\n", 2)[1]) - if len(updatedImports) == 0 && importsNode.Expression.Value == "" { + if len(updatedImports) == 0 { t.Nodes = t.Nodes[1:] return t, nil } t.Nodes[0] = importsNode return t, nil } - -func convertSlice(slice []*ast.ImportSpec) []ast.Spec { - result := make([]ast.Spec, len(slice)) - for i, v := range slice { - result[i] = ast.Spec(v) - } - return result -} diff --git a/cmd/templ/import/testdata/comments.txtar b/cmd/templ/import/testdata/comments.txtar new file mode 100644 index 000000000..dd0f3188c --- /dev/null +++ b/cmd/templ/import/testdata/comments.txtar @@ -0,0 +1,12 @@ +-- fmt_templ.templ -- +package test + +// Comment on variable or function. +var x = fmt.Sprintf("Hello") +-- fmt_templ.templ -- +package test + +import "fmt" + +// Comment on variable or function. +var x = fmt.Sprintf("Hello") diff --git a/cmd/templ/import/testdata/namedimportsadd.txtar b/cmd/templ/import/testdata/namedimportsadd.txtar new file mode 100644 index 000000000..370f58d5b --- /dev/null +++ b/cmd/templ/import/testdata/namedimportsadd.txtar @@ -0,0 +1,19 @@ +-- fmt_templ.templ -- +package test + +import ( + sconv "strconv" +) + +// Comment on variable or function. +var x = fmt.Sprintf(sconv.Quote("Hello")) +-- fmt_templ.templ -- +package test + +import ( + "fmt" + sconv "strconv" +) + +// Comment on variable or function. +var x = fmt.Sprintf(sconv.Quote("Hello")) diff --git a/cmd/templ/import/testdata/namedimportsremoved.txtar b/cmd/templ/import/testdata/namedimportsremoved.txtar new file mode 100644 index 000000000..62c799e42 --- /dev/null +++ b/cmd/templ/import/testdata/namedimportsremoved.txtar @@ -0,0 +1,16 @@ +-- fmt_templ.templ -- +package test + +import ( + sconv "strconv" +) + +// Comment on variable or function. +var x = fmt.Sprintf("Hello") +-- fmt_templ.templ -- +package test + +import "fmt" + +// Comment on variable or function. +var x = fmt.Sprintf("Hello") From eaeb287e713b6c277db210b1094b428a33db26a1 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 09:11:25 +0100 Subject: [PATCH 09/15] refactor: don't throw errors on non-flushable streams --- flush.go | 3 +-- flush_test.go | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/flush.go b/flush.go index ae195d661..56d7d3a62 100644 --- a/flush.go +++ b/flush.go @@ -2,7 +2,6 @@ package templ import ( "context" - "fmt" "io" ) @@ -33,5 +32,5 @@ func (f FlushComponent) Render(ctx context.Context, w io.Writer) (err error) { case flusherError: return w.Flush() } - return fmt.Errorf("unable to flush, writer is not flushable") + return nil } diff --git a/flush_test.go b/flush_test.go index 4f5608c60..5b046cf49 100644 --- a/flush_test.go +++ b/flush_test.go @@ -117,10 +117,10 @@ func TestFlush(t *testing.T) { t.Fatalf("expected flushed section to be 'hello', got %q", b.flushedSections[0]) } }) - t.Run("returns an error if the writer is not flushable", func(t *testing.T) { + t.Run("non-flushable streams are a no-op", func(t *testing.T) { sb := new(strings.Builder) - if err := Flush().Render(context.Background(), sb); err == nil { - t.Fatalf("expected an error, got nil") + if err := Flush().Render(context.Background(), sb); err != nil { + t.Fatalf("expected no error, got %v", err) } }) } From 0ec8538938d901c060efae8eebccc9234b7eb904 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 09:17:14 +0100 Subject: [PATCH 10/15] refactor: extract reusable function --- cmd/templ/import/process.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/cmd/templ/import/process.go b/cmd/templ/import/process.go index 70a6775f6..49617bfb9 100644 --- a/cmd/templ/import/process.go +++ b/cmd/templ/import/process.go @@ -107,25 +107,17 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) { // Delete all the existing imports. for _, imp := range firstGoNodeInTemplate.Imports { - var name string - if imp.Name != nil { - name = imp.Name.Name - } - var path string - if imp.Path != nil { - path, _ = strconv.Unquote(imp.Path.Value) + name, path, err := getImportDetails(imp) + if err != nil { + return t, err } astutil.DeleteNamedImport(fset, firstGoNodeInTemplate, name, path) } // Add imports, if there are any to add. for _, imp := range updatedImports { - var name string - if imp.Name != nil { - name = imp.Name.Name - } - var path string - if imp.Path != nil { - path, _ = strconv.Unquote(imp.Path.Value) + name, path, err := getImportDetails(imp) + if err != nil { + return t, err } astutil.AddNamedImport(fset, firstGoNodeInTemplate, name, path) } @@ -144,3 +136,17 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) { t.Nodes[0] = importsNode return t, nil } + +func getImportDetails(imp *ast.ImportSpec) (name, path string, err error) { + if imp.Name != nil { + name = imp.Name.Name + } + if imp.Path != nil { + path, err = strconv.Unquote(imp.Path.Value) + if err != nil { + err = fmt.Errorf("failed to unquote package path %s: %w", imp.Path.Value, err) + return + } + } + return name, path, nil +} From e77e927ce9caf3e6f943c7fd6e489f8807625622 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 09:18:52 +0100 Subject: [PATCH 11/15] refactor: rename import directory to match package name --- .version | 2 +- cmd/templ/fmtcmd/main.go | 2 +- cmd/templ/{import => imports}/process.go | 0 cmd/templ/{import => imports}/process_test.go | 0 cmd/templ/{import => imports}/testdata/comments.txtar | 0 cmd/templ/{import => imports}/testdata/deleteimports.txtar | 0 cmd/templ/{import => imports}/testdata/header.txtar | 0 cmd/templ/{import => imports}/testdata/namedimportsadd.txtar | 0 .../{import => imports}/testdata/namedimportsremoved.txtar | 0 cmd/templ/{import => imports}/testdata/noimports.txtar | 0 cmd/templ/{import => imports}/testdata/stringexp.txtar | 0 cmd/templ/{import => imports}/testdata/twoimports.txtar | 0 cmd/templ/lspcmd/proxy/server.go | 2 +- 13 files changed, 3 insertions(+), 3 deletions(-) rename cmd/templ/{import => imports}/process.go (100%) rename cmd/templ/{import => imports}/process_test.go (100%) rename cmd/templ/{import => imports}/testdata/comments.txtar (100%) rename cmd/templ/{import => imports}/testdata/deleteimports.txtar (100%) rename cmd/templ/{import => imports}/testdata/header.txtar (100%) rename cmd/templ/{import => imports}/testdata/namedimportsadd.txtar (100%) rename cmd/templ/{import => imports}/testdata/namedimportsremoved.txtar (100%) rename cmd/templ/{import => imports}/testdata/noimports.txtar (100%) rename cmd/templ/{import => imports}/testdata/stringexp.txtar (100%) rename cmd/templ/{import => imports}/testdata/twoimports.txtar (100%) diff --git a/.version b/.version index d1bfc105e..d26f8f48a 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.727 \ No newline at end of file +0.2.728 \ No newline at end of file diff --git a/cmd/templ/fmtcmd/main.go b/cmd/templ/fmtcmd/main.go index 7db74df26..c6ae7406c 100644 --- a/cmd/templ/fmtcmd/main.go +++ b/cmd/templ/fmtcmd/main.go @@ -10,7 +10,7 @@ import ( "sync" "time" - imports "github.com/a-h/templ/cmd/templ/import" + "github.com/a-h/templ/cmd/templ/imports" "github.com/a-h/templ/cmd/templ/processor" parser "github.com/a-h/templ/parser/v2" "github.com/natefinch/atomic" diff --git a/cmd/templ/import/process.go b/cmd/templ/imports/process.go similarity index 100% rename from cmd/templ/import/process.go rename to cmd/templ/imports/process.go diff --git a/cmd/templ/import/process_test.go b/cmd/templ/imports/process_test.go similarity index 100% rename from cmd/templ/import/process_test.go rename to cmd/templ/imports/process_test.go diff --git a/cmd/templ/import/testdata/comments.txtar b/cmd/templ/imports/testdata/comments.txtar similarity index 100% rename from cmd/templ/import/testdata/comments.txtar rename to cmd/templ/imports/testdata/comments.txtar diff --git a/cmd/templ/import/testdata/deleteimports.txtar b/cmd/templ/imports/testdata/deleteimports.txtar similarity index 100% rename from cmd/templ/import/testdata/deleteimports.txtar rename to cmd/templ/imports/testdata/deleteimports.txtar diff --git a/cmd/templ/import/testdata/header.txtar b/cmd/templ/imports/testdata/header.txtar similarity index 100% rename from cmd/templ/import/testdata/header.txtar rename to cmd/templ/imports/testdata/header.txtar diff --git a/cmd/templ/import/testdata/namedimportsadd.txtar b/cmd/templ/imports/testdata/namedimportsadd.txtar similarity index 100% rename from cmd/templ/import/testdata/namedimportsadd.txtar rename to cmd/templ/imports/testdata/namedimportsadd.txtar diff --git a/cmd/templ/import/testdata/namedimportsremoved.txtar b/cmd/templ/imports/testdata/namedimportsremoved.txtar similarity index 100% rename from cmd/templ/import/testdata/namedimportsremoved.txtar rename to cmd/templ/imports/testdata/namedimportsremoved.txtar diff --git a/cmd/templ/import/testdata/noimports.txtar b/cmd/templ/imports/testdata/noimports.txtar similarity index 100% rename from cmd/templ/import/testdata/noimports.txtar rename to cmd/templ/imports/testdata/noimports.txtar diff --git a/cmd/templ/import/testdata/stringexp.txtar b/cmd/templ/imports/testdata/stringexp.txtar similarity index 100% rename from cmd/templ/import/testdata/stringexp.txtar rename to cmd/templ/imports/testdata/stringexp.txtar diff --git a/cmd/templ/import/testdata/twoimports.txtar b/cmd/templ/imports/testdata/twoimports.txtar similarity index 100% rename from cmd/templ/import/testdata/twoimports.txtar rename to cmd/templ/imports/testdata/twoimports.txtar diff --git a/cmd/templ/lspcmd/proxy/server.go b/cmd/templ/lspcmd/proxy/server.go index 305400e27..02cc7b4c7 100644 --- a/cmd/templ/lspcmd/proxy/server.go +++ b/cmd/templ/lspcmd/proxy/server.go @@ -9,7 +9,7 @@ import ( "github.com/a-h/parse" lsp "github.com/a-h/protocol" "github.com/a-h/templ" - imports "github.com/a-h/templ/cmd/templ/import" + "github.com/a-h/templ/cmd/templ/imports" "github.com/a-h/templ/generator" "github.com/a-h/templ/parser/v2" "go.lsp.dev/uri" From f4dd349d7c7df6a41bf7190fb10cfb9340d4e1f6 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 09:32:15 +0100 Subject: [PATCH 12/15] feat: add streaming example --- examples/streaming/go.mod | 7 ++ examples/streaming/main.templ | 53 +++++++++++++++ examples/streaming/main_templ.go | 110 +++++++++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 examples/streaming/go.mod create mode 100644 examples/streaming/main.templ create mode 100644 examples/streaming/main_templ.go diff --git a/examples/streaming/go.mod b/examples/streaming/go.mod new file mode 100644 index 000000000..80d341ae0 --- /dev/null +++ b/examples/streaming/go.mod @@ -0,0 +1,7 @@ +module githbu.com/a-h/templ/examples/streaming + +go 1.21.5 + +replace github.com/a-h/templ => ../../ + +require github.com/a-h/templ v0.2.707 // indirect diff --git a/examples/streaming/main.templ b/examples/streaming/main.templ new file mode 100644 index 000000000..6dc08a806 --- /dev/null +++ b/examples/streaming/main.templ @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "net/http" + "time" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Create a channel to send data to the template. + data := make(chan string) + // Run a background process that will take 10 seconds to complete. + go func() { + // Always remember to close the channel. + defer close(data) + for i := 0; i < 10; i++ { + select { + case <-r.Context().Done(): + // Quit early if the client is no longer connected. + return + case <-time.After(time.Second): + // Send a new piece of data to the channel. + data <- fmt.Sprintf("Part %d", i+1) + } + } + }() + + // Pass the channel to the template. + component := Page(data) + + // Serve using the streaming mode of the handler. + templ.Handler(component, templ.WithStreaming()).ServeHTTP(w, r) + }) + http.ListenAndServe("127.0.0.1:8080", nil) +} + +templ Page(data chan string) { + + + + Page + + +

Page

+ for d := range data { + @templ.Flush() { +
{ d }
+ } + } + + +} diff --git a/examples/streaming/main_templ.go b/examples/streaming/main_templ.go new file mode 100644 index 000000000..4b32b024c --- /dev/null +++ b/examples/streaming/main_templ.go @@ -0,0 +1,110 @@ +// Code generated by templ - 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 templruntime "github.com/a-h/templ/runtime" + +import ( + "fmt" + "net/http" + "time" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Create a channel to send data to the template. + data := make(chan string) + // Run a background process that will take 10 seconds to complete. + go func() { + // Always remember to close the channel. + defer close(data) + for i := 0; i < 10; i++ { + select { + case <-r.Context().Done(): + // Quit early if the client is no longer connected. + return + case <-time.After(time.Second): + // Send a new piece of data to the channel. + data <- fmt.Sprintf("Part %d", i+1) + } + } + }() + + // Pass the channel to the template. + component := Page(data) + + // Serve using the streaming mode of the handler. + templ.Handler(component, templ.WithStreaming()).ServeHTTP(w, r) + }) + http.ListenAndServe("127.0.0.1:8080", nil) +} + +func Page(data chan 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 + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Page

Page

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for d := range data { + templ_7745c5c3_Var2 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(d) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/streaming/main.templ`, Line: 48, Col: 13} + } + _, 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("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = templ.Flush().Render(templ.WithChildren(ctx, templ_7745c5c3_Var2), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} From 49fa03e13c2ddd5467a8e001c15f2aab50e674b4 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 10:15:14 +0100 Subject: [PATCH 13/15] feat: add declarative shadow dom example --- examples/suspense/go.mod | 7 + examples/suspense/go.sum | 2 + examples/suspense/main.templ | 111 +++++++++++ examples/suspense/main_templ.go | 318 ++++++++++++++++++++++++++++++++ 4 files changed, 438 insertions(+) create mode 100644 examples/suspense/go.mod create mode 100644 examples/suspense/go.sum create mode 100644 examples/suspense/main.templ create mode 100644 examples/suspense/main_templ.go diff --git a/examples/suspense/go.mod b/examples/suspense/go.mod new file mode 100644 index 000000000..92bf26420 --- /dev/null +++ b/examples/suspense/go.mod @@ -0,0 +1,7 @@ +module githbu.com/a-h/templ/examples/suspense + +go 1.21.5 + +replace github.com/a-h/templ => ../../ + +require github.com/a-h/templ v0.2.707 diff --git a/examples/suspense/go.sum b/examples/suspense/go.sum new file mode 100644 index 000000000..5a8d551d8 --- /dev/null +++ b/examples/suspense/go.sum @@ -0,0 +1,2 @@ +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= diff --git a/examples/suspense/main.templ b/examples/suspense/main.templ new file mode 100644 index 000000000..f789576e7 --- /dev/null +++ b/examples/suspense/main.templ @@ -0,0 +1,111 @@ +package main + +import ( + "net/http" + "sync" + "time" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Create a channel to send deferred component renders to the template. + data := make(chan SlotContents) + + // We know there are 3 slots, so start a WaitGround. + var wg sync.WaitGroup + wg.Add(3) + + // Start the async processes. + // Sidebar. + go func() { + defer wg.Done() + time.Sleep(time.Second * 3) + data <- SlotContents{ + Name: "a", + Contents: A(), + } + }() + + // Content. + go func() { + defer wg.Done() + time.Sleep(time.Second * 2) + data <- SlotContents{ + Name: "b", + Contents: B(), + } + }() + + // Footer. + go func() { + defer wg.Done() + time.Sleep(time.Second * 1) + data <- SlotContents{ + Name: "c", + Contents: C(), + } + }() + + // Close the channel when all processes are done. + go func() { + wg.Wait() + close(data) + }() + + // Pass the channel to the template. + component := Page(data) + + // Serve using the streaming mode of the handler. + templ.Handler(component, templ.WithStreaming()).ServeHTTP(w, r) + }) + http.ListenAndServe("127.0.0.1:8080", nil) +} + +type SlotContents struct { + Name string + Contents templ.Component +} + +templ Slot(name string) { + +
Loading { name }...
+
+} + +templ A() { +
Component A.
+} + +templ B() { +
Component B.
+} + +templ C() { +
Component C.
+} + +templ Page(data chan SlotContents) { + + + + Page + + +

Page

+ @templ.Flush() { + + } + for sc := range data { + @templ.Flush() { +
+ @sc.Contents +
+ } + } + + +} diff --git a/examples/suspense/main_templ.go b/examples/suspense/main_templ.go new file mode 100644 index 000000000..552caf95b --- /dev/null +++ b/examples/suspense/main_templ.go @@ -0,0 +1,318 @@ +// Code generated by templ - DO NOT EDIT. + +// templ: version: v0.2.728 +package main + +//lint:file-ignore SA4006 This context is only used if a nested component is present. + +import "github.com/a-h/templ" +import templruntime "github.com/a-h/templ/runtime" + +import ( + "net/http" + "sync" + "time" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Create a channel to send deferred component renders to the template. + data := make(chan SlotContents) + + // We know there are 3 slots, so start a WaitGround. + var wg sync.WaitGroup + wg.Add(3) + + // Start the async processes. + // Sidebar. + go func() { + defer wg.Done() + time.Sleep(time.Second * 3) + data <- SlotContents{ + Name: "a", + Contents: A(), + } + }() + + // Content. + go func() { + defer wg.Done() + time.Sleep(time.Second * 2) + data <- SlotContents{ + Name: "b", + Contents: B(), + } + }() + + // Footer. + go func() { + defer wg.Done() + time.Sleep(time.Second * 1) + data <- SlotContents{ + Name: "c", + Contents: C(), + } + }() + + // Close the channel when all processes are done. + go func() { + wg.Wait() + close(data) + }() + + // Pass the channel to the template. + component := Page(data) + + // Serve using the streaming mode of the handler. + templ.Handler(component, templ.WithStreaming()).ServeHTTP(w, r) + }) + http.ListenAndServe("127.0.0.1:8080", nil) +} + +type SlotContents struct { + Name string + Contents templ.Component +} + +func Slot(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 + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var1 := templ.GetChildren(ctx) + if templ_7745c5c3_Var1 == nil { + templ_7745c5c3_Var1 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Loading ") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var3 string + templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(name) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `main.templ`, Line: 71, Col: 21} + } + _, 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("...
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +func A() 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 + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + 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("
Component A.
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +func B() 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 + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var5 := templ.GetChildren(ctx) + if templ_7745c5c3_Var5 == nil { + templ_7745c5c3_Var5 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Component B.
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +func C() 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 + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var6 := templ.GetChildren(ctx) + if templ_7745c5c3_Var6 == nil { + templ_7745c5c3_Var6 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
Component C.
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} + +func Page(data chan SlotContents) 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 + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var7 := templ.GetChildren(ctx) + if templ_7745c5c3_Var7 == nil { + templ_7745c5c3_Var7 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Page

Page

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Var8 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = templ.Flush().Render(templ.WithChildren(ctx, templ_7745c5c3_Var8), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + for sc := range data { + templ_7745c5c3_Var9 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = sc.Contents.Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) + templ_7745c5c3_Err = templ.Flush().Render(templ.WithChildren(ctx, templ_7745c5c3_Var9), templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + return templ_7745c5c3_Err + }) +} From 892eb10babea2d477dc859b38360a231077fc40d Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 10:16:23 +0100 Subject: [PATCH 14/15] chore: strip versions from generated files --- examples/suspense/main_templ.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/suspense/main_templ.go b/examples/suspense/main_templ.go index 552caf95b..77c6f2705 100644 --- a/examples/suspense/main_templ.go +++ b/examples/suspense/main_templ.go @@ -1,6 +1,5 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.2.728 package main //lint:file-ignore SA4006 This context is only used if a nested component is present. @@ -99,7 +98,7 @@ func Slot(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: `main.templ`, Line: 70, Col: 18} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/suspense/main.templ`, Line: 70, Col: 18} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2)) if templ_7745c5c3_Err != nil { @@ -112,7 +111,7 @@ func Slot(name string) templ.Component { var templ_7745c5c3_Var3 string templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `main.templ`, Line: 71, Col: 21} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/suspense/main.templ`, Line: 71, Col: 21} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3)) if templ_7745c5c3_Err != nil { @@ -284,7 +283,7 @@ func Page(data chan SlotContents) templ.Component { var templ_7745c5c3_Var10 string templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(sc.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `main.templ`, Line: 104, Col: 24} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `examples/suspense/main.templ`, Line: 104, Col: 24} } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { From f63583333f832dc67855144f25e6589a9973ba3a Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 25 Jun 2024 17:41:09 +0100 Subject: [PATCH 15/15] fix: don't wipe out code that doesn't have imports --- cmd/templ/imports/process.go | 2 +- cmd/templ/imports/testdata/extraspace.txtar | 15 ++++++++++++++ .../imports/testdata/noimportscode.txtar | 20 +++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 cmd/templ/imports/testdata/extraspace.txtar create mode 100644 cmd/templ/imports/testdata/noimportscode.txtar diff --git a/cmd/templ/imports/process.go b/cmd/templ/imports/process.go index 49617bfb9..12e88cf43 100644 --- a/cmd/templ/imports/process.go +++ b/cmd/templ/imports/process.go @@ -129,7 +129,7 @@ func Process(t parser.TemplateFile) (parser.TemplateFile, error) { } // Remove the package statement from the node, by cutting the first line of the file. importsNode.Expression.Value = strings.TrimSpace(strings.SplitN(updatedGoCode.String(), "\n", 2)[1]) - if len(updatedImports) == 0 { + if len(updatedImports) == 0 && importsNode.Expression.Value == "" { t.Nodes = t.Nodes[1:] return t, nil } diff --git a/cmd/templ/imports/testdata/extraspace.txtar b/cmd/templ/imports/testdata/extraspace.txtar new file mode 100644 index 000000000..182513c67 --- /dev/null +++ b/cmd/templ/imports/testdata/extraspace.txtar @@ -0,0 +1,15 @@ +-- fmt_templ.templ -- +package test + +const x = 123 + + +var x = fmt.Sprintf("Hello") +-- fmt_templ.templ -- +package test + +import "fmt" + +const x = 123 + +var x = fmt.Sprintf("Hello") diff --git a/cmd/templ/imports/testdata/noimportscode.txtar b/cmd/templ/imports/testdata/noimportscode.txtar new file mode 100644 index 000000000..4b67f4109 --- /dev/null +++ b/cmd/templ/imports/testdata/noimportscode.txtar @@ -0,0 +1,20 @@ +-- fmt.templ -- +package test + +func test() { + // Do nothing. +} + +templ Hello() { +
Hello
+} +-- fmt.templ -- +package test + +func test() { + // Do nothing. +} + +templ Hello() { +
Hello
+}