Href creating wrong links

Hay,
I want to create a side menu in my baseof.html with some Links in it.
everything works EXCEPT, that all the Links are always relative to the active Page.

for example: If i set href="{{ .Site.BaseURL }}" i get baseURL/BaseURL as a link on my main Page, and baseURL/subpage/BaseURL on my subpage.

Anyone got any idea how to fix this?

Thanks in advance

Silver

My short tip would be to never use .Site.BaseURL to construct your links.

Use: href="{{ .RelPermalink }}"

If you need a link to home page, do href="{{ site.Home.RelPermalink }}"

Thank you very much for your answer.

Sadly, this won’t work either. No matter what i put in for href, it will always be like “baseURL/CurrentPage/hrefinput”, instad of “baseURL/hrefinput” (or only href input).

Yes, it does. I know what I’m talking about. None of my examples produces anything remotely similar to your examples. Try it before you discard it.

It wasn’t my intention to insult you.
I indeed tried it, and it didn’t work for me.

I know it should work but for some reason it doesn’t.

It seems kind of difficult to guess why you’re getting the wrong links output, without seeing the project.

Can you share a link to a repository?
Or if not then share a dummy project that reproduces the issue.

Hay, thank you for your answer.
I Uploaded a “Stripped” version of my stuff: GitHub - Amsee-Daniel/MyTheme
The Only file that really contains stuff (And the problematic hrefs) is the baseof.html.

Basically the relevant sidebar code block from the based.html template is:

 <div class="sidenav">
    <ul>
      <li><a href="{{ .RelPermalink }}">Home</a></li>
      <p>{{ .File.Path }} <br>
        {{ "" | absURL }}
      </p>
      {{ range sort (readDir (delimit (slice "content" "") "/")) "Name" }}
      {{ if not (in .Name ".") }}
      <li>
        <a href="{{ .Name }}">{{ .Name }}</a><br>
      </li>
      {{ end }}
      {{ end }}
    </ul>
  </div>

It is not a good idea to use Local File Templates when trying to generate a list of links to recent pages residing under the contentDir.

Please have a look at the Most Recent Articles example.

Ideally you need to generate the Page links list with the range function and within its context reference the respective .RelPermalink -as mentioned above by Bjørn Erik-.

Thanks a lot.
What about this one?

It is behaving exactly the same.

With <li><a href="{{ .RelPermalink }}">Home</a></li> you are calling .RelPermalink without a relevant context. Therefore Hugo does not know what link to render and you are getting unpredictable output.

Go templates are context aware. You always need to use the link variables within a Page collection.

To generate a link to the Homepage either use:

<li><a href="/">Home</a></li>

OR

<li><a href="{{ .Site.BaseURL }}">Home</a></li>

P.S. There is no built-in variable to generate a link to the Homepage as far as I know.

Thank you SO much!
Putting a “/” in front of every Link makes them relative to root!

A quick clarifier:

“Relative” may not mean what you think it means here.

Relative, to me, in the strict sense, means, given a file tree like this:

file-1
dir/file-2

Then relative linking file-1 from dir/file-2 would look like: ../file-1

But, in the Hugo sense, Relative linking means “the domain is not included, only the path portion of the URL is included”.

Hope that helps.

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