Skip to content

Commit

Permalink
feat: enable implicit context, fixes #253
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Oct 28, 2023
1 parent 78a6f93 commit a8e833a
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 17 deletions.
32 changes: 16 additions & 16 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (g *generator) writeTemplate(nodeIdx int, t parser.HTMLTemplate) error {
}
indentLevel++
// return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
if _, err = g.w.WriteIndent(indentLevel, "return templ.ComponentFunc(func(templ_7745c5c3_Ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {\n"); err != nil {
if _, err = g.w.WriteIndent(indentLevel, "return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {\n"); err != nil {
return err
}
{
Expand All @@ -289,15 +289,15 @@ func (g *generator) writeTemplate(nodeIdx int, t parser.HTMLTemplate) error {
return err
}
// ctx = templ.InitializeContext(ctx)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Ctx = templ.InitializeContext(templ_7745c5c3_Ctx)\n"); err != nil {
if _, err = g.w.WriteIndent(indentLevel, "ctx = templ.InitializeContext(ctx)\n"); err != nil {
return err
}
g.childrenVar = g.createVariableName()
// templ_7745c5c3_Var1 := templ.GetChildren(ctx)
// if templ_7745c5c3_Var1 == nil {
// templ_7745c5c3_Var1 = templ.NopComponent
// }
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("%s := templ.GetChildren(templ_7745c5c3_Ctx)\n", g.childrenVar)); err != nil {
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("%s := templ.GetChildren(ctx)\n", g.childrenVar)); err != nil {
return err
}
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("if %s == nil {\n", g.childrenVar)); err != nil {
Expand All @@ -314,7 +314,7 @@ func (g *generator) writeTemplate(nodeIdx int, t parser.HTMLTemplate) error {
return err
}
// ctx = templ.ClearChildren(children)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Ctx = templ.ClearChildren(templ_7745c5c3_Ctx)\n"); err != nil {
if _, err = g.w.WriteIndent(indentLevel, "ctx = templ.ClearChildren(ctx)\n"); err != nil {
return err
}
// Nodes.
Expand Down Expand Up @@ -602,7 +602,7 @@ func (g *generator) writeSwitchExpression(indentLevel int, n parser.SwitchExpres
}

func (g *generator) writeChildrenExpression(indentLevel int) (err error) {
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_Err = %s.Render(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer)\n", g.childrenVar)); err != nil {
if _, err = g.w.WriteIndent(indentLevel, fmt.Sprintf("templ_7745c5c3_Err = %s.Render(ctx, templ_7745c5c3_Buffer)\n", g.childrenVar)); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
Expand All @@ -621,7 +621,7 @@ func (g *generator) writeTemplElementExpression(indentLevel int, n parser.TemplE
func (g *generator) writeBlockTemplElementExpression(indentLevel int, n parser.TemplElementExpression) (err error) {
var r parser.Range
childrenName := g.createVariableName()
if _, err = g.w.WriteIndent(indentLevel, childrenName+" := templ.ComponentFunc(func(templ_7745c5c3_Ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {\n"); err != nil {
if _, err = g.w.WriteIndent(indentLevel, childrenName+" := templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {\n"); err != nil {
return err
}
indentLevel++
Expand Down Expand Up @@ -661,8 +661,8 @@ func (g *generator) writeBlockTemplElementExpression(indentLevel int, n parser.T
return err
}
g.sourceMap.Add(n.Expression, r)
// .Render(templ.WithChildren(templ_7745c5c3_Ctx, children), templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(templ.WithChildren(templ_7745c5c3_Ctx, " + childrenName + "), templ_7745c5c3_Buffer)\n"); err != nil {
// .Render(templ.WithChildren(ctx, children), templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(templ.WithChildren(ctx, " + childrenName + "), templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
Expand All @@ -681,8 +681,8 @@ func (g *generator) writeSelfClosingTemplElementExpression(indentLevel int, n pa
return err
}
g.sourceMap.Add(n.Expression, r)
// .Render(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer)\n"); err != nil {
// .Render(ctx, templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(ctx, templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
Expand All @@ -701,8 +701,8 @@ func (g *generator) writeCallTemplateExpression(indentLevel int, n parser.CallTe
return err
}
g.sourceMap.Add(n.Expression, r)
// .Render(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer)\n"); err != nil {
// .Render(ctx, templ_7745c5c3_Buffer)
if _, err = g.w.Write(".Render(ctx, templ_7745c5c3_Buffer)\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
Expand Down Expand Up @@ -859,8 +859,8 @@ func (g *generator) writeAttributeCSS(indentLevel int, attr parser.ExpressionAtt
return
}
// Render the CSS before the element if required.
// templ_7745c5c3_Err = templ.RenderCSSItems(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer, templ_7745c5c3_CSSClassess...)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ.RenderCSSItems(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer, "+classesName+"...)\n"); err != nil {
// templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_CSSClassess...)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, "+classesName+"...)\n"); err != nil {
return
}
if err = g.writeErrorHandler(indentLevel); err != nil {
Expand Down Expand Up @@ -926,8 +926,8 @@ func (g *generator) writeElementScript(indentLevel int, n parser.Element) (err e
return
}
// Render the scripts before the element if required.
// templ_7745c5c3_Err = templ.RenderScriptItems(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer, a, b, c)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ.RenderScriptItems(templ_7745c5c3_Ctx, templ_7745c5c3_Buffer, "+strings.Join(scriptExpressions, ", ")+")\n"); err != nil {
// templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, a, b, c)
if _, err = g.w.WriteIndent(indentLevel, "templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, "+strings.Join(scriptExpressions, ", ")+")\n"); err != nil {
return err
}
if err = g.writeErrorHandler(indentLevel); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion generator/htmldiff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import (
)

func Diff(input templ.Component, expected string) (diff string, err error) {
return DiffCtx(context.Background(), input, expected)
}

func DiffCtx(ctx context.Context, input templ.Component, expected string) (diff string, err error) {
var wg sync.WaitGroup
wg.Add(2)

Expand Down Expand Up @@ -42,7 +46,7 @@ func Diff(input templ.Component, expected string) (diff string, err error) {
}()

// Render the component.
err = input.Render(context.Background(), w)
err = input.Render(ctx, w)
if err != nil {
errs = append(errs, fmt.Errorf("failed to render component: %w", err))
}
Expand Down
5 changes: 5 additions & 0 deletions generator/test-context/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ul>
<li>test</li>
<li>the if passed</li>
<li>the else if passed</li>
</ul>
26 changes: 26 additions & 0 deletions generator/test-context/render_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testcontext

import (
"context"
_ "embed"
"testing"

"github.com/a-h/templ/generator/htmldiff"
)

//go:embed expected.html
var expected string

func Test(t *testing.T) {
component := render()

ctx := context.WithValue(context.Background(), contextKeyName, "test")

diff, err := htmldiff.DiffCtx(ctx, component, expected)
if err != nil {
t.Fatal(err)
}
if diff != "" {
t.Error(diff)
}
}
19 changes: 19 additions & 0 deletions generator/test-context/template.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package testcontext

type contextKey string

var contextKeyName contextKey = "name"

templ render() {
<ul>
<li>{ ctx.Value(contextKeyName).(string) }</li>
if ctx.Value(contextKeyName).(string) == "test" {
<li>the if passed</li>
}
if ctx.Value(contextKeyName).(string) != "test" {
<li>the else if failed</li>
} else if ctx.Value(contextKeyName).(string) == "test" {
<li>the else if passed</li>
}
</ul>
}
95 changes: 95 additions & 0 deletions generator/test-context/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a8e833a

Please sign in to comment.