Skip to content

Instantly share code, notes, and snippets.

@arkadianriver
Last active July 30, 2018 09:12
Show Gist options
  • Save arkadianriver/ed60a3c861a2e97045ac9609031bb16f to your computer and use it in GitHub Desktop.
Save arkadianriver/ed60a3c861a2e97045ac9609031bb16f to your computer and use it in GitHub Desktop.
Why no variable access in Hugo posts and pages like in Jekyll? Oh well, let's write a shortcode to pass 'em through...

Enabling variables in hugo posts

...using a shortcode.

Say you maintain a Keywords file in your data folder data/keywords.yml with some contents:

catname: Frisky
dogname: Spot
pets: |
  Pets are very
  silly animals
  aren't they?

Add this shortcode to layouts/shortcodes/kwd.html:

{{ $key := .Get 0 }}{{ $val := index .Site.Data.keywords $key }}{{ $val }}

Now, in your posts, you can easily access those keyword variables:

That crazy cat, {{% kwd catname %}}, just lept up and pounced right
on {{% kwd dogname %}}, the poor pooch. {{% kwd pets %}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment