diff --git a/README.md b/README.md index d1e23cfb..b9418d28 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,7 @@ For example, this is a JSON version of an emitted RuntimeContainer struct: * *`keys $map`*: Returns the keys from `$map`. If `$map` is `nil`, a `nil` is returned. If `$map` is not a `map`, an error will be thrown. * *`last $array`*: Returns the last value of an array. * *`parseBool $string`*: parseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error. Alias for [`strconv.ParseBool`](http://golang.org/pkg/strconv/#ParseBool) +* *`read $string`*: Read the content of the file located at `$path` * *`replace $string $old $new $count`*: Replaces up to `$count` occurences of `$old` with `$new` in `$string`. Alias for [`strings.Replace`](http://golang.org/pkg/strings/#Replace) * *`sha1 $string`*: Returns the hexadecimal representation of the SHA1 hash of `$string`. * *`split $string $sep`*: Splits `$string` into a slice of substrings delimited by `$sep`. Alias for [`strings.Split`](http://golang.org/pkg/strings/#Split) diff --git a/template.go b/template.go index 793396d5..fc45582e 100644 --- a/template.go +++ b/template.go @@ -31,6 +31,21 @@ func exists(path string) (bool, error) { return false, err } +func read(path string) (string, error) { + _, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + return "", nil + } + return "", err + } + b, err := ioutil.ReadFile(path) + if err != nil { + return "", err + } + return string(b), nil +} + func getArrayValues(funcName string, entries interface{}) (*reflect.Value, error) { entriesVal := reflect.ValueOf(entries) @@ -426,6 +441,7 @@ func newTemplate(name string) *template.Template { "dict": dict, "dir": dirList, "exists": exists, + "read": read, "first": arrayFirst, "groupBy": groupBy, "groupByKeys": groupByKeys,