github.com/jbramsden/hugo@v0.47.1/docs/content/en/content-management/shortcodes.md (about)

     1  ---
     2  title: Shortcodes
     3  linktitle:
     4  description: Shortcodes are simple snippets inside your content files calling built-in or custom templates.
     5  godocref:
     6  date: 2017-02-01
     7  publishdate: 2017-02-01
     8  lastmod: 2017-03-31
     9  menu:
    10    docs:
    11      parent: "content-management"
    12      weight: 35
    13  weight: 35	#rem
    14  categories: [content management]
    15  keywords: [markdown,content,shortcodes]
    16  draft: false
    17  aliases: [/extras/shortcodes/]
    18  toc: true
    19  ---
    20  
    21  ## What a Shortcode is
    22  
    23  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.
    24  
    25  Hugo created **shortcodes** to circumvent these limitations.
    26  
    27  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.
    28  
    29  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.
    30  
    31  ## Use Shortcodes
    32  
    33  {{< youtube 2xkNJL4gJ9E >}}
    34  
    35  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.
    36  
    37  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"`.
    38  
    39  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.
    40  
    41  Here are two examples of paired shortcodes:
    42  
    43  ```
    44  {{%/* mdshortcode */%}}Stuff to `process` in the *center*.{{%/* /mdshortcode */%}}
    45  ```
    46  
    47  ```
    48  {{</* highlight go */>}} A bunch of code here {{</* /highlight */>}}
    49  ```
    50  
    51  The examples above use two different delimiters, the difference being the `%` character in the first and the `<>` characters in the second.
    52  
    53  ### Shortcodes with Markdown
    54  
    55  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>`:
    56  
    57  ```
    58  {{%/* myshortcode */%}}Hello **World!**{{%/* /myshortcode */%}}
    59  ```
    60  
    61  ### Shortcodes Without Markdown
    62  
    63  The `<` character indicates that the shortcode's inner content does *not* need further rendering. Often shortcodes without markdown include internal HTML:
    64  
    65  ```
    66  {{</* myshortcode */>}}<p>Hello <strong>World!</strong></p>{{</* /myshortcode */>}}
    67  ```
    68  
    69  ### Nested Shortcodes
    70  
    71  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].
    72  
    73  ## Use Hugo's Built-in Shortcodes
    74  
    75  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.
    76  
    77  ### `figure`
    78  
    79  `figure` is an extension of the image syntax in markdown, which does not provide a shorthand for the more semantic [HTML5 `<figure>` element][figureelement].
    80  
    81  The `figure` shortcode can use the following named parameters:
    82  
    83  src
    84  : URL of the image to be displayed.
    85  
    86  link
    87  : If the image needs to be hyperlinked, URL of the destination.
    88  
    89  target
    90  : Optional `target` attribute for the URL if `link` parameter is set.
    91  
    92  rel
    93  : Optional `rel` attribute for the URL if `link` parameter is set.
    94  
    95  alt
    96  : Alternate text for the image if the image cannot be displayed.
    97  
    98  title
    99  : Image title.
   100  
   101  caption
   102  : Image caption.
   103  
   104  class
   105  : `class` attribute of the HTML `figure` tag.
   106  
   107  height
   108  : `height` attribute of the image.
   109  
   110  width
   111  : `width` attribute of the image.
   112  
   113  attr
   114  : Image attribution text.
   115  
   116  attrlink
   117  : If the attribution text needs to be hyperlinked, URL of the destination.
   118  
   119  #### Example `figure` Input
   120  
   121  {{< code file="figure-input-example.md" >}}
   122  {{</* figure src="/media/spf13.jpg" title="Steve Francia" */>}}
   123  {{< /code >}}
   124  
   125  #### Example `figure` Output
   126  
   127  {{< output file="figure-output-example.html" >}}
   128  <figure>
   129    <img src="/media/spf13.jpg"  />
   130    <figcaption>
   131        <h4>Steve Francia</h4>
   132    </figcaption>
   133  </figure>
   134  {{< /output >}}
   135  
   136  ### `gist`
   137  
   138  Bloggers often want to include GitHub gists when writing posts. Let's suppose we want to use the [gist at the following url][examplegist]:
   139  
   140  ```
   141  https://gist.github.com/spf13/7896402
   142  ```
   143  
   144  We can embed the gist in our content via username and gist ID pulled from the URL:
   145  
   146  ```
   147  {{</* gist spf13 7896402 */>}}
   148  ```
   149  
   150  #### Example `gist` Input
   151  
   152  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:
   153  
   154  {{< code file="gist-input.md" >}}
   155  {{</* gist spf13 7896402 "img.html" */>}}
   156  {{< /code >}}
   157  
   158  #### Example `gist` Output
   159  
   160  {{< output file="gist-output.html" >}}
   161  {{< gist spf13 7896402 >}}
   162  {{< /output >}}
   163  
   164  #### Example `gist` Display
   165  
   166  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.
   167  
   168  {{< gist spf13 7896402 >}}
   169  
   170  ### `highlight`
   171  
   172  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.
   173  
   174  #### Example `highlight` Input
   175  
   176  {{< code file="content/tutorials/learn-html.md" >}}
   177  {{</* highlight html */>}}
   178  <section id="main">
   179    <div>
   180     <h1 id="title">{{ .Title }}</h1>
   181      {{ range .Pages }}
   182          {{ .Render "summary"}}
   183      {{ end }}
   184    </div>
   185  </section>
   186  {{</* /highlight */>}}
   187  {{< /code >}}
   188  
   189  #### Example `highlight` Output
   190  
   191  The `highlight` shortcode example above would produce the following HTML when the site is rendered:
   192  
   193  {{< output file="tutorials/learn-html/index.html" >}}
   194  <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>
   195    <span style="color: #f92672">&lt;div&gt;</span>
   196     <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>
   197      {{ range .Pages }}
   198          {{ .Render &quot;summary&quot;}}
   199      {{ end }}
   200    <span style="color: #f92672">&lt;/div&gt;</span>
   201  <span style="color: #f92672">&lt;/section&gt;</span>
   202  {{< /output >}}
   203  
   204  {{% note "More on Syntax Highlighting" %}}
   205  To see even more options for adding syntax-highlighted code blocks to your website, see [Syntax Highlighting in Developer Tools](/tools/syntax-highlighting/).
   206  {{% /note %}}
   207  
   208  ### `instagram`
   209  
   210  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:
   211  
   212  ```
   213  https://www.instagram.com/p/BWNjjyYFxVx/
   214  ```
   215  
   216  #### Example `instagram` Input
   217  
   218  {{< code file="instagram-input.md" >}}
   219  {{</* instagram BWNjjyYFxVx */>}}
   220  {{< /code >}}
   221  
   222  You also have the option to hide the caption:
   223  
   224  {{< code file="instagram-input-hide-caption.md" >}}
   225  {{</* instagram BWNjjyYFxVx hidecaption */>}}
   226  {{< /code >}}
   227  
   228  #### Example `instagram` Output
   229  
   230  By adding the preceding `hidecaption` example, the following HTML will be added to your rendered website's markup:
   231  
   232  {{< output file="instagram-hide-caption-output.html" >}}
   233  {{< instagram BWNjjyYFxVx hidecaption >}}
   234  {{< /output >}}
   235  
   236  #### Example `instagram` Display
   237  
   238  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.
   239  
   240  {{< instagram BWNjjyYFxVx hidecaption >}}
   241  
   242  
   243  ### `ref` and `relref`
   244  
   245  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.
   246  
   247  `ref` and `relref` also make it possible to make fragmentary links that work for the header links generated by Hugo.
   248  
   249  {{% note "More on Cross References" %}}
   250  Read a more extensive description of `ref` and `relref` in the [cross references](/content-management/cross-references/) documentation.
   251  {{% /note %}}
   252  
   253  `ref` and `relref` take exactly one required parameter of _reference_, quoted and in position `0`.
   254  
   255  #### Example `ref` and `relref` Input
   256  
   257  ```
   258  [Neat]({{</* ref "blog/neat.md" */>}})
   259  [Who]({{</* relref "about.md#who" */>}})
   260  ```
   261  
   262  #### Example `ref` and `relref` Output
   263  
   264  Assuming that standard Hugo pretty URLs are turned on.
   265  
   266  ```
   267  <a href="/blog/neat">Neat</a>
   268  <a href="/about/#who:c28654c202e73453784cfd2c5ab356c0">Who</a>
   269  ```
   270  
   271  ### `tweet`
   272  
   273  You want to include a single tweet into your blog post? Everything you need is the URL of the tweet:
   274  
   275  ```
   276  https://twitter.com/spf13/status/877500564405444608
   277  ```
   278  
   279  #### Example `tweet` Input
   280  
   281  Pass the tweet's ID from the URL as a parameter to the `tweet` shortcode:
   282  
   283  {{< code file="example-tweet-input.md" >}}
   284  {{</* tweet 877500564405444608 */>}}
   285  {{< /code >}}
   286  
   287  #### Example `tweet` Output
   288  
   289  Using the preceding `tweet` example, the following HTML will be added to your rendered website's markup:
   290  
   291  {{< output file="example-tweet-output.html" >}}
   292  {{< tweet 877500564405444608 >}}
   293  {{< /output >}}
   294  
   295  #### Example `tweet` Display
   296  
   297  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.
   298  
   299  {{< tweet 877500564405444608 >}}
   300  
   301  ### `vimeo`
   302  
   303  Adding a video from [Vimeo][] is equivalent to the YouTube shortcode above.
   304  
   305  ```
   306  https://vimeo.com/channels/staffpicks/146022717
   307  ```
   308  
   309  #### Example `vimeo` Input
   310  
   311  Extract the ID from the video's URL and pass it to the `vimeo` shortcode:
   312  
   313  {{< code file="example-vimeo-input.md" >}}
   314  {{</* vimeo 146022717 */>}}
   315  {{< /code >}}
   316  
   317  #### Example `vimeo` Output
   318  
   319  Using the preceding `vimeo` example, the following HTML will be added to your rendered website's markup:
   320  
   321  {{< output file="example-vimeo-output.html" >}}
   322  {{< vimeo 146022717 >}}
   323  {{< /output >}}
   324  
   325  {{% tip %}}
   326  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.
   327  
   328  ```
   329  {{</* vimeo id="146022717" class="my-vimeo-wrapper-class" */>}}
   330  ```
   331  {{% /tip %}}
   332  
   333  #### Example `vimeo` Display
   334  
   335  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.
   336  
   337  {{< vimeo 146022717 >}}
   338  
   339  ### `youtube`
   340  
   341  The `youtube` shortcode embeds a responsive video player for [YouTube videos][]. Only the ID of the video is required, e.g.:
   342  
   343  ```
   344  https://www.youtube.com/watch?v=w7Ft2ymGmfc
   345  ```
   346  
   347  
   348  #### Example `youtube` Input
   349  
   350  Copy the YouTube video ID that follows `v=` in the video's URL and pass it to the `youtube` shortcode:
   351  
   352  {{< code file="example-youtube-input.md" >}}
   353  {{</* youtube w7Ft2ymGmfc */>}}
   354  {{< /code >}}
   355  
   356  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`:
   357  
   358  
   359  {{< code file="example-youtube-input-with-autoplay.md" >}}
   360  {{</* youtube id="w7Ft2ymGmfc" autoplay="true" */>}}
   361  {{< /code >}}
   362  
   363  #### Example `youtube` Output
   364  
   365  Using the preceding `youtube` example, the following HTML will be added to your rendered website's markup:
   366  
   367  {{< code file="example-youtube-output.html" >}}
   368  {{< youtube id="w7Ft2ymGmfc" autoplay="true" >}}
   369  {{< /code >}}
   370  
   371  #### Example `youtube` Display
   372  
   373  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].
   374  
   375  {{< youtube w7Ft2ymGmfc >}}
   376  
   377  ## Privacy Config
   378  
   379  To learn how to configure your Hugo site to meet the new EU privacy regulation, see [Hugo and the GDPR][].
   380  
   381  ## Create Custom Shortcodes
   382  
   383  To learn more about creating custom shortcodes, see the [shortcode template documentation][].
   384  
   385  [`figure` shortcode]: #figure
   386  [contentmanagementsection]: /content-management/formats/
   387  [examplegist]: https://gist.github.com/spf13/7896402
   388  [figureelement]: http://html5doctor.com/the-figure-figcaption-elements/ "An article from HTML5 doctor discussing the fig and figcaption elements."
   389  [Hugo and the GDPR]: /about/hugo-and-gdpr/
   390  [Instagram]: https://www.instagram.com/
   391  [pagevariables]: /variables/page/
   392  [partials]: /templates/partials/
   393  [Pygments]: http://pygments.org/
   394  [quickstart]: /getting-started/quick-start/
   395  [sctemps]: /templates/shortcode-templates/
   396  [scvars]: /variables/shortcodes/
   397  [shortcode template documentation]: /templates/shortcode-templates/
   398  [templatessection]: /templates/
   399  [Vimeo]: https://vimeo.com/
   400  [YouTube Videos]: https://www.youtube.com/