How to Check for Home Page?


#1

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?


#2

Same thing but check for .Site.BaseUrl?


#3

Tried that initially, but it doesn’t work.

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

{{ .Url }} gives me /


#4

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.


#5

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!


#6

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.


#7

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?


#8

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…


#9

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.


#10

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}}

#11

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


#13

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


#14

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


#15

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


#16

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.


#17

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.


#18

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.


#19

Indeed:

{{ if .IsHome }}

#20

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


#21