# Issue using printf with BaseURL variable

**URL:** <https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264>\
**Category:** support\
**Created:** [April 8, 2025, 8:20am UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264 "2025-04-08T08:20:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paolo](https://avatars.discourse-cdn.com/v4/letter/p/6f9a4e/32.png) [@Paolo](https://discourse.gohugo.io/u/Paolo)\
**Post date:** [April 8, 2025, 8:20am UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264/1 "2025-04-08T08:20:23Z")

</div>

I am running hugo locally. I have the following:

```auto
{{ $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?

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [April 8, 2025, 9:20am UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264/2 "2025-04-08T09:20:43Z")

</div>

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:

```auto
<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.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 8, 2025, 12:15pm UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264/3 "2025-04-08T12:15:29Z")

</div>

> [@irkode](#):
>
> `<a href="{{ absURL "" }}">Home</a>`

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

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

```

---

<div class="post-metadata">

**Author:** ![irkode](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/irkode/32/26135_2.png) [@irkode](https://discourse.gohugo.io/u/irkode)\
**Post date:** [April 8, 2025, 12:20pm UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264/4 "2025-04-08T12:20:44Z")

</div>

just curious

> [@bep](#):
>
> highly recommend

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

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [April 8, 2025, 12:26pm UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264/5 "2025-04-08T12:26:56Z")

</div>

> [@irkode](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [April 8, 2025, 5:06pm UTC](https://discourse.gohugo.io/t/issue-using-printf-with-baseurl-variable/54264/6 "2025-04-08T17:06:44Z")

</div>

> which becomes more obvious in more complex examples

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