hugohugo-shortcode

Use variable inside Hugo content


I'm trying to use a variable within the content of a Hugo statically generated site. For example, the content looks like the following:

  1. Go to your site's url ({{ .Site.BaseURL }})
  2. Enter your credentials
  3. .....(blah blah blah)

When this gets rendered, the {{ .... }} part doesn't get processed...it stays the same as I put above. I've tried it with a $ in front as well. Variables within templates seem to work just fine. Do I need to create a shortcode to use within content pages?


Solution

  • So it looks like a shortcode is the way to do this. For what it's worth, I changed the document to look like the following:

    1. Go to your site's url ({{< siteurl >}})

    In layouts/shortcodes, I created the file siteurl.html. It looks like the following:

    {{ .Page.Site.BaseURL }}
    

    I needed to add .Page in there to get access to the Site variables. See this Issue Report for more details.