Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default values for variables #213

Closed
vascop opened this issue Oct 10, 2017 · 10 comments · Fixed by #298
Closed

Default values for variables #213

vascop opened this issue Oct 10, 2017 · 10 comments · Fixed by #298
Labels

Comments

@vascop
Copy link

vascop commented Oct 10, 2017

Is there a way to use a default value when a variable isn't defined?

Something like:
{{(ds "config").notes | default "this a default note")}}

@hairyhenderson
Copy link
Owner

@vascop Not in a general sense right now. For your specific example, that might result in an error if the config datasource doesn't have a notes key. I'd have to dig a little further into it to make sure.

Either way - this is a good idea! Thanks for logging this issue 🙂

@zealic
Copy link
Contributor

zealic commented Oct 24, 2017

I think we need a new func: hasKey

like hugo's isset

{{ $config := (ds "config") -}}
{{ $notes := "this a default note" -}}
{{ if hasKey $confg "notes" -}}
{{ $notes := $config.notes -}}
{{ end -}}

@hairyhenderson
Copy link
Owner

@zealic there is has/conv.Has already: https://gomplate.hairyhenderson.ca/functions/conv/#conv-has

@hairyhenderson
Copy link
Owner

And actually, that's an excellent point... @vascop will has fix your use case? What @zealic suggested should work (except use has instead of hasKey).

@vascop
Copy link
Author

vascop commented Oct 27, 2017

I think it'd probably work, though much more verbose since we'd have to do the whole "check if it has key and overwrite" dance instead of one-lining the default values.

@hairyhenderson
Copy link
Owner

@vascop understood. It's not ideal. But it might be the only option...

The challenge is that referencing an undefined key will cause Go's text/template to blow up:

$gomplate -i '{{ (json `{"foo":"bar"}`).baz }}'
panic: template: template:1:9: executing "template" at <`{"foo":"bar"}`>: map has no entry for key "baz"

goroutine 1 [running]:
main.(*Gomplate).RunTemplate(0xc42030e030, 0x7fff5fbff54c, 0x20, 0x1e95de0, 0xc42000c018)
	/go/src/github.com/hairyhenderson/gomplate/gomplate.go:31 +0x42c

My impression is that we won't be able to get that specific syntax to work without re-implementing Go's templating system.

What we can do is have a default function that will provide a value if the given input is nil or an empty string. That's not too difficult, but it wouldn't address your specific use-case.

@estahn
Copy link

estahn commented Apr 19, 2018

👍 for default, e.g. {{ .foo | default "bar" }}.

@hairyhenderson
Copy link
Owner

@estahn if . has no foo key, that particular usage will never work. In that case you'll have to always wrap it in {{ if has . "foo" }}...{{ end }}.

But I still think default would be useful for handling empty values. I'll see about implementing this soon.

@thepalbi
Copy link

Just for future reference, I'm using this to make something like that default case:

{{ has (ds "config") KEY | ternary (ds "config").KEY "default value" }}

@studioph
Copy link

studioph commented May 7, 2022

Wanted to share another option that I found works and is pretty close to the sprig version as well. I'm configuring my datasources as contexts in the config file in this case.

{{ index . "foo" | default .values.defaults.foo }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants