Accessing .Site.BaseURL from within render-link.html

I’m trying to check if .Destination starts with the base URL of my site with the “render-link.html” render hook

(strings.HasPrefix .Destination .Site.BaseURL)

but get an error:

execute of template failed at <.Site.BaseURL>: can’t evaluate field Site in type goldmark.linkContext 

According to the documentation the error is correct, as the render link doesn’t receive the .Site context.

Still trying to avoid just pasting the baseURL as string into the render hook - is there a trick to get .Site context where I need / want it?

The render hook receives the page context, which includes the site context. Either of these will work:

{{ site.BaseURL }}
{{ .Page.Site.BaseURL }}

Get in the habit of using the first one (the site function).

1 Like


Just out of curiosity: why should I favour the first one?

So you don’t run into context problems (e.g., the subject of the topic)

Ok, understood :grinning:

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