How to Check for Home Page?

It seems there should be an easy way with Go templates to check for being on the home page. If the home page is the root, this works: {{ if eq .Url "/" }} But of course this doesn’t work if the domain root isn’t the root of the hugo site. Ideas?

Same thing but check for .Site.BaseUrl?

Tried that initially, but it doesn’t work.

{{ .Site.BaseUrl }} gives me localhost:1313

{{ .Url }} gives me /

My first thought was

{{ if $.IsNode }} "Home" {{ else }} "not Home" {{ end }}

But the home page isn’t the only page that’s a node. Then I thought to put a flag in layouts/index.html, since that’s used only by the home page.

{{ $isHome := true }}

Then check the flag in the partial:

{{ if $isHome }} "Home" {{ else }} "Not Home" {{ end }}

That’s not working - I can’t figure out how to access a variable inside a partial.

1 Like

I also tried your second approach @michael_henderson (setting a variable on layouts/index.html) but ran into the same problem of not being able to call it inside of a partial.

Crazy how challenging such a seemingly simple task is proving to be!

This works for my home page but NOT my section files (eg, post/index.html).

{{ if and ($.IsNode) (eq $.Section "") }} "Home" {{ else }} "not Home" {{ end }}

Don’t understand why .Section and .Type aren’t passed to the partial for nodes.

I can’t remember why, but this worked for my use case at the time. Though I’m not sure it’ll be any help for anyone else…

{{ $title := (.Title | title ) }}
{{ $siteTitle := (.Site.Title) }}

{{ if eq $siteTitle $title }} {{ .Site.Params.desc.home }} {{ end }}

I’m not even sure how this works: I can only guess that Hugo uses .Site.Title for the homepage .Title?

1 Like

My idea was to compare .Permalink to (add .Site.BaseUrl "/") but it didn’t work.
I found out that comparing them to their own raw value also fails.
In my case:
{{ eq .Site.BaseUrl "http://localhost:1313/homepage" }} // => false
{{ eq .Permalink "http://localhost:1313/homepage/" }} // => false
I guess hugo compares the objects and not their value here.
It works when both fields are primitive values like here:
{{ eq .Title .Site.Title }} // => true
A workaround is to fource converting both values to strings first:
{{ eq (add .Site.BaseUrl "/") (add .Permalink "") }} // => true

I am not that familiar with Go but it should be straight forward to fix the Eq( ) function…

We added a quick comparison function using reflect.DeepEquals. Since we did that, the golang template package has added their own.

I imagine at this point we would simply need to remove ours and use theirs.

As a temporary workaround I tried @jorin’s approach… all seemed well until I realized that it silently broke live reloading (and Hugo/Go seemed to not understand the logic).

Specifically, I tried this:

{{ if eq (add .Site.BaseUrl "/") (add .Permalink " ") }}
    Home page only stuff here...  
{{end}}

I copy&pasted your code. works for me as soon as I remove the space " " ›› "".

Thanks for the follow-up… unfortunately that still doesn’t work :frowning:

Do you use the latest version of hugo?
(What gives you hugo version?)

Yes, I’m using the latest, v0.12

I am having the same (I think) issue as @isaac - If I insert this code at the beginning of my head.html partial:

{{ $isHomepage := (eq (add .Site.BaseUrl "/") (add .Permalink "")) }}

…the whole partial stops working - returns no output at all.

Also latest Hugo version (v0.12) here.

With Hugo version Hugo Static Site Generator v0.15DEV BuildDate: 2015-05-26T15:20:41+09:00

This works:

{{ .Title }}{{ if eq (add .Site.BaseUrl "/") (add .Permalink "/") }} Home Page Stuff{{ end }}

Had to add a / after .Permalink.

This should work in 0.14 with any baseURL …

{{ if eq (relURL .URL) ("/" | safeHTML) }}HOME SWEET HOME{{ end }}

Which opens up a question … there should really be a more straight forward way.

Indeed:

{{ if .IsHome }}
7 Likes

Just tested, and can confirm that works. Thanks, @bep :slight_smile:

1 Like