Insert HTML element based on the URL [solved]

Inside the navigation I would like to insert a link back to ‘home’ if the current url doesn’t equal ‘home’. My template code looks if the .Permalink of the current page is not equal to the .Site.BaseURL.

{{ if ne .Permalink .Site.BaseURL }}
        <a href='{{ .Site.BaseURL }}'> <span class="arrow">←</span>Home</a>
{{ end }}

<h1>{{ .Site.BaseURL }}</h1>
<h1>{{ .Permalink }}</h1>

Furthermore I printed both URLs for each page inside the navigation. The problem is that the link is shown in every template including home.

The weird thing is that both URLs are the same at home, as the output below shows.


Therefore the link to home should not be printed in the navigation.

1 Like

The problem was that .Site.BaseURL can’t be casted to a string that easy and a comparison with a string (.Permalink) wasn’t possible.

I found a small work around in this forum here.

The workaround looks like this:

{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" }}
{{ if ne $url "" }}
    <a href='{{ .Site.BaseURL }}'> <span class="arrow">←</span>Home</a>
{{ end }}