Issue using printf with BaseURL variable

I am running hugo locally. I have the following:

{{ $base := .Site.BaseURL }}
<a {{ printf "href=%q" $base | safeHTMLAttr }}>foo</a>
<a {{ printf "href=%q" .Site.BaseURL | safeHTMLAttr }}>bar</a>

the first href results in %!q(<nil, the second one in http://localhost:1313/.

Why is the first href incorrect?

I’m not sure what you are trying to achieve with this variants. If you want to get the absolute URL to your homepage you might want to use:

<a href="{{ absURL "" }}">Home</a>

nevertheless your code produces the same output for me:

as a guess I would think these code l,ines are just copied from different places in your template(s) having other code around and the _CONTEXT-DOT at .Site is different.

While the above is recommended over the original poster, I would highly recommend:

<a href="{{ site.Home.Permalink }}">Home</a>

just curious

because it’s easier to see what it does (agreed) or … speed, stability , problems?

Well, it’s 1) less room for errors (you get an error if you e.g. type site.Shome) 2) I think in general it’s a good practise to use the Page object for linking/navigation, which becomes more obvious in more complex examples.

1 Like

which becomes more obvious in more complex examples

One of these more complex examples is “permalinkable” output formats.