How do I access configuration settings in a template (.e.g $HUGO_uglyurls )?

I need to make changes based on if I’m running local or on a server. I want to know if a setting is set to something. How do I access all settings from a template ? Something along the lines of this…

{{ if eq .HUGO_uglyurls true}}
{{/* Run code to set my site for local use */}}
{{end}}

For now I have been using this as a workaround. Basically, I set in a make file params local to true or false. In the template I do this.

{{$local := .Site.Params.local}}
{{if eq $local true}}
{{/* add index.html where needed (e.g., posts) */}}
{{end}}

I set it in the make file like this.

local:
env HUGO_PARAMS='{"local":true}' HUGO_uglyurls=true \
hugo -D --verbose --baseURL="$(LOCAL_SITE)"

In general, you cannot access Hugo’s general settings from a template.

Is there a specific reason you are checking the uglyURLS config to figure out where you are running?

You can use different config files/directories to correspond to your running environment: Configure Hugo | Hugo

and you can check which environment is currently running: Hugo-specific Variables | Hugo

Also, if local vs server == running dev server hugo server vs building site via hugo, you can test site.IsServer: Site Variables | Hugo

Thanks guys. I just wanted to make the site local when needed to add index.html where needed. That will work ‘IsServer’. I was reading the docs here and an example uses hugo --uglyurls but that does not seem work. I get Error: unknown flag: --uglyURLs. Should I just continue setting configurations from my make file like this?

local:
env HUGO_PARAMS='{"local":true}' HUGO_uglyurls=true \
hugo -D --verbose  --baseURL="$(LOCAL_SITE)"

server:
env HUGO_PARAMS='{"local":false}' HUGO_baseURL="$SERVER_SITE" \
hugo -D --verbose server

Or are there flags I can use like --uglyURLs=true --relativeURLs=true etc… I did not see the --uglyURLs flag in the man page or help?

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