32cb8785e Fix page weights in content management section (#1896) 11977b96f Make relURL and related functions consistent (#1895) f12180207 Clarify github deployment (#1894) 958877789 Remove remaining references to Highlight.js (#1893) fc487d263 Minor edit to taxonomy page 3b6a224b2 Update theme b28553b62 Change "flavor" to "edition" when referring to builds (#1892) 660e7581c Replaced sudo in OpenBSD with doas (#1891) e3fcdea10 fix a few minor grammatical issues on Firebase docs (#1889) e4c8b30eb update Static Web Apps docs (#1890) da2197c9e Update hosting-on-firebase.md (#1347) 5f2a0c271 Adding deployment guide for Azure Static Web Apps (#1456) 5aaf570cd add Azure Static Web App config to 404 template 35fc54362 add Azure Static Web App config to 404 template d48f67ba1 Update 01-flavors.md 11debae8d Cleaned Use of ref and relref section, added refs of index.md and _in… (#1744) b77604078 docs: Add link to menu entry variables (#1827) 0fa8a6bf0 Misc copy edits (#1887) c27b545ac Improve explanation of safeHTMLAttr's function (#1503) b04a4b32e Make CLI command summaries meaningful (#1886) dbf00a81f Fix a typo in diagrams documentation (#1885) 11f884327 docs: Clarify how to remove draft/future/expired content (#1831) 6dc9e9860 Improve complement function (#1884) 56448a51a Remove erroneous sourcemap desc (#1883) a0d0d2829 Merge branch 'divinerites-patch-1' 10f20cb5e Add a plausible-hugo theme component 9f1413eb5 Minor edits to showcase example 7d78420db fix broken link to Isso Comments 925cb291f Make directory tree consistent with other examples 300fff092 Add link to security policy from getenv.md (#1746) 7b4c517a6 Fix docs menu weights ce35775e0 Update faq.md (#1763) f3fb791a4 Remove dated new-in flags (#1879) b6c634629 Remove deprecated templating langs (#1880) 1b25ca34f Update the findRE and replaceRE functions (#1881) 28757ec73 Add Alora Labs website to showcase (#1494) e3c4bc4e7 Remove unimplemented "ugly" property 86afd84ff Update editors.md (#1878) 44c093911 Add urlquery function docs (#1633) 16a8c3548 Update links to installation page (#1876) 9e357f078 Add missing sections to BSD installation page (#1875) 1b1291634 Promote "Installation" to a section 9dd51235b Add detail to description of .Plain page variable (#1870) d333d0287 Minor markdown linting fix and URL updates (#1873) d57c8aa50 Remove extraneous apostrophe (#1871) 8c25cfc5c Update index.md 09fea41e0 Add lang to fenced code block 35b904798 Add small documentation about .Site.Social.twitter variable (#1854) 672042f89 Consolidate site configuration dfd4dd873 Add help.ampio.com showcase. (#1863) e8d0e7bdf Include link to internal templates code (#1794) 7db6f0c01 Add example to split function (#1867) be87dba80 Clarify split function docs (#1792) a079193f1 Fix typo on data templates page b234c70ee Fix data templates page (#1855) 074232b45 Update front-matter.md (#1856) 711c8fa80 Added missing default value (#1862) 034762882 Fixed some grammar issues and typos (#1865) 764574a4d Fix spelling error 2698f2d44 update URLs to prevent redirects (#1864) 68f05fdc8 Fenced code blocks should have a language specified (#1861) 24393315b GitHub Workflows security hardening (#1859) 3eeee13bf Markdown formatting: Add Fenced code block languages (#1858) e152cdf1f netlify: Hugo 0.105.0 4c7fc9f7e Merge branch 'tempv0.105.0' d16710afc Change anchor reference to use relref function calls (#1853) f52af8e4a tpl/encoding: Add noHTMLEscape option to jsonify eca0046c4 Update hosting-on-keycdn docs (#1852) ffbe17a48 Add note for rsync deploy command (#1415) c482133f1 docs: Update quick start to clarify the need of extended version (#1828) 1e3b33804 use correct URL for Google Search console verification (#1851) dac034f63 Markdown and formatting fixes (#1850) 43f177e3c Fix LiveReload in quick-start (#1739) f78deaa5f Add link for ''Hugo Shortcode Syntax Highlighting' VS Code extension (#1765) 08087ecd7 Remove some hidden pages (#1848) b6cb5ae48 Markdown linting fixes (#1846) 527ec5941 Update hugo.md (#1742) 83e8f2168 Clarify that a shortcode with .Inner must be closed (#1785) 4193f4445 Add Super Linter GitHub Action (#1845) fd91bfe1a Formatting and grammar fixes (#1844) ab5a49c49 Create codeql-analysis GitHub Action (#1812) 63b3e082e Add tutorial on using fusejs to search examples (#1756) 54c253ab0 Note that Google Universal Analytics are deprecated (#1770) 385fa77c6 Update articles.toml (#1840) 5e336bd26 Replace awkward wording (ESL?) (#1842) 2446ad349 Added Introduction to Hugo tutorial/video series (#1736) 7b21b2e76 Don't use self-closing generator tag ef73712ff Image processing. available methods: add method 'Colors' (#1837) 018f83bbe [comment platform] - add new alternative (#1751) 5636c208b Grammar and spelling fixes (#1836) 3f2e26f77 Change link of repojacking vulnerable link - JekyllToHugo (#1834) 301379fc3 fix: use shorter image URL to make it easier to read (#1835) de5fa7b30 Update search.md to include Pagefind (#1826) e9d72bcda Breadcrumb example: add basic accessibility (#1832) 6cffff87a netlify: Hugo 0.104.3 892360f61 Update output-formats.md 09a7a46ae Remove my defunct and little used migrator (#1824) 347434cca netlify: Hugo 0.104.2 f8c721162 Update postcss.md c2baf7155 netlify: Hugo 0.104.1 05d1192cd Update diagrams.md (#1823) 3c43a8bbe netlify: Hugo 0.104.0 57973b334 Merge branch 'tempv0.104.0' da775a36d docs: Regen docs helper ae48b5901 docs: Regenerate CLI docs af4a823b1 resources/images: Add $image.Colors 8e3f9ca64 Remove outdated IE conditional comments example (#1821) d1a84701b fix typo in template introduction (#1820) c0c7339e0 Update internal.md 17aefc515 Remove the recommendation about where to put the GA tempalte 263297236 Adjust GA template instructions 1cc265d99 Update the GA template usage section e11968338 config/security: Allow proxy variables in subcommands 9218ab993 netlify: Hugo 0.103.1 0b0e890d1 Update markdownify and RenderString documentation (#1818) 50f5d4776 Fix internal link (#1817) 6beb443c5 netlify: Hugo 0.103.0 14b5af248 Merge branch 'tempv0.103.0' 548e7aa62 server: Add 404 support 3a20aa0ba Update theme git-subtree-dir: docs git-subtree-split: 32cb8785ea74d5b82f2e2bea79d059cab497902a
16 KiB
title | linkTitle | description | categories | keywords | menu | toc | weight | aliases | testparam | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Shortcodes | Shortcodes | Shortcodes are simple snippets inside your content files calling built-in or custom templates. |
|
|
|
true | 100 |
|
Hugo Rocks! |
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
{{< 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
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. 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
{{< code file="figure-input-example.md" >}} {{</* figure src="/media/spf13.jpg" title="Steve Francia" */>}} {{< /code >}}
Example figure
Output
{{< output file="figure-output-example.html" >}}
{{< /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 remarkable 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 depend 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 }}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 style sheets and surrounding markup.
{{< instagram BWNjjyYFxVx hidecaption >}}
{{% note %}}
The instagram
-shortcode refers an endpoint of Instagram's API, that's deprecated since October 24th, 2020. Thus, no images can be fetched from this API endpoint, resulting in an error when the instagram
-shortcode is used. For more information please have a look at GitHub issue #7879.
{{% /note %}}
param
Gets a value from the current Page's
params set in front matter, with a fallback 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="https://example.com/blog/neat">Neat</a>
<a href="/about/#who">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/SanDiegoZoo/status/1453110110599868418
Example tweet
Input
Pass the tweet's user (case-insensitive) and ID from the URL as parameters to the tweet
shortcode.
{{< code file="example-tweet-input.md" >}} {{</* tweet user="SanDiegoZoo" id="1453110110599868418" */>}} {{< /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 user="SanDiegoZoo" id="1453110110599868418" >}} {{< /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 user="SanDiegoZoo" id="1453110110599868418" >}}
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:
{{< 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 >}}
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.
{{< code file="example-youtube-input-with-title.md" >}} {{</* youtube id="w7Ft2ymGmfc" title="A New Hugo Site in Under Two Minutes" */>}} {{< /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 style sheets 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.