mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
eb16165694
d1cf9adc4 Fix typo 26e10a690 Fix the name and arg mismatch between partial defn and call 2db0e53cd Merge commit '9c36cff15224f6cbd19058ad61311229b7a23c83' 9c36cff15 Squashed 'themes/gohugoioTheme/' changes from 68ddff44..b8202f53 4b021eff8 Update lang.Merge.md b37af2916 Add title to yaml a9a281233 Fixed incorrect usage of the code-toggle shortcode 4560a0169 Update Warning for Theme Links (#676) 0305e3c6b Document .File.ContentBaseName 6d30c5aa1 Update configuration.md 158df174a Document .Sites and .Sites.First 0c0f583b8 Add stale config e2531afd8 Document path template functions 4dd779057 Clarify that partialCached is per site/language 19e5bbe0c Update index.md 44b000857 Add missing dot a41300cf9 Release 0.52 2d1d92b88 Merge branch 'temp52' c5925250d releaser: Prepare repository for 0.53-DEV d000b04a2 releaser: Add release notes to /docs for release of 0.52 4bb983a0a releaser: Bump versions for release of 0.52 36736ca28 tpl: Add "param" shortcode 378677aa6 Add Elasticsearch/bonsai.io to services doc. 4c3fd4fa4 docs: Document inline shortcodes 6c64c374c Whitelist CSS modules from purge 817a872b9 Improve search icon position cf86ff1c7 Add minification and resource cache clear to build command fd77e8df3 Update asset dependencies and adopt Hugo Pipes cdbe97e8c Update render.md b0e279220 git command to update submodule to latest a1cb98c12 cache/filecache: Add a :project placeholder 07c1b2b46 cache/filecache: Use time.Duration for maxAge ffa9b165e Add AND as a title 6e7733b40 Add OR as a sub title to make it easier to find in search 72b6791a1 docs: Document the new file cache 714d3ca91 Fix minification issues cd1e961da Revert "Add Elasticsearch/bonsai.io to services doc." 15a0cda6e Add Elasticsearch/bonsai.io to services doc. f931d86de Release 0.51 e2ffe867a Merge branch 'temp51' 423e7f5c8 releaser: Prepare repository for 0.52-DEV c6f2d6ae1 releaser: Add release notes to /docs for release of 0.51 5bbb556dc releaser: Bump versions for release of 0.51 3b2b172b9 docs: Document shortcode error handling b8672f3d4 docs: Document symdiff 4bc6071e6 docs: Document complement d1baab752 docs: Re-generate CLI docs 9ea667e24 Revert "tpl: Update Jsonify to return pretty-print output" ce5a1403d docs: Regenerate the docs helper 99a1f4a94 Fix note for reserved partial name(starting with -> including). eba3cbc42 fix accidentally modification on paragraph. 3eebd98c3 Add note for reserved partial name. 40b881cc2 Document templates.Exists b5c3bcd3b Update multilingual.md 61c59c67e Fix misspelling (#648) f21d8c4a4 Correct minor typo (#5372) e967001b9 Release 0.50 685fd6b08 releaser: Prepare repository for 0.51-DEV f245a9faa releaser: Add release notes to /docs for release of 0.50 4354da30d releaser: Bump versions for release of 0.50 feaa05469 docs: Regenerate CLI docs 5c724200c Merge commit 'd6a4af7018e8618944a6471ceeb7aae1d4df6afa' 2ddab36c2 Merge commit '74309fe5699a595080fdb3a14711e0869babce99' 8cf296a7c docs: Regenerate CLI docs 9097683dd tpl: Update Jsonify to return pretty-print output git-subtree-dir: docs git-subtree-split: d1cf9adc412245c96d9d32592a903370d3972aef
419 lines
14 KiB
Markdown
419 lines
14 KiB
Markdown
---
|
|
title: Shortcodes
|
|
linktitle:
|
|
description: Shortcodes are simple snippets inside your content files calling built-in or custom templates.
|
|
godocref:
|
|
date: 2017-02-01
|
|
publishdate: 2017-02-01
|
|
lastmod: 2017-03-31
|
|
menu:
|
|
docs:
|
|
parent: "content-management"
|
|
weight: 35
|
|
weight: 35 #rem
|
|
categories: [content management]
|
|
keywords: [markdown,content,shortcodes]
|
|
draft: false
|
|
aliases: [/extras/shortcodes/]
|
|
testparam: "Hugo Rocks!"
|
|
toc: 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][partials] 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 Markdown
|
|
|
|
The `%` character indicates that the shortcode's inner content---called in the [shortcode template][sctemps] with the [`.Inner` variable][scvars]---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][sctemps].
|
|
|
|
## 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][figureelement].
|
|
|
|
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.
|
|
|
|
class
|
|
: `class` attribute of the HTML `figure` tag.
|
|
|
|
height
|
|
: `height` attribute of the image.
|
|
|
|
width
|
|
: `width` attribute of the image.
|
|
|
|
attr
|
|
: Image attribution text.
|
|
|
|
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" >}}
|
|
<figure>
|
|
<img src="/media/spf13.jpg" />
|
|
<figcaption>
|
|
<h4>Steve Francia</h4>
|
|
</figcaption>
|
|
</figure>
|
|
{{< /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][examplegist]:
|
|
|
|
```
|
|
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](/tools/syntax-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 */>}}
|
|
<section id="main">
|
|
<div>
|
|
<h1 id="title">{{ .Title }}</h1>
|
|
{{ range .Pages }}
|
|
{{ .Render "summary"}}
|
|
{{ end }}
|
|
</div>
|
|
</section>
|
|
{{</* /highlight */>}}
|
|
{{< /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" >}}
|
|
<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>
|
|
{{< /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](/tools/syntax-highlighting/).
|
|
{{% /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. If will log an `ERROR` if the param with the given key could not be found in either.
|
|
|
|
```bash
|
|
{{</* 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:
|
|
|
|
```bash
|
|
{{</* 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](/content-management/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.
|
|
|
|
```
|
|
{{</* vimeo id="146022717" class="my-vimeo-wrapper-class" */>}}
|
|
```
|
|
{{% /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 an 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][quickstart].
|
|
|
|
{{< 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][].
|
|
|
|
[`figure` shortcode]: #figure
|
|
[contentmanagementsection]: /content-management/formats/
|
|
[examplegist]: https://gist.github.com/spf13/7896402
|
|
[figureelement]: http://html5doctor.com/the-figure-figcaption-elements/ "An article from HTML5 doctor discussing the fig and figcaption elements."
|
|
[Hugo and the GDPR]: /about/hugo-and-gdpr/
|
|
[Instagram]: https://www.instagram.com/
|
|
[pagevariables]: /variables/page/
|
|
[partials]: /templates/partials/
|
|
[Pygments]: http://pygments.org/
|
|
[quickstart]: /getting-started/quick-start/
|
|
[sctemps]: /templates/shortcode-templates/
|
|
[scvars]: /variables/shortcodes/
|
|
[shortcode template documentation]: /templates/shortcode-templates/
|
|
[templatessection]: /templates/
|
|
[Vimeo]: https://vimeo.com/
|
|
[YouTube Videos]: https://www.youtube.com/
|