From 52295247b56175b48857d38fda6576844f43f7d1 Mon Sep 17 00:00:00 2001 From: Satoshi Fukutomi Date: Mon, 11 Jul 2022 21:55:55 +0900 Subject: [PATCH] Support raw tags in lambda. --- mustache.go | 6 +++++- mustache_test.go | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mustache.go b/mustache.go index a6cdff7..fa2b90b 100644 --- a/mustache.go +++ b/mustache.go @@ -648,7 +648,11 @@ func getElementText(element interface{}, buf io.Writer) error { case *textElement: fmt.Fprintf(buf, "%s", elem.text) case *varElement: - fmt.Fprintf(buf, "{{%s}}", elem.name) + if elem.raw { + fmt.Fprintf(buf, "{{{%s}}}", elem.name) + } else { + fmt.Fprintf(buf, "{{%s}}", elem.name) + } case *sectionElement: if elem.inverted { fmt.Fprintf(buf, "{{^%s}}", elem.name) diff --git a/mustache_test.go b/mustache_test.go index 938291e..e106ff7 100644 --- a/mustache_test.go +++ b/mustache_test.go @@ -396,6 +396,28 @@ func TestLambdaStruct(t *testing.T) { } } +func TestLambdaRawTag(t *testing.T) { + tmpl := `{{#Lambda}}Hello {{{Name}}}.{{/Lambda}}` + data := struct { + Name string + Lambda LambdaFunc + }{ + Name: "
", + Lambda: func(text string, render RenderFunc) (string, error) { + return render(text) + }, + } + + output, err := Render(tmpl, data) + if err != nil { + t.Fatal(err) + } + expect := "Hello
." + if output != expect { + t.Fatalf("TestLambdaStruct expected %q got %q", expect, output) + } +} + func TestLambdaError(t *testing.T) { tmpl := `{{#lambda}}{{/lambda}}` data := map[string]interface{}{