diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2017-03-06 20:04:15 -0500 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2017-03-06 20:47:16 -0500 |
| commit | 2adb96bdc7261389ddde0408fd9e6b689c73adc0 (patch) | |
| tree | d4a8d369fff0dd3e5a340287d640d9a0b3006470 /README.md | |
| parent | 7b6434d5784b811d4a3e5b663cfacd2716bd7db9 (diff) | |
Adding 'has' func to determine if an object has a named key
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -59,6 +59,8 @@ Gomplate is an alternative that will let you process templates which also includ - [Example](#example) - [`trim`](#trim) - [Example](#example) + - [`has`](#has) + - [Example](#example) - [`json`](#json) - [Example](#example) - [`jsonArray`](#jsonarray) @@ -389,6 +391,39 @@ $ FOO=" world " | gomplate < input.tmpl Hello, world! ``` +#### `has` + +Has reports whether or not a given object has a property with the given key. Can be used with `if` to prevent the template from trying to access a non-existent property in an object. + +##### Example + +_Let's say we're using a Vault datasource..._ + +_`input.tmpl`:_ +```go +{{ $secret := datasource "vault" "mysecret" -}} +The secret is ' +{{- if (has $secret "value") }} +{{- $secret.value }} +{{- else }} +{{- $secret | toYAML }} +{{- end }}' +``` + +If the `secret/foo/mysecret` secret in Vault has a property named `value` set to `supersecret`: + +```console +$ gomplate -d vault:///secret/foo < input.tmpl +The secret is 'supersecret' +``` + +On the other hand, if there is no `value` property: + +```console +$ gomplate -d vault:///secret/foo < input.tmpl +The secret is 'foo: bar' +``` + #### `json` Converts a JSON string into an object. Only works for JSON Objects (not Arrays or other valid JSON types). This can be used to access properties of JSON objects. |
