Hugo Shortcodes: A Practical Guide
Contents
Hugo shortcodes are simple snippets inside your content files that call built-in or custom templates. They are a powerful way to inject dynamic content into your Markdown without writing raw HTML.
Why Shortcodes?
Markdown is great for writing, but it falls short when you need to embed complex elements like videos, tweets, or custom layouts. Shortcodes bridge the gap — they keep your content clean while giving you the full power of Hugo templates.
Built-in Shortcodes
Hugo ships with several useful shortcodes out of the box.
Figure
| |
This generates semantic HTML with <figure>, <img>, and <figcaption> tags.
YouTube
| |
Embeds a responsive YouTube iframe. You can also pass autoplay=1 as a second parameter.
Tweet
| |
Embeds a Twitter/X card with a single tweet ID.
Gist
| |
Embeds a GitHub Gist.
| |
Embeds an Instagram post.
Custom Shortcodes
This is where the real power lies. Create a file in layouts/shortcodes/ and it becomes available as a shortcode immediately.
Example: A Quote Shortcode
Create layouts/shortcodes/quote.html:
| |
Usage in content:
| |
Shortcode Variables
Every shortcode template has access to these variables:
.Inner— content between opening and closing shortcode tags.Get "key"— named parameter.Get 0— positional parameter.Params— slice of all positional params.Page— the Page object of the current content
Shortcode with Positional Parameters
| |
Usage:
| |
Shortcode Best Practices
- Use named parameters — they make your shortcodes self-documenting and easier to read.
- Handle errors gracefully — check if required parameters exist and provide defaults.
- Use
markdownify— if your shortcode wraps content, pipe.Innerthroughmarkdownifyto render Markdown inside it. - Keep them focused — each shortcode should do one thing well.
- Add comments — shortcode templates can be complex; a brief comment helps future you.
Debugging Shortcodes
If your shortcode isn’t rendering as expected, run Hugo with:
| |
This forces Hugo to rebuild every partial on every change. You can also use {{< myshortcode >}} (with /* and */) to display the shortcode source instead of executing it — useful for documentation.
Ordered and Unordered Shortcodes
Shortcodes come in two forms:
- Unordered —
{{< shortcode >}}— no closing tag - Ordered —
{{< shortcode >}}content{{< /shortcode >}}— wraps content
Use ordered shortcodes when your template needs to process .Inner.
Conclusion
Shortcodes are one of Hugo’s standout features. They let you extend Markdown without breaking out of it, keeping your content portable and your templates maintainable. Whether you use built-in shortcodes or craft your own, they will make your Hugo site more dynamic and your writing workflow smoother.
Reference
Author Paisen
LastMod 2026-06-07