Last active
May 24, 2016 22:05
-
-
Save teepark/390374afc6984d4032b32dcba10953dd to your computer and use it in GitHub Desktop.
whitespace-eating tags backport wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build !go1.6 | |
package main | |
import ( | |
"regexp" | |
"text/template" | |
) | |
func parseTemplate(tmpl *template.Template, input string) (*template.Template, error) { | |
input = wsPrefix.ReplaceAllString(input, "{{ ") | |
input = wsSuffix.ReplaceAllString(input, " }}") | |
return tmpl.Parse(input) | |
} | |
var ( | |
wsPrefix = regexp.MustCompile(`\s*{{- `) | |
wsSuffix = regexp.MustCompile(` -}}\s*`) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build go1.6 | |
package main | |
import "text/template" | |
func parseTemplate(tmpl *template.Template, input string) (*template.Template, error) { | |
return tmpl.Parse(input) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"testing" | |
"text/template" | |
) | |
func TestWhitespaceRemoved(t *testing.T) { | |
tmpl, err := parseTemplate(template.New(""), "\t{{- .VAL -}} \n , text") | |
if err != nil { | |
t.Fatalf("error parsing template: %s", err) | |
} | |
buf := new(bytes.Buffer) | |
err = tmpl.Execute(buf, map[string]string{"VAL": "value"}) | |
if err != nil { | |
t.Errorf("execute error: %s", err) | |
} | |
expected := "value, text" | |
got := buf.String() | |
if got != expected { | |
t.Errorf("rendered string: expected %q, got %q", expected, got) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment