HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Internal templates
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • Overview
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
CONTENT MANAGEMENT

Shortcodes

Shortcodes are simple snippets inside your content files calling built-in or custom templates.

What a shortcode is

Hugo loves Markdown because of its simple content format, but there are times when Markdown falls short. Often, content authors are forced to add raw HTML (e.g., video <iframe>’s) to Markdown content. We think this contradicts the beautiful simplicity of Markdown’s syntax.

Hugo created shortcodes to circumvent these limitations.

A shortcode is a simple snippet inside a content file that Hugo will render using a predefined template. Note that shortcodes will not work in template files. If you need the type of drop-in functionality that shortcodes provide but in a template, you most likely want a partial template instead.

In addition to cleaner Markdown, shortcodes can be updated any time to reflect new classes, techniques, or standards. At the point of site generation, Hugo shortcodes will easily merge in your changes. You avoid a possibly complicated search and replace operation.

Use shortcodes

In your content files, a shortcode can be called by calling {{% shortcodename parameters %}}. Shortcode parameters are space delimited, and parameters with internal spaces can be quoted.

The first word in the shortcode declaration is always the name of the shortcode. Parameters follow the name. Depending upon how the shortcode is defined, the parameters may be named, positional, or both, although you can’t mix parameter types in a single call. The format for named parameters models that of HTML with the format name="value".

Some shortcodes use or require closing shortcodes. Again like HTML, the opening and closing shortcodes match (name only) with the closing declaration, which is prepended with a slash.

Here are two examples of paired shortcodes:

{{% mdshortcode %}}Stuff to `process` in the *center*.{{% /mdshortcode %}}
{{< highlight go >}} A bunch of code here {{< /highlight >}}

The examples above use two different delimiters, the difference being the % character in the first and the <> characters in the second.

Shortcodes with raw string parameters

You can pass multiple lines as parameters to a shortcode by using raw string literals:

{{<  myshortcode `This is some <b>HTML</b>,
and a new line with a "quoted string".` >}}

Shortcodes with Markdown

Shortcodes using the % as the outer-most delimiter will be fully rendered when sent to the content renderer. This means that the rendered output from a shortcode can be part of the page’s table of contents, footnotes, etc.

Shortcodes without Markdown

The < character indicates that the shortcode’s inner content does not need further rendering. Often shortcodes without Markdown include internal HTML:

{{< myshortcode >}}<p>Hello <strong>World!</strong></p>{{< /myshortcode >}}

Nested shortcodes

You can call shortcodes within other shortcodes by creating your own templates that leverage the .Parent variable. .Parent allows you to check the context in which the shortcode is being called. See Shortcode templates.

Embedded shortcodes

Use these embedded shortcodes as needed.

figure

To override Hugo’s embedded figure shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

The figure shortcode can use the following named parameters:

src
URL of the image to be displayed.
link
If the image needs to be hyperlinked, URL of the destination.
target
Optional target attribute for the URL if link parameter is set.
rel
Optional rel attribute for the URL if link parameter is set.
alt
Alternate text for the image if the image cannot be displayed.
title
Image title.
caption
Image caption. Markdown within the value of caption will be rendered.
class
class attribute of the HTML figure tag.
height
height attribute of the image.
width
width attribute of the image.
loading
loading attribute of the image.
attr
Image attribution text. Markdown within the value of attr will be rendered.
attrlink
If the attribution text needs to be hyperlinked, URL of the destination.

Example usage:

{{< figure src="elephant.jpg" title="An elephant at sunset" >}}

Rendered:

<figure>
  <img src="elephant.jpg">
  <figcaption><h4>An elephant at sunset</h4></figcaption>
</figure>

gist

To override Hugo’s embedded gist shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

To display a GitHub gist with this URL:

https://gist.github.com/user/50a7482715eac222e230d1e64dd9a89b

Include this in your Markdown:

{{< gist user 50a7482715eac222e230d1e64dd9a89b >}}

This will display all files in the gist alphabetically by file name.

To display a specific file within the gist:

{{< gist user 23932424365401ffa5e9d9810102a477 list.html >}}

Rendered:

highlight

To override Hugo’s embedded highlight shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

To display a highlighted code sample:

{{< highlight go-html-template >}}
{{ range .Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{< /highlight >}}

Rendered:

{{ range .Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}

To specify one or more highlighting options, include a quotation-encapsulated, comma-separated list:

{{< highlight go-html-template "lineNos=inline, lineNoStart=42" >}}
{{ range .Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
{{< /highlight >}}

Rendered:

42{{ range .Pages }}
43  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
44{{ end }}

instagram

To override Hugo’s embedded instagram shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

To display an Instagram post with this URL:

https://www.instagram.com/p/CxOWiQNP2MO/

Include this in your Markdown:

{{< instagram CxOWiQNP2MO >}}

Rendered:

param

To override Hugo’s embedded param shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

The param shortcode renders a parameter from the page’s front matter, falling back to a site parameter of the same name. The shortcode throws an error if the parameter does not exist.

Example usage:

{{< param testparam >}}

Access nested values by chaining the identifiers:

{{< param my.nested.param >}}

ref

To override Hugo’s embedded ref shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

Always use the {{% %}} notation when calling this shortcode.

The ref shortcode returns the permalink of the given page reference.

Example usage:

[Post 1]({{% ref "/posts/post-1" %}})
[Post 1]({{% ref "/posts/post-1.md" %}})
[Post 1]({{% ref "/posts/post-1#foo" %}})
[Post 1]({{% ref "/posts/post-1.md#foo" %}})

Rendered:

<a href="http://example.org/posts/post-1/">Post 1</a>
<a href="http://example.org/posts/post-1/">Post 1</a>
<a href="http://example.org/posts/post-1/#foo">Post 1</a>
<a href="http://example.org/posts/post-1/#foo">Post 1</a>

relref

To override Hugo’s embedded relref shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

Always use the {{% %}} notation when calling this shortcode.

The relref shortcode returns the permalink of the given page reference.

Example usage:

[Post 1]({{% relref "/posts/post-1" %}})
[Post 1]({{% relref "/posts/post-1.md" %}})
[Post 1]({{% relref "/posts/post-1#foo" %}})
[Post 1]({{% relref "/posts/post-1.md#foo" %}})

Rendered:

<a href="/posts/post-1/">Post 1</a>
<a href="/posts/post-1/">Post 1</a>
<a href="/posts/post-1/#foo">Post 1</a>
<a href="/posts/post-1/#foo">Post 1</a>

twitter

To override Hugo’s embedded twitter shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

You may call the twitter shortcode by using its tweet alias.

To display a Twitter post with this URL:

https://twitter.com/SanDiegoZoo/status/1453110110599868418

Include this in your Markdown:

{{< twitter user="SanDiegoZoo" id="1453110110599868418" >}}

Rendered:

Owl bet you'll lose this staring contest 🦉 pic.twitter.com/eJh4f2zncC

— San Diego Zoo Wildlife Alliance (@sandiegozoo) October 26, 2021

vimeo

To override Hugo’s embedded vimeo shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

To display a Vimeo video with this URL:

https://vimeo.com/channels/staffpicks/55073825

Include this in your Markdown:

{{< vimeo 55073825 >}}

Rendered:

If you want to further customize the visual styling of the YouTube or Vimeo output, add a class parameter when calling the shortcode. The new class will be added to the <div> that wraps the <iframe> and will remove the inline styles. Note that you will need to call the id as a named parameter as well. You can also give the vimeo video a descriptive title with title.

{{< vimeo id="146022717" class="my-vimeo-wrapper-class" title="My vimeo video" >}}

youtube

To override Hugo’s embedded vimeo shortcode, copy the source code to a file with the same name in the layouts/shortcodes directory.

The youtube shortcode embeds a responsive video player for YouTube videos. Only the ID of the video is required, e.g.:

https://www.youtube.com/watch?v=w7Ft2ymGmfc

Example youtube input

Copy the YouTube video ID that follows v= in the video’s URL and pass it to the youtube shortcode:

example-youtube-input.md
{{< youtube w7Ft2ymGmfc >}}

Furthermore, you can automatically start playback of the embedded video by setting the autoplay parameter to true. Remember that you can’t mix named and unnamed parameters, so you’ll need to assign the yet unnamed video ID to the parameter id:

example-youtube-input-with-autoplay.md
{{< youtube id="w7Ft2ymGmfc" autoplay="true" >}}

For accessibility reasons, it’s best to provide a title for your YouTube video. You can do this using the shortcode by providing a title parameter. If no title is provided, a default of “YouTube Video” will be used.

example-youtube-input-with-title.md
{{< youtube id="w7Ft2ymGmfc" title="A New Hugo Site in Under Two Minutes" >}}

Example youtube output

Using the preceding youtube example, the following HTML will be added to your rendered website’s markup:

example-youtube-output.html
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
  <iframe src="https://www.youtube.com/embed/w7Ft2ymGmfc?autoplay=1" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="YouTube Video"></iframe>
</div>

Example youtube display

Using the preceding youtube example (without autoplay="true"), the following simulates the displayed experience for visitors to your website. Naturally, the final display will be contingent on your style sheets and surrounding markup. The video is also include in the Quick Start of the Hugo documentation.

Privacy configuration

To learn how to configure your Hugo site to meet the new EU privacy regulation, see Hugo and the GDPR.

Create custom shortcodes

To learn more about creating custom shortcodes, see the shortcode template documentation.

See also

  • Hugo and the General Data Protection Regulation
  • Configure markup
  • Create your own shortcodes
  • Build options
  • Comments

On this page

  • What a shortcode is
  • Use shortcodes
  • Embedded shortcodes
  • Privacy configuration
  • Create custom shortcodes
Last updated: February 11, 2024: Capitalize the word Markdown throughout the documentation (c36d20d3)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance