diff --git a/template_nest.go b/template_nest.go index e484e39..0429fae 100644 --- a/template_nest.go +++ b/template_nest.go @@ -15,15 +15,17 @@ type Hash map[string]interface{} // Option holds configuration for TemplateNest type Option struct { Delimiters [2]string - NameLabel string - TemplateDir string - TemplateExtension string - DieOnBadParams bool + NameLabel string // Identify the template to be used + TemplateDir string // directory where templates are located + TemplateExtension string // appended on the label to idenfity the template + DieOnBadParams bool // attempt to populate a variable that doesn't exist should result in an error + ShowLabels bool // Prepend & Append a string to every template, helpful in debugging + CommentDelimiters [2]string // Used in conjunction with ShowLabels, if HTML then use '' } type TemplateNest struct { option Option - cache map[string]TemplateFileIndex + cache map[string]TemplateFileIndex } // TemplateFileIndex represents an indexed template file. @@ -60,6 +62,9 @@ func New(opts Option) (*TemplateNest, error) { if opts.Delimiters == [2]string{} { opts.Delimiters = [2]string{""} } + if opts.CommentDelimiters == [2]string{} { + opts.CommentDelimiters = [2]string{""} + } if opts.NameLabel == "" { opts.NameLabel = "TEMPLATE" } @@ -70,7 +75,7 @@ func New(opts Option) (*TemplateNest, error) { // Initialize TemplateNest with the final options. nest := &TemplateNest{ option: opts, - cache: make(map[string]TemplateFileIndex), + cache: make(map[string]TemplateFileIndex), } // Walk through the template directory and index the templates. @@ -271,6 +276,18 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) { rendered = rendered[:variable.StartPosition] + replacement + rendered[variable.EndPosition:] } + if nest.option.ShowLabels { + labelStart := fmt.Sprintf( + "%s BEGIN %s %s\n", + nest.option.CommentDelimiters[0], tName, nest.option.CommentDelimiters[1], + ) + labelEnd := fmt.Sprintf( + "%s END %s %s\n", + nest.option.CommentDelimiters[0], tName, nest.option.CommentDelimiters[1], + ) + rendered = labelStart + rendered + labelEnd + } + return strings.TrimSpace(rendered), nil default: diff --git a/tests/03_show_labels_test.go b/tests/03_show_labels_test.go new file mode 100644 index 0000000..4e2c95a --- /dev/null +++ b/tests/03_show_labels_test.go @@ -0,0 +1,68 @@ +package tests + +import ( + "git.virtual.blue/tomgracey/template-nest-go" + "github.com/stretchr/testify/assert" + "testing" +) + +func TestRenderWithShowLabels(t *testing.T) { + nest, err := templatenest.New(templatenest.Option{ + TemplateDir: "templates", + ShowLabels: true, + }) + if err != nil { + t.Fatalf("Failed to initialize TemplateNest: %+v", err) + } + + nestNoLabels, err := templatenest.New(templatenest.Option{TemplateDir: "templates"}) + if err != nil { + t.Fatalf("Failed to initialize TemplateNest: %+v", err) + } + + page := templatenest.Hash{ + "TEMPLATE": "00-simple-page", + "variable": "Simple Variable", + "simple_component": templatenest.Hash{ + "TEMPLATE": "01-simple-component", + "variable": "Simple Variable in Simple Component", + }, + } + outputPage := templatenest.Hash{"TEMPLATE": "output/04-simple-page-with-labels"} + + render := nest.MustRender(page) + outputRender := nestNoLabels.MustRender(outputPage) + + assert.Equal(t, outputRender, render, "Rendered output does not match expected output") +} + +func TestRenderWithShowLabelsAltDelimiters(t *testing.T) { + nest, err := templatenest.New(templatenest.Option{ + TemplateDir: "templates", + ShowLabels: true, + CommentDelimiters: [2]string{""}, + }) + if err != nil { + t.Fatalf("Failed to initialize TemplateNest: %+v", err) + } + + nestNoLabels, err := templatenest.New(templatenest.Option{TemplateDir: "templates"}) + if err != nil { + t.Fatalf("Failed to initialize TemplateNest: %+v", err) + } + + page := templatenest.Hash{ + "TEMPLATE": "00-simple-page", + "variable": "Simple Variable", + "simple_component": templatenest.Hash{ + "TEMPLATE": "01-simple-component", + "variable": "Simple Variable in Simple Component", + }, + } + outputPage := templatenest.Hash{"TEMPLATE": "output/05-simple-page-with-labels-alt-delims"} + + render := nest.MustRender(page) + outputRender := nestNoLabels.MustRender(outputPage) + + assert.Equal(t, outputRender, render, "Rendered output does not match expected output") +}