2013-07-06 19:36:30 -04:00
---
2014-05-29 18:42:05 -04:00
aliases:
- /doc/shortcodes/
date: 2013-07-01
2014-04-23 03:00:11 -04:00
menu:
main:
2014-05-29 18:42:05 -04:00
parent: extras
next: /extras/highlighting
prev: /extras/permalinks
title: Shortcodes
weight: 40
2013-07-08 17:57:01 -04:00
---
2013-07-04 11:32:55 -04:00
2014-10-07 19:52:58 -04:00
Hugo uses Markdown for its simple content format. However, there’ s a lot
of things that Markdown doesn’ t support well.
2013-07-04 11:32:55 -04:00
2014-02-18 18:35:03 -05:00
We are unwilling to accept being constrained by our simple format. Also
2014-10-07 19:52:58 -04:00
unacceptable is writing raw HTML in our Markdown every time we want to include
2014-02-18 18:35:03 -05:00
unsupported content such as a video. To do so is in complete opposition to the
intent of using a bare bones format for our content and utilizing templates to
apply styling for display.
2013-07-04 11:32:55 -04:00
2014-10-07 19:52:58 -04:00
To avoid both of these limitations, Hugo created shortcodes.
2013-07-04 11:32:55 -04:00
2014-08-27 05:36:26 -04:00
A shortcode is a simple snippet inside a content file that Hugo will render
2014-10-07 19:52:58 -04:00
using a predefined template. Note that shortcodes will not work in template
files---if you need a functionality like that in a template, you most likely
2014-11-18 08:21:45 -05:00
want a [partial template ](/templates/partials ) instead.
2013-07-04 11:32:55 -04:00
2014-02-18 18:35:03 -05:00
## Using a shortcode
2014-01-10 21:19:19 -05:00
Shortcode rewrite, take 2
This commit contains a restructuring and partial rewrite of the shortcode handling.
Prior to this commit rendering of the page content was mingled with handling of the shortcodes. This led to several oddities.
The new flow is:
1. Shortcodes are extracted from page and replaced with placeholders.
2. Shortcodes are processed and rendered
3. Page is processed
4. The placeholders are replaced with the rendered shortcodes
The handling of summaries is also made simpler by this.
This commit also introduces some other chenges:
1. distinction between shortcodes that need further processing and those who do not:
* `{{< >}}`: Typically raw HTML. Will not be processed.
* `{{% %}}`: Will be processed by the page's markup engine (Markdown or (infuture) Asciidoctor)
The above also involves a new shortcode-parser, with lexical scanning inspired by Rob Pike's talk called "Lexical Scanning in Go",
which should be easier to understand, give better error messages and perform better.
2. If you want to exclude a shortcode from being processed (for documentation etc.), the inner part of the shorcode must be commented out, i.e. `{{%/* movie 47238zzb */%}}`. See the updated shortcode section in the documentation for further examples.
The new parser supports nested shortcodes. This isn't new, but has two related design choices worth mentioning:
* The shortcodes will be rendered individually, so If both `{{< >}}` and `{{% %}}` are used in the nested hierarchy, one will be passed through the page's markdown processor, the other not.
* To avoid potential costly overhead of always looking far ahead for a possible closing tag, this implementation looks at the template itself, and is branded as a container with inner content if it contains a reference to `.Inner`
Fixes #565
Fixes #480
Fixes #461
And probably some others.
2014-10-27 16:48:30 -04:00
In your content files, a shortcode can be called by using '`{{%/* name parameters
*/%}}`' respectively. Shortcodes are space delimited (parameters with spaces
2014-02-18 18:35:03 -05:00
can be quoted).
2014-01-10 21:19:19 -05:00
2014-02-18 18:35:03 -05:00
The first word is always the name of the shortcode. Parameters follow the name.
2014-10-07 19:52:58 -04:00
The format for named parameters models that of HTML with the format
2014-11-18 11:49:43 -05:00
`name="value"` .
2014-01-10 21:19:19 -05:00
2014-02-18 18:35:03 -05:00
Some shortcodes use or require closing shortcodes. Like HTML, the opening and closing
shortcodes match (name only), the closing being prepended with a slash.
Example of a paired shortcode:
2014-05-15 09:58:55 -04:00
2014-11-18 11:49:43 -05:00
{{< /* highlight go */>}} A bunch of code here {{</* /highlight */>}}
2014-02-18 18:35:03 -05:00
2014-11-18 11:49:43 -05:00
The examples above use two different delimiters, the difference being the `%` and the `<` character:
### Shortcodes with Markdown
The `%` characters indicates that the shortcode's inner content needs further processing by the page's rendering processor (i.e. Markdown), needed to get the **bold** text in the example below:
```
{{%/* myshortcode */%}}Hello * *World!**{{%/* /myshortcode */%}}
```
### Shortcodes without Markdown
The `<` character indicates that the shortcode's inner content doesn't need any further rendering, this will typically be pure HTML:
```
{{< /* myshortcode */>}}<p>Hello <strong>World!</strong></p>{{</* /myshortcode */>}}
```
2014-02-18 18:35:03 -05:00
## Hugo Shortcodes
Hugo ships with a set of predefined shortcodes.
2013-07-04 11:32:55 -04:00
2014-02-18 18:35:03 -05:00
### highlight
2013-07-04 11:32:55 -04:00
2014-02-18 18:35:03 -05:00
This shortcode will convert the source code provided into syntax highlighted
2014-10-07 19:52:58 -04:00
HTML. Read more on [highlighting ](/extras/highlighting ).
2014-01-10 21:19:19 -05:00
2014-02-18 18:35:03 -05:00
#### Usage
2014-10-07 19:52:58 -04:00
`highlight` takes exactly one required parameter of _language_ and requires a
2014-02-18 18:35:03 -05:00
closing shortcode.
2014-01-10 21:19:19 -05:00
2014-02-18 18:35:03 -05:00
#### Example
2014-05-15 09:58:55 -04:00
2014-11-18 11:49:43 -05:00
{{< /* highlight html */>}}
2014-02-18 18:35:03 -05:00
< section id = "main" >
< div >
< h1 id = "title" > {{ .Title }}< / h1 >
{{ range .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
< / div >
< / section >
2014-11-18 11:49:43 -05:00
{{< /* /highlight */>}}
2014-02-18 18:35:03 -05:00
#### Example Output
< 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 .Data.Pages }}
{{ .Render " summary" }}
{{ end }}
< span style = "color: #f92672 " > < /div> </ span >
< span style = "color: #f92672 " > < /section> </ span >
2014-01-10 21:19:19 -05:00
2014-02-18 18:35:03 -05:00
### figure
2014-10-07 19:52:58 -04:00
`figure` is simply an extension of the image capabilities present with Markdown.
`figure` provides the ability to add captions, CSS classes, alt text, links etc.
2014-01-10 21:19:19 -05:00
2014-02-18 18:35:03 -05:00
#### Usage
2013-07-04 11:32:55 -04:00
2014-10-07 19:52:58 -04:00
`figure` can use the following parameters:
2014-02-18 18:35:03 -05:00
* src
* link
* title
* caption
* attr (attribution)
* attrlink
* alt
#### Example
2014-11-18 11:49:43 -05:00
{{< /* figure src="/media/spf13.jpg" title="Steve Francia" */>}}
2014-02-18 18:35:03 -05:00
#### Example output
2014-05-15 09:58:55 -04:00
< figure >
< img src = "/media/spf13.jpg" / >
< figcaption >
< h4 > Steve Francia< / h4 >
< / figcaption >
< / figure >
2014-02-18 18:35:03 -05:00
## Creating your own shortcodes
To create a shortcode, place a template in the layouts/shortcodes directory. The
template name will be the name of the shortcode.
2014-10-07 19:52:58 -04:00
In creating a shortcode, you can choose if the shortcode will use _positional
parameters_ or _named parameters_ (but not both). A good rule of thumb is that if a
shortcode has a single required value in the case of the `youtube` example below,
2014-02-18 18:35:03 -05:00
then positional works very well. For more complex layouts with optional
2014-10-07 19:52:58 -04:00
parameters, named parameters work best.
2014-01-10 21:19:19 -05:00
**Inside the template**
2014-10-07 19:52:58 -04:00
To access a parameter by position, the `.Get` method can be used:
2014-01-10 21:19:19 -05:00
2014-02-25 23:57:31 -05:00
{{ .Get 0 }}
2014-01-10 21:19:19 -05:00
2014-10-07 19:52:58 -04:00
To access a parameter by name, the `.Get` method should be utilized:
2014-01-10 21:19:19 -05:00
2014-02-25 23:57:31 -05:00
{{ .Get "class" }}
2014-10-07 19:52:58 -04:00
`with` is great when the output depends on a parameter being set:
2014-02-25 23:57:31 -05:00
{{ with .Get "class"}} class="{{.}}"{{ end }}
2014-10-07 19:52:58 -04:00
`.Get` can also be used to check if a parameter has been provided. This is
2014-02-25 23:57:31 -05:00
most helpful when the condition depends on either one value or another...
2014-10-07 19:52:58 -04:00
or both:
2014-02-25 23:57:31 -05:00
{{ or .Get "title" | .Get "alt" | if }} alt="{{ with .Get "alt"}}{{.}}{{else}}{{.Get "title"}}{{end}}"{{ end }}
2014-01-10 21:19:19 -05:00
2014-10-01 03:51:25 -04:00
If a closing shortcode is used, the variable `.Inner` will be populated with all
2014-02-18 18:35:03 -05:00
of the content between the opening and closing shortcodes. If a closing
2014-10-01 03:51:25 -04:00
shortcode is required, you can check the length of `.Inner` and provide a warning
2014-02-18 18:35:03 -05:00
to the user.
2014-01-10 21:19:19 -05:00
2014-10-01 03:51:25 -04:00
The variable `.Params` contains the list of parameters in case you need to do more complicated things than `.Get` .
You can also use the variable `.Page` to access all the normal [Page Variables ](/templates/variables/ ).
2014-01-10 21:19:19 -05:00
## Single Positional Example: youtube
2014-11-18 11:49:43 -05:00
{{< /* youtube 09jf3ow9jfw */>}}
2013-07-04 11:32:55 -04:00
2013-12-10 19:36:44 -05:00
Would load the template /layouts/shortcodes/youtube.html
< div class = "embed video-player" >
< iframe class = "youtube-player" type = "text/html" width = "640" height = "385" src = "http://www.youtube.com/embed/{{ index .Params 0 }}" allowfullscreen frameborder = "0" >
< / iframe >
< / div >
2014-10-07 19:52:58 -04:00
This would be rendered as:
2013-07-04 11:32:55 -04:00
< div class = "embed video-player" >
< iframe class = "youtube-player" type = "text/html"
2014-10-07 19:52:58 -04:00
width="640" height="385"
2013-07-04 11:32:55 -04:00
src="http://www.youtube.com/embed/09jf3ow9jfw"
allowfullscreen frameborder="0">
< / iframe >
< / div >
2014-01-10 21:19:19 -05:00
## Single Named Example: image with caption
2013-07-04 11:32:55 -04:00
2014-11-18 11:49:43 -05:00
{{< /* img src="/media/spf13.jpg" title="Steve Francia" */>}}
2013-07-04 11:32:55 -04:00
2013-12-10 19:36:44 -05:00
Would load the template /layouts/shortcodes/img.html
2014-05-15 09:58:55 -04:00
2013-12-10 19:36:44 -05:00
<!-- image -->
2014-02-25 23:57:31 -05:00
< figure { { with . Get " class " } } class = "{{.}}" { { end } } >
{{ with .Get "link"}}< a href = "{{.}}" > {{ end }}
< img src = "{{ .Get " src " } } " { { if or ( . Get " alt " ) ( . Get " caption " ) } } alt = "{{ with .Get " alt " } } { { . } } { { else } } { { . Get " caption " } } { { end } } " { { end } } / >
{{ if .Get "link"}}< / a > {{ end }}
{{ if or (or (.Get "title") (.Get "caption")) (.Get "attr")}}
2013-12-10 19:36:44 -05:00
< figcaption > {{ if isset .Params "title" }}
2014-02-25 23:57:31 -05:00
< h4 > {{ .Get "title" }}< / h4 > {{ end }}
{{ if or (.Get "caption") (.Get "attr")}}< p >
{{ .Get "caption" }}
{{ with .Get "attrlink"}}< a href = "{{.}}" > {{ end }}
{{ .Get "attr" }}
{{ if .Get "attrlink"}}< / a > {{ end }}
2014-02-18 18:35:03 -05:00
< / p > {{ end }}
2013-12-10 19:36:44 -05:00
< / figcaption >
{{ end }}
< / figure >
<!-- image -->
2013-07-04 11:32:55 -04:00
Would be rendered as:
< figure >
< img src = "/media/spf13.jpg" / >
< figcaption >
< h4 > Steve Francia< / h4 >
< / figcaption >
< / figure >
2014-01-10 21:19:19 -05:00
## Paired Example: Highlight
2014-10-07 19:52:58 -04:00
*Hugo already ships with the `highlight` shortcode*
2013-07-04 11:32:55 -04:00
2014-11-18 11:49:43 -05:00
{{< /* highlight html */>}}
2014-01-10 21:19:19 -05:00
< html >
< body > This HTML < / body >
< / html >
2014-11-18 11:49:43 -05:00
{{< /* /highlight */>}}
2013-07-04 11:32:55 -04:00
2014-10-07 19:52:58 -04:00
The template for this utilizes the following code (already include in Hugo)
2014-02-25 23:57:31 -05:00
{{ .Get 0 | highlight .Inner }}
2013-07-04 11:32:55 -04:00
2014-01-10 21:19:19 -05:00
And will be rendered as:
2013-07-04 11:32:55 -04:00
2014-01-10 21:19:19 -05:00
< div class = "highlight" style = "background: #272822 " >< pre style = "line-height: 125%" >< span style = "color: #f92672 " > < html> </ span >
< span style = "color: #f92672 " > < body> </ span > This HTML < span style = "color: #f92672 " > < /body> </ span >
< span style = "color: #f92672 " > < /html> </ span >
< / pre > < / div >
2013-07-04 11:32:55 -04:00
2014-10-07 19:52:58 -04:00
Please notice that this template makes use of a Hugo-specific template function
called `highlight` which uses Pygments to add the highlighting code.
2013-07-04 11:32:55 -04:00
2014-10-07 19:52:58 -04:00
More shortcode examples can be found at [spf13.com ](https://github.com/spf13/spf13.com/tree/master/layouts/shortcodes ).