Skip to content

Commit

Permalink
Allow overriding the template delimiters
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Mar 7, 2017
1 parent 2adb96b commit 1816384
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Gomplate is an alternative that will let you process templates which also includ
- [Usage](#usage)
- [Commandline Arguments](#commandline-arguments)
- [`--datasource`/`-d`](#-datasource-d)
- [Overriding the template delimiters](#overriding-the-template-delimiters)
- [Syntax](#syntax)
- [About `.Env`](#about-env)
- [Built-in functions](#built-in-functions)
Expand Down Expand Up @@ -191,6 +192,11 @@ A few different forms are valid:
- `mydata.json`
- This form infers the name from the file name (without extension). Only valid for files in the current directory.

#### Overriding the template delimiters

Sometimes it's necessary to override the default template delimiters (`{{`/`}}`).
Use `--left-delim`/`--right-delim` or set `$GOMPLATE_LEFT_DELIM`/`$GOMPLATE_RIGHT_DELIM`.

## Syntax

### About `.Env`
Expand Down
26 changes: 22 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ func (g *Gomplate) createTemplate() *template.Template {

// Gomplate -
type Gomplate struct {
funcMap template.FuncMap
funcMap template.FuncMap
leftDelim string
rightDelim string
}

// RunTemplate -
Expand All @@ -30,7 +32,7 @@ func (g *Gomplate) RunTemplate(in io.Reader, out io.Writer) {
if err != nil {
log.Fatalf("Read failed!\n%v\n", err)
}
tmpl, err := g.createTemplate().Parse(string(text))
tmpl, err := g.createTemplate().Delims(g.leftDelim, g.rightDelim).Parse(string(text))
if err != nil {
log.Fatalf("Line %q: %v\n", string(text), err)
}
Expand All @@ -41,12 +43,14 @@ func (g *Gomplate) RunTemplate(in io.Reader, out io.Writer) {
}

// NewGomplate -
func NewGomplate(data *Data) *Gomplate {
func NewGomplate(data *Data, leftDelim, rightDelim string) *Gomplate {
env := &Env{}
typeconv := &TypeConv{}
ec2meta := aws.NewEc2Meta()
ec2info := aws.NewEc2Info()
return &Gomplate{
leftDelim: leftDelim,
rightDelim: rightDelim,
funcMap: template.FuncMap{
"getenv": env.Getenv,
"bool": typeconv.Bool,
Expand Down Expand Up @@ -81,8 +85,10 @@ func NewGomplate(data *Data) *Gomplate {
func runTemplate(c *cli.Context) error {
defer runCleanupHooks()
data := NewData(c.StringSlice("datasource"))
lDelim := c.String("left-delim")
rDelim := c.String("right-delim")

g := NewGomplate(data)
g := NewGomplate(data, lDelim, rDelim)
g.RunTemplate(os.Stdin, os.Stdout)
return nil
}
Expand All @@ -99,6 +105,18 @@ func main() {
Name: "datasource, d",
Usage: "Data source in alias=URL form. Specify multiple times to add multiple sources.",
},
cli.StringFlag{
Name: "left-delim",
Usage: "Override the default left-delimiter `{{`",
Value: "{{",
EnvVar: "GOMPLATE_LEFT_DELIM",
},
cli.StringFlag{
Name: "right-delim",
Usage: "Override the default right-delimiter `}}`",
Value: "}}",
EnvVar: "GOMPLATE_RIGHT_DELIM",
},
}

err := app.Run(os.Args)
Expand Down
11 changes: 10 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestBoolTemplates(t *testing.T) {
func TestEc2MetaTemplates(t *testing.T) {
createGomplate := func(status int, body string) (*Gomplate, *httptest.Server) {
server, ec2meta := aws.MockServer(status, body)
return &Gomplate{template.FuncMap{"ec2meta": ec2meta.Meta}}, server
return &Gomplate{funcMap: template.FuncMap{"ec2meta": ec2meta.Meta}}, server
}

g, s := createGomplate(404, "")
Expand Down Expand Up @@ -142,3 +142,12 @@ func TestHasTemplate(t *testing.T) {
{{- end }}`
assert.Equal(t, "bar", testTemplate(g, tmpl))
}

func TestCustomDelim(t *testing.T) {
g := &Gomplate{
leftDelim: "[",
rightDelim: "]",
funcMap: template.FuncMap{},
}
assert.Equal(t, "hi", testTemplate(g, `[print "hi"]`))
}

0 comments on commit 1816384

Please sign in to comment.