HUGO

  • News
  • Docs
  • Themes
  • Community
  • GitHub
Star

What's on this Page

    • What a Shortcode is
    • Use Shortcodes
      • Shortcodes with Markdown
      • Shortcodes Without Markdown
      • Nested Shortcodes
    • Use Hugo’s Built-in Shortcodes
      • figure
        • Example figure Input
        • Example figure Output
      • gist
        • Example gist Input
        • Example gist Output
        • Example gist Display
      • highlight
        • Example highlight Input
        • Example highlight Output
      • instagram
        • Example instagram Input
        • Example instagram Output
        • Example instagram Display
      • ref and relref
        • Example ref and relref Input
        • Example ref and relref Output
      • speakerdeck
        • speakerdeck Example Input
        • speakerdeck Example Output
        • speakerdeck Example Display
      • tweet
        • Example tweet Input
        • Example tweet Output
        • Example tweet Display
      • vimeo
        • Example vimeo Input
        • Example vimeo Output
        • Example vimeo Display
      • youtube
        • Example youtube Input
        • Example youtube Output
        • Example youtube Display
    • Create Custom Shortcodes
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 <iframes>) 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 Markdown

The % character indicates that the shortcode’s inner content—called in the shortcode template with the .Inner variable—needs further processing by the page’s rendering processor (i.e. markdown via Blackfriday). In the following example, Blackfriday would convert **World** to <strong>World</strong>:

{{% myshortcode %}}Hello **World!**{{% /myshortcode %}}

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.

Use Hugo’s Built-in Shortcodes

Hugo ships with a set of predefined shortcodes that represent very common usage. These shortcodes are provided for author convenience and to keep your markdown content clean.

figure

figure is an extension of the image syntax in markdown, which does not provide a shorthand for the more semantic HTML5 <figure> element.

The figure shortcode can use the following named parameters:

  • src
  • link
  • title
  • caption
  • class
  • attr (i.e., attribution)
  • attrlink
  • alt

Example figure Input

figure-input-example.md

{{< figure src="/media/spf13.jpg" title="Steve Francia" >}}

Example figure Output

figure-output-example.html

<figure>
  <img src="/media/spf13.jpg"  />
  <figcaption>
      <h4>Steve Francia</h4>
  </figcaption>
</figure>

gist

Bloggers often want to include GitHub gists when writing posts. Let’s suppose we want to use the gist at the following url:

https://gist.github.com/spf13/7896402

We can embed the gist in our content via username and gist ID pulled from the URL:

{{< gist spf13 7896402 >}}

Example gist Input

If the gist contains several files and you want to quote just one of them, you can pass the filename (quoted) as an optional third argument:

gist-input.md

{{< gist spf13 7896402 "img.html" >}}

Example gist Output

gist-output.html

<script src="//gist.github.com/spf13/7896402.js"></script>

Example gist Display

To demonstrate the remarkably efficiency of Hugo’s shortcode feature, we have embedded the spf13 gist example in this page. The following simulates the experience for visitors to your website. Naturally, the final display will be contingent on your stylesheets and surrounding markup.

highlight

This shortcode will convert the source code provided into syntax-highlighted HTML. Read more on highlighting. highlight takes exactly one required language parameter and requires a closing shortcode.

Example highlight Input

content/tutorials/learn-html.md

{{< highlight html >}}
<section id="main">
  <div>
   <h1 id="title">{{ .Title }}</h1>
    {{ range .Data.Pages }}
        {{ .Render "summary"}}
    {{ end }}
  </div>
</section>
{{< /highlight >}}

Example highlight Output

The highlight shortcode example above would produce the following HTML when the site is rendered:

tutorials/learn-html/index.html

<span style="color: #f92672">&lt;section</span> <span style="color: #a6e22e">id=</span><span style="color: #e6db74">&quot;main&quot;</span><span style="color: #f92672">&gt;</span>
  <span style="color: #f92672">&lt;div&gt;</span>
   <span style="color: #f92672">&lt;h1</span> <span style="color: #a6e22e">id=</span><span style="color: #e6db74">&quot;title&quot;</span><span style="color: #f92672">&gt;</span>{{ .Title }}<span style="color: #f92672">&lt;/h1&gt;</span>
    {{ range .Data.Pages }}
        {{ .Render &quot;summary&quot;}}
    {{ end }}
  <span style="color: #f92672">&lt;/div&gt;</span>
<span style="color: #f92672">&lt;/section&gt;</span>

To see even more options for adding syntax-highlighted code blocks to your website, see Syntax Highlighting in Developer Tools.

instagram

If you’d like to embed a photo from Instagram, you only need the photo’s ID. You can discern an Instagram photo ID from the URL:

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

Example instagram Input

instagram-input.md

{{< instagram BWNjjyYFxVx >}}

You also have the option to hide the caption:

instagram-input-hide-caption.md

{{< instagram BWNjjyYFxVx hidecaption >}}

Example instagram Output

By adding the preceding hidecaption example, the following HTML will be added to your rendered website’s markup:

instagram-hide-caption-output.html

<blockquote class="instagram-media" data-instgrm-version="7" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);"><div style="padding:8px;"> <div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:33.251231527093594% 0; text-align:center; width:100%;"> <div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5/P8/t9FuRVCRmU73JWlzosgSIIZURCjo/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;"></div></div><p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;"><a href="https://www.instagram.com/p/BWNjjyYFxVx/" style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;" target="_blank">A post shared by Bjørn Erik Pedersen (@bepsays)</a> on <time style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;" datetime="2017-07-06T16:27:46+00:00">Jul 6, 2017 at 9:27am PDT</time></p></div></blockquote>
<script async defer src="//platform.instagram.com/en_US/embeds.js"></script>

Example instagram Display

Using the preceding instagram with hidecaption` example above, the following simulates the displayed experience for visitors to your website. Naturally, the final display will be contingent on your stylesheets and surrounding markup.

A post shared by Bjørn Erik Pedersen (@bepsays) on Jul 6, 2017 at 9:27am PDT

ref and relref

These shortcodes will look up the pages by their relative path (e.g., blog/post.md) or their logical name (post.md) and return the permalink (ref) or relative permalink (relref) for the found page.

ref and relref also make it possible to make fragmentary links that work for the header links generated by Hugo.

Read a more extensive description of ref and relref in the cross references documentation.

ref and relref take exactly one required parameter of reference, quoted and in position 0.

Example ref and relref Input

[Neat]({{< ref "blog/neat.md" >}})
[Who]({{< relref "about.md#who" >}})

Example ref and relref Output

Assuming that standard Hugo pretty URLs are turned on.

<a href="/blog/neat">Neat</a>
<a href="/about/#who:c28654c202e73453784cfd2c5ab356c0">Who</a>

speakerdeck

To embed slides from Speaker Deck, click on “< /> Embed” (under Share right next to the template on Speaker Deck) and copy the URL:

<script async class="speakerdeck-embed" data-id="4e8126e72d853c0060001f97" data-ratio="1.33333333333333" src="//speakerdeck.com/assets/embed.js"></script>

speakerdeck Example Input

Extract the value from the field data-id and pass it to the shortcode:

speakerdeck-example-input.md

{{< speakerdeck 4e8126e72d853c0060001f97 >}}

speakerdeck Example Output

speakerdeck-example-input.md

<script async class='speakerdeck-embed' data-id='4e8126e72d853c0060001f97' data-ratio='1.33333333333333' src='//speakerdeck.com/assets/embed.js'></script>

speakerdeck Example Display

For the preceding speakerdeck example, the following simulates the displayed experience for visitors to your website. Naturally, the final display will be contingent on your stylesheets and surrounding markup.

tweet

You want to include a single tweet into your blog post? Everything you need is the URL of the tweet:

https://twitter.com/spf13/status/877500564405444608

Example tweet Input

Pass the tweet’s ID from the URL as a parameter to the tweet shortcode:

example-tweet-input.md

{{< tweet 877500564405444608 >}}

Example tweet Output

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

example-tweet-output.html

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Hugo 0.24 Released: Big archetype update + <a href="https://twitter.com/Netlify?ref_src=twsrc%5Etfw">@Netlify</a> _redirects etc. file support<a href="https://t.co/X94FmYDEZJ">https://t.co/X94FmYDEZJ</a> <a href="https://twitter.com/hashtag/gohugo?src=hash&amp;ref_src=twsrc%5Etfw">#gohugo</a> <a href="https://twitter.com/hashtag/golang?src=hash&amp;ref_src=twsrc%5Etfw">#golang</a> <a href="https://twitter.com/spf13?ref_src=twsrc%5Etfw">@spf13</a> <a href="https://twitter.com/bepsays?ref_src=twsrc%5Etfw">@bepsays</a></p>&mdash; GoHugo.io (@GoHugoIO) <a href="https://twitter.com/GoHugoIO/status/877500564405444608?ref_src=twsrc%5Etfw">June 21, 2017</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

Example tweet Display

Using the preceding tweet example, the following simulates the displayed experience for visitors to your website. Naturally, the final display will be contingent on your stylesheets and surrounding markup.

Hugo 0.24 Released: Big archetype update + @Netlify _redirects etc. file supporthttps://t.co/X94FmYDEZJ #gohugo #golang @spf13 @bepsays

— GoHugo.io (@GoHugoIO) June 21, 2017

vimeo

Adding a video from Vimeo is equivalent to the YouTube shortcode above.

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

Example vimeo Input

Extract the ID from the video’s URL and pass it to the vimeo shortcode:

example-vimeo-input.md

{{< vimeo 146022717 >}}

Example vimeo Output

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

example-vimeo-output.html


<div style="position: relative; padding-bottom: 56.25%; padding-top: 30px; height: 0; overflow: hidden;">
  <iframe src="//player.vimeo.com/video/146022717" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
 </div>

If you want to further customize the visual styling of the YouTube or Vimeo output, add a class named 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.

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

Example vimeo Display

Using the preceding vimeo example, the following simulates the displayed experience for visitors to your website. Naturally, the final display will be contingent on your stylesheets and surrounding markup.

youtube

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 an 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" >}}

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%; padding-top: 30px; height: 0; overflow: hidden;">
  <iframe src="//www.youtube.com/embed/w7Ft2ymGmfc?autoplay=1"
  style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" allowfullscreen frameborder="0" 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 stylesheets and surrounding markup. The video is also include in the Quick Start of the Hugo documentation.

Create Custom Shortcodes

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

See Also

  • markdownify
  • Related Content
  • Shortcode Variables
  • Comments
  • .Get
  • About Hugo
    • Overview
    • Hugo Features
    • The Benefits of Static
    • Roadmap
    • License
  • Getting Started
    • Get Started Overview
    • Quick Start
    • Install Hugo
    • Basic Usage
    • Directory Structure
    • Configuration
  • Themes
    • Themes Overview
    • Install and Use Themes
    • Customize a Theme
    • Create a Theme
  • Content Management
    • Content Management Overview
    • Organization
    • Supported Content Formats
    • Front Matter
    • Shortcodes
    • Related Content
    • Sections
    • Types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and Cross References
    • URL Management
    • Menus
    • Table of Contents
    • Comments
    • Multilingual and i18n
    • Syntax Highlighting
  • Templates
    • Templates Overview
    • Introduction
    • Template Lookup Order
    • Custom Output Formats
    • Base Templates and Blocks
    • List Page Templates
    • Homepage Template
    • Section Templates
    • Taxonomy Templates
    • Single Page Templates
    • Content View Templates
    • Data Templates
    • Partial Templates
    • Shortcode Templates
    • Local File Templates
    • 404 Page
    • Menu Templates
    • Pagination
    • RSS Templates
    • Sitemap Template
    • Robots.txt
    • Internal Templates
    • Alternative Templating
    • Template Debugging
  • Functions
    • Functions Quick Reference
    • .AddDate
    • .Format
    • .Get
    • .GetPage
    • .Param
    • .Scratch
    • .Unix
    • Math
    • absLangURL
    • absURL
    • after
    • apply
    • base64
    • chomp
    • countrunes
    • countwords
    • dateFormat
    • default
    • delimit
    • dict
    • echoParam
    • emojify
    • eq
    • findRE
    • first
    • ge
    • getenv
    • gt
    • hasPrefix
    • highlight
    • htmlEscape
    • htmlUnescape
    • humanize
    • i18n
    • imageConfig
    • in
    • index
    • int
    • intersect
    • isset
    • jsonify
    • lang.NumFmt
    • last
    • le
    • lower
    • lt
    • markdownify
    • md5
    • ne
    • now
    • partialCached
    • plainify
    • pluralize
    • print
    • printf
    • println
    • querify
    • range
    • readDir
    • readFile
    • ref
    • relLangURL
    • relURL
    • relref
    • render
    • replace
    • replaceRE
    • safeCSS
    • safeHTML
    • safeHTMLAttr
    • safeJS
    • safeURL
    • seq
    • sha
    • shuffle
    • singularize
    • slice
    • slicestr
    • sort
    • split
    • string
    • strings.TrimLeft
    • strings.TrimPrefix
    • strings.TrimRight
    • strings.TrimSuffix
    • substr
    • time
    • title
    • trim
    • truncate
    • union
    • uniq
    • upper
    • urlize
    • urls.Parse
    • where
    • with
  • Variables
    • Variables Overview
    • Site Variables
    • Page Variables
    • Shortcode Variables
    • Taxonomy Variables
    • File Variables
    • Menu Variables
    • Hugo Variables
    • Git Variables
    • Sitemap Variables
  • CLI
  • Troubleshooting
    • Troubleshoot
    • Accented Characters in URLs
    • Build Performance
    • EOF Error
  • Tools
    • Developer Tools Overview
    • Migrations
    • Starter Kits
    • Frontends
    • Editor Plug-ins
    • Search
    • Other Projects
  • Hosting & Deployment
    • Hosting & Deployment Overview
    • Host-Agnostic Deploys with Nanobox
    • Host on Netlify
    • Host on Firebase
    • Host on GitHub
    • Host on GitLab
    • Host on Bitbucket
    • Deployment with Wercker
    • Deployment with Rsync
  • Contribute
    • Contribute to Hugo
    • Development
    • Documentation
    • Themes
“Shortcodes” was last updated: October 13, 2017: Initial commit (a48229f)
Improve this page
By the Hugo Authors

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

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

  • File an Issue
  • Get Help
  • Discuss the Source Code
  • @GoHugoIO
  • @spf13
  • @bepsays
  • News
  • Docs
  • Themes
  • Community
  • GitHub
  • About Hugo
  • Getting Started
  • Themes
  • Content Management
  • Templates
  • Functions
  • Variables
  • CLI
  • Troubleshooting
  • Tools
  • Hosting & Deployment
  • Contribute