diff --git a/template.go b/template.go index d0fadb8..76ce7df 100644 --- a/template.go +++ b/template.go @@ -18,6 +18,7 @@ func truthy(val interface{}) bool { } // Templater for custom emails. +// Slices "variables", "conditionals", and map "values" should NOT wrap names in { and }. // Variables should be written as {varName}. // If statements should be written as {if (!)varName}...{endif}. // Strings are true if != "", ints are true if != 0. @@ -95,8 +96,7 @@ func templateEmail(content string, variables []string, conditionals []string, va positive = false varName = varName[1:] } - wrappedVarName := "{" + varName + "}" - validVar := slices.Contains(conditionals, wrappedVarName) + validVar := slices.Contains(conditionals, varName) if validVar { ifTrue = positive == truthy(values[varName]) } else { @@ -123,10 +123,9 @@ func templateEmail(content string, variables []string, conditionals []string, va if ifStart != -1 { continue } - wrappedVarName := "{" + varName + "}" - validVar := slices.Contains(variables, wrappedVarName) + validVar := slices.Contains(variables, varName) if !validVar { - out += wrappedVarName + out += "{" + varName + "}" continue } out += fmt.Sprint(values[varName]) diff --git a/template_test.go b/template_test.go index 465bc2b..b6bf1d0 100644 --- a/template_test.go +++ b/template_test.go @@ -23,7 +23,7 @@ func TestBlankTemplate(t *testing.T) { func testConditional(isTrue bool, t *testing.T) { in := `Success, {username}! Your account has been created. {if myCondition}Log in at {myAccountURL} with username {username} to get started.{endif}` - vars := []string{"{username}", "{myAccountURL}", "{myCondition}"} + vars := []string{"username", "myAccountURL", "myCondition"} conds := vars vals := map[string]any{ "username": "TemplateUsername", @@ -64,7 +64,7 @@ func TestConditionalFalse(t *testing.T) { func TestTemplateDoubleBraceGracefulHandling(t *testing.T) { in := `Success, {{username}}! Your account has been created. Log in at {myAccountURL} with username {username} to get started.` - vars := []string{"{username}", "{myAccountURL}"} + vars := []string{"username", "myAccountURL"} vals := map[string]any{ "username": "TemplateUsername", "myAccountURL": "TemplateURL", @@ -87,16 +87,16 @@ func TestTemplateDoubleBraceGracefulHandling(t *testing.T) { func TestVarAtAnyPosition(t *testing.T) { in := `Success, user! Your account has been created. Log in at myAccountURL with your username to get started.` - vars := []string{"{username}", "{myAccountURL}"} + vars := []string{"username", "myAccountURL"} vals := map[string]any{ "username": "TemplateUsername", "myAccountURL": "TemplateURL", } for i := range in { - newIn := in[0:i] + vars[0] + in[i:] + newIn := in[0:i] + "{" + vars[0] + "}" + in[i:] - target := strings.ReplaceAll(newIn, vars[0], vals["username"].(string)) + target := strings.ReplaceAll(newIn, "{"+vars[0]+"}", vals["username"].(string)) out, err := templateEmail(newIn, vars, []string{}, vals) @@ -105,7 +105,7 @@ func TestVarAtAnyPosition(t *testing.T) { } if out != target { - t.Fatalf(`returned string doesn't match desired output: "%+v" != "%+v"`, out, target) + t.Fatalf(`returned string doesn't match desired output: "%+v" != "%+v, from "%+v""`, out, target, newIn) } } }