- About Hugo
- Getting Started
- Hugo Modules
-
Content Management
- 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 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
- .HasMenuCurrent
- .IsMenuCurrent
- .Param
- .Render
- .RenderString
- .Scratch
- .Unix
- absLangURL
- absURL
- after
- anchorize
- append
- apply
- base64
- chomp
- complement
- cond
- countrunes
- countwords
- dateFormat
- default
- delimit
- dict
- echoParam
- emojify
- eq
- errorf and warnf
- fileExists
- findRE
- first
- float
- ge
- getenv
- group
- gt
- hasPrefix
- highlight
- hmac
- htmlEscape
- htmlUnescape
- hugo
- humanize
- i18n
- Image Functions
- in
- index
- int
- intersect
- isset
- jsonify
- lang.Merge
- lang.NumFmt
- last
- le
- len
- lower
- lt
- markdownify
- Math
- md5
- merge
- ne
- now
- os.Stat
- partialCached
- path.Base
- path.Dir
- path.Ext
- path.Join
- path.Split
- plainify
- pluralize
- printf
- println
- querify
- range
- readDir
- readFile
- ref
- reflect.IsMap
- reflect.IsSlice
- relLangURL
- relref
- relURL
- replace
- replaceRE
- safeCSS
- safeHTML
- safeHTMLAttr
- safeJS
- safeURL
- seq
- sha
- shuffle
- singularize
- site
- slice
- slicestr
- sort
- split
- string
- strings.Count
- strings.HasSuffix
- strings.Repeat
- strings.RuneCount
- strings.TrimLeft
- strings.TrimPrefix
- strings.TrimRight
- strings.TrimSuffix
- substr
- symdiff
- templates.Exists
- time
- title
- transform.Unmarshal
- trim
- truncate
- union
- uniq
- upper
- urlize
- urls.Parse
- where
- with
- Variables
- Hugo Pipes
- CLI
- Troubleshooting
- Tools
- Hosting & Deployment
- Contribute
- Maintenance
Shortcodes
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
New in v0.64.1You 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
In Hugo 0.55
we changed how the %
delimiter works. Shortcodes using the %
as the outer-most delimiter will now be fully rendered when sent to the content renderer (e.g. Blackfriday for Markdown), meaning they can be part of the generated table of contents, footnotes, etc.
If you want the old behavior, you can put the following line in the start of your shortcode template:
{{ $_hugo_config := `{ "version": 1 }` }}
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
- 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 iflink
parameter is set. - rel
- Optional
rel
attribute for the URL iflink
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 HTMLfigure
tag.- height
height
attribute of the image.- width
width
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 figure
Input
{{< figure src="/media/spf13.jpg" title="Steve Francia" >}}
Example figure
Output
<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 spf13 7896402 "img.html" >}}
Example gist
Output
<script type="application/javascript" src="https://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
{{< highlight html >}}
<section id="main">
<div>
<h1 id="title">{{ .Title }}</h1>
{{ range .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:
<span style="color: #f92672"><section</span> <span style="color: #a6e22e">id=</span><span style="color: #e6db74">"main"</span><span style="color: #f92672">></span>
<span style="color: #f92672"><div></span>
<span style="color: #f92672"><h1</span> <span style="color: #a6e22e">id=</span><span style="color: #e6db74">"title"</span><span style="color: #f92672">></span>{{ .Title }}<span style="color: #f92672"></h1></span>
{{ range .Pages }}
{{ .Render "summary"}}
{{ end }}
<span style="color: #f92672"></div></span>
<span style="color: #f92672"></section></span>
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 BWNjjyYFxVx >}}
You also have the option to hide the caption:
Example instagram
Output
By adding the preceding hidecaption
example, the following HTML will be added to your rendered website’s markup:
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.
param
Gets a value from the current Page's
params set in front matter, with a fall back to the site param value. It will log an ERROR
if the param with the given key could not be found in either.
{{< param testparam >}}
Since testparam
is a param defined in front matter of this page with the value Hugo Rocks!
, the above will print:
To access deeply nested params, use “dot syntax”, e.g:
{{< param "my.nested.param" >}}
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.
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>
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:
{{< tweet 877500564405444608 >}}
Example tweet
Output
Using the preceding tweet
example, the following HTML will be added to your rendered website’s markup:
<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&ref_src=twsrc%5Etfw">#gohugo</a> <a href="https://twitter.com/hashtag/golang?src=hash&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>— GoHugo.io (@GoHugoIO) <a href="https://twitter.com/GoHugoIO/status/877500564405444608?ref_src=twsrc%5Etfw">June 21, 2017</a></blockquote>
<script async src="https://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 Input shortcode .
https://vimeo.com/channels/staffpicks/146022717
Example vimeo
Input
Extract the ID from the video’s URL and pass it to the vimeo
shortcode:
{{< vimeo 146022717 >}}
Example vimeo
Output
Using the preceding vimeo
example, the following HTML will be added to your rendered website’s markup:
<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
<iframe src="https://player.vimeo.com/video/146022717" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" title="vimeo video" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>
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:
{{< 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
:
{{< 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.
{{< 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:
<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 stylesheets and surrounding markup. The video is also include in the
Quick Start of the Hugo documentation
.
Privacy Config
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 .