Custom Hugo Figure Snippet

Hugo is a really nice way to write a blog - your writing is stored as plain text and by using Markdown you can introduce some nice basic HTML formatting. Markdown allows you to have HTML inside the same file by adding the following flag to your config.toml file:


However, if you want to use Hugo’s functions inside HTML which is inside your Markdown file, that is not allowed. Enter, Hugo snippets ! In your theme (mine is called hugo-finn) create the directory layouts/shortcodes/ and inside this directory create a snippet file (mine is called figure.html).

These snippet files are special because any Hugo functions inside the snippet are parsed, before the snippet itself is parsed into the Markdown file. Read the full shortcode documentation here.

Here is how I call my snippet, where src, title, and author are the arguments passed to the figure.html snippet.

{{< figure src="" title="" author="" >}}

And, here is how the snippet works, where the main takeaway is {{ .Get "src" }} is how to get an argument input!

    <a href='{{ .Get "src" | absURL }}' title='Full size image: {{ .Get "title" }}'>
      {{ .Get "src" | absURL }} 4096w,
      {{ .Get "src" | absURL }} 2048w,
      {{ .Get "src" | absURL }} 1024w,
      {{ .Get "src" | absURL }} 512w,'
            src='{{ .Get "src" | absURL }}'
            alt='{{ .Get "title" }}'
    <figcaption>(📷: {{ .Get "author" }}) {{ .Get "title" }}</figcaption>

Pretty sweet! I’ve even got :figure mapped as a text expansion shortcut so I don’t have to remember the syntax each time! Click through to read about responsively serving images with srcset, or click through to read about my image resizing service, AutoImageResizer, head over here!

Happy developing!

– Finn 👋