This used to work - now I'm confused

Sorry, been a while since I updated Hugo. There appear to have been a LOT of breaking changes.

The following used to work nicely but now generates the can't give argument to non-function getenv error.

{{- $e := (getenv "HUGO_ENV") | (getenv "ENVIRONMENT") | default .Site.Params.env | default "DEV" -}}

I kind of assume that I need some kind of nested set of (or ....) statements but the logic escapes me.

Can someone please lend a hand?

1) Override security config to allow access to ENVIRONMENT

[security.funcs]
getenv = ['^HUGO_', '^CI$', '^ENVIRONMENT$']

2) Use the or operator

{{ $e := or (getenv "HUGO_ENV") (getenv "ENVIRONMENT") site.Params.env "DEV" }}

The or operator returns the first truthy argument. If all arguments are falsy, returns the last argument.

And just to add to the mix, there’s also the hugo.Environment template function:

the current running environment as defined through the --environment cli tag

1 Like

:+1:

Thank you so much - that worked!

Time to get back to giving some much needed attention to my blog.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.