Skip to content

euank/gotmpl

Repository files navigation

gotmpl

A stupid-simple template substitution tool.

Template language

This code supports the following three features:

  1. ${var} — a template variable which will be replaced with the value of var
  2. \$ — an escape, which will be replaced with a $
  3. \\ — an escape, which will be replaced with a \

No other special syntax is supported.

Usage

As a CLI tool

By default, the gotmpl cli tool will resolve template variables from the current environment. As an argument, it takes a file to template and prints the result to stdout.

For example:

$ cat input_file
hello ${VAR}

$ export VAR=world

$ gotmpl input_file
hello world

$ gotmpl input_file > output_file
$ cat output_file
hello world

As a library

A template may be evaluated by providing anything which satisfies the Lookup interface. The most trivial thing to use is a go map via the MapLookup:

result, err := gotmpl.TemplateString("foo is ${foo}", gotmpl.MapLookup(map[string]string{"foo": "value"}))

fmt.Println(result)
// Prints: foo is value

See Also

About

The simplest templating possible, in library and cli form

Resources

License

Stars

Watchers

Forks

Packages

No packages published