How do I get environment variables in the Template?

I would like to append a sha1 to the css file everytime I modify it, so that I am not served an old version by a cdn.

the way I would like to do this is to pass the sha1 as an environment variable and use that in generating the HTML in header template.

Can some one tell me how can I access the environment variable in the template.

Not currently possible, I think.

But a template function that wraps os.Getenv would be easy to add I guess. If you know Go (or want to learn, it’s fun!), a PR might be the fastest approach.


I don’t mind adding this. I have been looking and I don’t understand the codebase yet.

So I have a few questions

  • Why should I add the method to the Template ? Is is object wrapped by a template before it is passed down ?
  • I would think the method would need to be present on Site/Page/Node to be available in the template. am I wrong ?

This is, in my head, a general purpose template function, not connected to Page or anything.


If you look at the init() in template.go - this is where the custom Hugo template functions are made available to the templates via the funcMap.

For what I understand, using HUGO_TITLE as an environment variable will work because there is a title field in the configuration file.

It does not seem to work with unknown configuration keys: I tried HUGO_PARAMS_SOMETHING=1 and it does not seem to be picked up in the templates.

However the getenv function can be used in the templates to collect a random environment variable. Combined with default, it does the job well.

1 Like

I am new to hugo, here is the syntax

<script type=“text/javascript” src=’/app/global.bundle.{{getenv “HUGO_HASHNEW”}}.js’></script>

1 Like

With CDN you have to change the file names only for new files!

You can use this for CSS and JS!