How to check if the site is on localhost?


I’d like to add features to the theme that are only visible when the site is in the development environment (localhost). How can I do that?

I thought about using the $.Site.BaseURL variable and checking if this variable equals or contains “localhost”. After all, this

{{ printf "%#v" $.Site.BaseURL }}



But neither in nor eq seem to work with $.Site.BaseURL:

{{ if in $.Site.BaseURL "localhost" }}
  localhost code, doesn't work
{{ end }}


{{ if eq $.Site.BaseURL "http://localhost:1313/" }}
  localhost code, also doesn't work.
{{ end }}

The docs show that these functions also work with strings, so I’m not sure what I’m doing wrong here.

BaseURL isn’t a string …

Something like this should work:

{{ if eq (printf "%#v" $.Site.BaseURL) "http://localhost:1313/" }}

Thanks Bep, does that code work for you? For me it doesn’t (Hugo v0.14 on Windows 7 SP1).

Also, do you know a good resource for learning Hugo, besides the docs? For example, I didn’t knew that BaseURL is a string, while I figure from your response I should have known that. :slight_smile:

No, I haven’t tested that construction, but a variation of that one should work.

BaseURL is of type template.URL. If you dig, that info could be found here:

Thanks, that’s a good Hugo resource for Go programmers. Not for people new to Go and Hugo, I think.

For others, I got it working with this:

{{ if eq (printf "%v" $.Site.BaseURL) "http://localhost:1313/" }}
   <!-- code for localhost only -->
{{ end }}

Thanks Bep!


As a small note beside: it could be possible that users run Hugo’s built-in server locally on another port than :1313, which could happen if they run two of them simultainiously on different ports.

What if one wants to check if the URL in this example contains ‘13’ or something else. An example would, be to add specific code on a certain pages/nodes.

In @Jura 's example the logical thing to do would be to change eq to in and the hyperlink to just 13. But that won’t work, neither will {{ if in .URL "13"}}Hugo is awesome{{end}} since ‘in’ in this case just outputs the same thing as ‘eq’. So how does one check if an url contains (and not equals) 13?

As URL isn’t a string, you would have to do:

{{ if in (print .URL) "13"}}
1 Like

Hello Bep,
This works, but only for nodes. What I’m trying to accomplish is to write a <a href="/blog/">back to blog</a> on all blogposts.

/Hugo 0.15

<a href="{{ absURL "/blog/"}}">back to blog</a>.

Something else must be off with my if code, but thanks @bep - you the man :sunglasses:

@samrich How many different layouts do you have for your blog posts? Is this as simple as just adding <a href="/blog/">Back to Blog</a> in the templating layer? The leading / should mean it’s root relative.

Better, you could also write the following:

<a href="{{.Site.BaseURL}}/blog/">Back to Blog</a> if you want the full path.

If you want to use an even more abstracted approach (eg, with a /_default/single.html), you could also do this:

<a href="{{.Site.BaseURL}}/{{.Section}}">Back to {{.Section}}</a>

You could also use the {{ whatever | pluralize }} depending on your use case.

If you are looking for something more like @Jura’s original question, I would recommend setting a param (a la env) in your config then checking for that value or modifying it with a flag (or as needed) during your dev work.

Thanks @rdwatters! I actually solved it kind of like you suggested in the last paragraph. I added a new taxonomy that will be used to identify which section in a particular language that the page adheres to. In this case I have a taxonomy called “Langid” and just put langid = “fr-blog” on all posts through an archetype.

use separated config for release

// config-release.toml
BaseURL  = ""
PublishDir = "/var/www/mysite"
   isRelease = true

// config.toml
BaseURL   = http://localhost:1313/

// layout
{{ if .Site.Params.isRelease }}
  some release code, like Google Analytics
{{ else }}
  some dev code, like logging
{{ end }}

use next commands

// to release
hugo --config ./config-release.toml

// to dev
hugo server