A go template renderer based on Perl's Template Toolkit

feat: make blocks defined in wrapper content available to wrapper template

Changed files
+54
+10
eval.go
··· 297 297 for name, block := range e.blocks { 298 298 contentEval.blocks[name] = block 299 299 } 300 + // Collect block definitions from wrapper content so they're available to the wrapper 301 + for _, node := range n.Content { 302 + if block, ok := node.(*BlockStmt); ok { 303 + contentEval.blocks[block.Name] = block 304 + } 305 + } 300 306 for _, node := range n.Content { 301 307 if err := contentEval.evalNode(node); err != nil { 302 308 return err ··· 344 350 345 351 wrapperEval := NewEvaluator(e.renderer, wrapperVars) 346 352 for name, block := range e.blocks { 353 + wrapperEval.blocks[name] = block 354 + } 355 + // Include blocks defined in wrapper content 356 + for name, block := range contentEval.blocks { 347 357 wrapperEval.blocks[name] = block 348 358 } 349 359 result, err := wrapperEval.Eval(tmpl)
+44
gott_test.go
··· 895 895 } 896 896 } 897 897 898 + // TestBlockInWrapperContent tests that blocks defined inside wrapper content are available to the wrapper 899 + func TestBlockInWrapperContent(t *testing.T) { 900 + memFS := fstest.MapFS{ 901 + "layout.html": &fstest.MapFile{Data: []byte("HEADER:[% INCLUDE header %]CONTENT:[% content %]")}, 902 + } 903 + 904 + r, err := New(&Config{ 905 + IncludePaths: []fs.FS{memFS}, 906 + }) 907 + if err != nil { 908 + t.Fatalf("New() error = %v", err) 909 + } 910 + 911 + tests := []struct { 912 + name string 913 + template string 914 + vars map[string]any 915 + want string 916 + }{ 917 + { 918 + name: "block defined in wrapper content", 919 + template: "[% WRAPPER layout.html %][% BLOCK header %]MyHeader[% END %]BodyContent[% END %]", 920 + want: "HEADER:MyHeaderCONTENT:BodyContent", 921 + }, 922 + { 923 + name: "multiple blocks in wrapper content", 924 + template: "[% WRAPPER layout.html %][% BLOCK header %]H1[% END %][% BLOCK footer %]F1[% END %]Body[% END %]", 925 + want: "HEADER:H1CONTENT:Body", 926 + }, 927 + } 928 + 929 + for _, tt := range tests { 930 + t.Run(tt.name, func(t *testing.T) { 931 + got, err := r.Process(tt.template, tt.vars) 932 + if err != nil { 933 + t.Fatalf("Process() error = %v", err) 934 + } 935 + if got != tt.want { 936 + t.Errorf("Process() = %q, want %q", got, tt.want) 937 + } 938 + }) 939 + } 940 + } 941 + 898 942 // TestTryCatchWithDynamicPaths tests the combination of TRY/CATCH with dynamic paths 899 943 func TestTryCatchWithDynamicPaths(t *testing.T) { 900 944 memFS := fstest.MapFS{