Skip to content
Regis Philibert edited this page Dec 30, 2021 · 8 revisions

HUGE/Env is responsible of everything environment. Its public functions are available to any user.

About Environment logic

HUGE/Env relies on its huge/env/Get function detailed below to determine the environment. As is, it's just a wrapper for hugo.Environment.

Quick hugo.Environment aside

  • hugo.Environment returns the value of
  • the --environment (or -e) flag set through Hugo CLI or,
  • if not set, HUGO_ENV or HUGO_ENVIRONMENT environment variable.
  • If none of the above are set it returns development in server mode,
  • and production in build mode.

If the project relies on another logic to determine its environment, one can add its own partial at layouts/partials/huge/env/Get, it will overrides HUGE's.

Override example

Let's say the project relies on NODE_ENV for determining environment.

{{/* /layouts/partials/huge/env/Get */}}
{{ $env := hugo.Environment }}
{{ with getenv "NODE_ENV" }}
  {{ $env = . }}
{{ end }}
{{ return $env }}

Now huge/env/Get and other HUGE/Env functions will also rely on NODE_ENV to evaluate.

Functions

huge/env/Get

Lengthily discussed above.

Parameters

Any

Returns

A string representing the current environment. Logic is discussed above.

Example

{{ if eq (partial "huge/env/Get") "development" }}
  {{ $debug = true }}
{{ end }}

huge/env/Is

Parameter

A string.

Returns

A boolean, true if the given string matches the current environment, false if not.

Example

{{ if partial "huge/env/Is" "production" }}
  {{ $ga = true}}
{{ end }}

huge/env/IsNot

Parameter

A string.

Returns

A boolean, true if the given string does not match the current environment, false if it does.

Example

{{ if partial "huge/env/IsNot" "production" }}
  {{ $debug = true }}
{{ end }}

huge/env/IsProduction

Parameter

Any

Returns

A boolean, true if the current environment matches production, false if not.

Example

{{ if partial "huge/env/IsProduction" "production" }}
  {{ $debug = true }}
{{ end }}

huge/env/When

This is handy when you need to do something always or never or only in one given environment.

Parameter

A string which must be among a list of:

  • always
  • never
  • {environment}: A string representing one of the project's environments.

Returns

A boolean: true if the given parameter is always or matches the current environment. false if the given parameter is never or do not match the current environment.

Clone this wiki locally