Can I reference BaseURL from content?

Hey all!
Is there a way to reference static content (e.g. images) dynamically like you would in a template? i.e. using

{{ .Site.BaseURL }})

As far as I understand the default way is to just go for

img src=“assets/images/myimg.jpg”

This works just fine if the directory of the HTML-page never changes. In my case I worry 2 reasons for URL changes:
(1) moving from current uglyURLs to pretty ones
(2) going for the - hopefully released in 0.16 - multilang features which will move my page into e.g. en/page.html

If there’s no way it’s of course not the end of the world to fix these references if the page-directory ever changes.

Thanks all!
Omni

You would need to use shortcodes for that.

Perfect, Thanks a lot!

If anyone else wants to use this:
I created the file

layouts/shortcodes/baseurl.html

with the following content:

{{ .Page.Site.BaseURL }}

and can use it in the content as follows:

{{< baseurl >}}

which outputs (in local deployment):

http://localhost:1313/

I also think this is quite helpful if you want to reference static content from e.g. news articles which might end up in /year/month/day directory. It would save you the counting of “…/…/…/”.
Or is that another misunderstanding on my side?

Thanks again for the quick help =)
Omni

5 Likes

Thank you for taking the time to post your solution, this really helped a lot.

This solved one of my major issues, thanks.