hugo/content/en/content-management/shortcodes.md
Bjørn Erik Pedersen 14e369b961 Squashed 'docs/' changes from 341ecabb2..988f7d5c2
988f7d5c2 Document default `enableInlineShortcodes` value
0f604a345 Fix typo in 0.66.0 release note
26fc74fe3 How to access individual EXIF data tags
d5d3bad9a Fix localhost links
fa6921213 Update index.md
5bf558f78 Release 0.66.0
74ccdaaf5 Merge branch 'temp660'
75faa478b releaser: Add release notes to /docs for release of 0.66.0
c4a4a9922 docs: Regen CLI docs
0624ac198 Add build.UseResourceCacheWhen
58a8d7cd1 Add build options documentation
d926c595e fix typo
99713d44b resources: Add basic @import support to resources.PostCSS
224b96cf7 deploy: Implement include/exclude filters for deploy
eb1a00050 Adjusting description; WordPress with capitalized P
91d8efa22 Add another tool for migration from the Wordpress
a6938a4ac Adjust showcase description
a9c0a0a69 Adjust showcase
e5af08aa6 Adding Aether as a proposed showcase item.
0013daa34 Add hugo.IsProduction function
34c419ef3 tpl: Add math.Sqrt
5bdab0ebd Update minification.md
9039332e2 Hugo 0.65.3
1400caf3a Merge branch 'temp653'
9796bb337 releaser: Add release notes to /docs for release of 0.65.3
65b26598f Fix typo
23aa57d80 Fix crashes for 404 in IsAncestor etc.
42c54bc6c 0.65.2
67fd5c1f6 Merge branch 'temp652'
d820ac017 releaser: Add release notes to /docs for release of 0.65.2
51f0888ff Release 0.65.1
91e95260c releaser: Add release notes to /docs for release of 0.65.1
1880ebf05 fix broken link on internal.md
ffaa33889 Update migrations.md
de4d64675 Another tool for migration from Medium platform
90b178d77 releaser: Add release notes to /docs for release of 0.65.1
6925cda30 Handle corner case with rendering text as code in URL
3cb4b19dd Release 0.65.0
7a600cb99 Merge branch 'temp650'
ef9531ff6 releaser: Add release notes to /docs for release of 0.65.0
9bc19606f docs: Regenerate CLI docs
d4a886ed2 Add Page.GetTerms
a3bf273a5 fix broken link on use-modules.md
001f52f4e Fix mage URL in development.md
eef72e887 Merge commit '4b670bc8cc38103c2c60e5090c2f56bf30832b8d'
b18a76631 commands: Support "hugo mod get -u ./..."

git-subtree-dir: docs
git-subtree-split: 988f7d5c2d7a1d40ec2c8ab961cb5a4e41b5bd4c
2020-03-09 20:19:32 +01:00

15 KiB

title linktitle description godocref date publishdate lastmod menu weight categories keywords draft aliases testparam toc
Shortcodes Shortcodes are simple snippets inside your content files calling built-in or custom templates. 2017-02-01 2017-02-01 2019-11-07
docs
parent weight
content-management 35
35
content management
markdown
content
shortcodes
false
/extras/shortcodes/
Hugo Rocks! true

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

{{< youtube 2xkNJL4gJ9E >}}

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

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

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 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.
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

{{< code file="figure-input-example.md" >}} {{</* figure src="/media/spf13.jpg" title="Steve Francia" */>}} {{< /code >}}

Example figure Output

{{< output file="figure-output-example.html" >}}

Steve Francia

{{< /output >}}

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:

{{< code file="gist-input.md" >}} {{</* gist spf13 7896402 "img.html" */>}} {{< /code >}}

Example gist Output

{{< output file="gist-output.html" >}} {{< gist spf13 7896402 >}} {{< /output >}}

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.

{{< gist spf13 7896402 >}}

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

{{< code file="content/tutorials/learn-html.md" >}} {{</* highlight html */>}}

{{ .Title }}

{{ range .Pages }} {{ .Render "summary"}} {{ end }}
{{}} {{< /code >}}

Example highlight Output

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

{{< output file="tutorials/learn-html/index.html" >}} <section id="main"> <div> <h1 id="title">{{ .Title }}</h1> {{ range .Pages }} {{ .Render "summary"}} {{ end }} </div> </section> {{< /output >}}

{{% note "More on Syntax Highlighting" %}} To see even more options for adding syntax-highlighted code blocks to your website, see Syntax Highlighting in Developer Tools. {{% /note %}}

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

{{< code file="instagram-input.md" >}} {{</* instagram BWNjjyYFxVx */>}} {{< /code >}}

You also have the option to hide the caption:

{{< code file="instagram-input-hide-caption.md" >}} {{</* instagram BWNjjyYFxVx hidecaption */>}} {{< /code >}}

Example instagram Output

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

{{< output file="instagram-hide-caption-output.html" >}} {{< instagram BWNjjyYFxVx hidecaption >}} {{< /output >}}

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.

{{< instagram BWNjjyYFxVx hidecaption >}}

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:

{{< param testparam >}}

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.

{{% note "More on Cross References" %}} Read a more extensive description of ref and relref in the cross references documentation. {{% /note %}}

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:

{{< code file="example-tweet-input.md" >}} {{</* tweet 877500564405444608 */>}} {{< /code >}}

Example tweet Output

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

{{< output file="example-tweet-output.html" >}} {{< tweet 877500564405444608 >}} {{< /output >}}

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.

{{< tweet 877500564405444608 >}}

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:

{{< code file="example-vimeo-input.md" >}} {{</* vimeo 146022717 */>}} {{< /code >}}

Example vimeo Output

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

{{< output file="example-vimeo-output.html" >}} {{< vimeo 146022717 >}} {{< /output >}}

{{% tip %}} 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. You can also give the vimeo video a descriptive title with title.

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

{{% /tip %}}

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.

{{< vimeo 146022717 >}}

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:

{{< code file="example-youtube-input.md" >}} {{</* youtube w7Ft2ymGmfc */>}} {{< /code >}}

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:

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

Example youtube Output

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

{{< code file="example-youtube-output.html" >}} {{< youtube id="w7Ft2ymGmfc" autoplay="true" >}} {{< /code >}}

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.

{{< youtube w7Ft2ymGmfc >}}

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.