mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
337d0c5f51
dce236ad1 Wrap up the bundle etc. edits for now 27d058566 Add the bundle tree to the organization bundle a83f750dd Move organization.md to its own bundle 01ec4f462 Make the image docs a bundle 60de1e090 Some more resources copy-edits 05d763c0c Some resource copy-edits 6299d6dbb Update the imgproc shortcode 59e0fc209 Add headless bundle documentation a3bbf60bf Link Page Resources page from Front Matter page 830576f86 Update order significance section, add counter section 3b1836509 Revert the recent change made to default list template 886ed0e10 Page Bundles draft rev 2 f530d1a7a image processing and page resources made into regular .md ec47cecda Finalised Page Resources and Image Processing Moved Page Resources and Impage Processing out of the Bundle section and at the root of the Content Management section 253092335 Modified front matter metadata exemple. Added yaml version. da5e4f476 Adding date in the front-matter; missed in previous commit 6bc3ced13 Add rough draft for page and section bundles a0e44458f Image processing first draft, Resources second read/fix 2367f0b78 data: Remove duplicate layouts in table c2f179839 First draft of bundles/resources (covers resources and metadata) 2a3f9a613 Add weights to pages in Bundles branch 9a0146cc0 Switch front-matter format of Bundles doc to yaml; add front-matter 1295fc083 First draft for Bundles documentation organization structure 5a2e52231 Fix archetype paths 9c2e5c063 Merge commit '22cced34fc608256f8271ad591a5ccca991bb164' 22cced34f Squashed 'themes/gohugoioTheme/' changes from 75da2f6b..ecad8247 55d16c9a1 Fix broken sentence in multilingual sections a76895ad2 Replace the outdated Emacs package with new one e6cf1dec0 Remove obsolete link to hugo roadmap dd2fd145b Add GitLab Pages to mentioned hosters (#309) a05ce6bf6 Add 0.34 release notes poster 5c0ebdfca Release 0.34 13c2f3dc8 Merge branch 'temp34' e6b5ffa04 Add 0.34 poster 1e1960496 releaser: Add release notes to /docs for release of 0.34 ac3efe182 releaser: Bump versions for release of 0.34 8f91f62d8 Fixes #222 cca35dbe4 Fix example eaaa21ca1 Add missing params key 00d0b0363 Adding new Blogger utility to tools/migrations 7d36d579e Updated the line number for Dockerfile pointer 852188f85 Update installing.md with Fedora instructions 4d151a3ab Update search.md 4c2750bfb Update deployment-with-nanobox.md c3cc9cd49 configuration: Remove defaultExtension from docs f7c96b4b5 Update GitHub Pages documentation 55787f09a Merge branch 'rmetzler-menu-link-title' 2abbd9bd9 Merge branch 'master' into menu-link-title e1fd710b7 Bring archetypes in from theme. daf6f51c0 Mention the significance of leading 0 in int fn string input 07f498755 Add documentation for `cond` function. 050ccd12b Add documentation for the .HasShortcode function 919af9071 Correct anchor under 'Add custom metadata to a Taxonomy Term' 55600b4ff More layouts work 201cf4f67 Add some more single page layout variants d5e7c03e2 Rework the layouts doc 84622e67c Cleans up the code sample c231c9bd5 Add a new note to 0.33 relnotes 328ec9930 Release 0.33 b108fcc7b Merge branch 'temp33' into next ab9d9ee65 releaser: Prepare repository for 0.34-DEV e20c75320 releaser: Add release notes to /docs for release of 0.33 49f24dcd1 releaser: Bump versions for release of 0.33 9c8e5e207 Update 0.33 poster 7655603c8 Regenerate the docshelper data 16dc99583 Add Hugo 0.33 poster ce40cc197 Merge commit '3cf4300097610bb8b5bd0686d96d1df5db641895' 9a3085523 releaser: Prepare repository for 0.33-DEV a52db97d8 fixing typos and syntax for consistency 64525670f ádd title to some menu entries. This needs hugo >= v0.32 85d415ab2 ádd examples for menu .Title and .Page git-subtree-dir: docs git-subtree-split: dce236ad1258a9d9a0ee209f02b2e1f65b46f0fb
354 lines
15 KiB
Markdown
354 lines
15 KiB
Markdown
---
|
|
title: Create Your Own Shortcodes
|
|
linktitle: Shortcode Templates
|
|
description: You can extend Hugo's built-in shortcodes by creating your own using the same templating syntax as that for single and list pages.
|
|
date: 2017-02-01
|
|
publishdate: 2017-02-01
|
|
lastmod: 2017-02-01
|
|
categories: [templates]
|
|
keywords: [shortcodes,templates]
|
|
menu:
|
|
docs:
|
|
parent: "templates"
|
|
weight: 100
|
|
weight: 100
|
|
sections_weight: 100
|
|
draft: false
|
|
aliases: []
|
|
toc: true
|
|
---
|
|
|
|
Shortcodes are a means to consolidate templating into small, reusable snippets that you can embed directly inside of your content. In this sense, you can think of shortcodes as the intermediary between [page and list templates][templates] and [basic content files][].
|
|
|
|
{{% note %}}
|
|
Hugo also ships with built-in shortcodes for common use cases. (See [Content Management: Shortcodes](/content-management/shortcodes/).)
|
|
{{% /note %}}
|
|
|
|
## Create Custom Shortcodes
|
|
|
|
Hugo's built-in shortcodes cover many common, but not all, use cases. Luckily, Hugo provides the ability to easily create custom shortcodes to meet your website's needs.
|
|
|
|
{{< youtube Eu4zSaKOY4A >}}
|
|
|
|
### File Placement
|
|
|
|
To create a shortcode, place an HTML template in the `layouts/shortcodes` directory of your [source organization][]. Consider the file name carefully since the shortcode name will mirror that of the file but without the `.html` extension. For example, `layouts/shortcodes/myshortcode.html` will be called with either `{{</* myshortcode /*/>}}` or `{{%/* myshortcode /*/%}}` depending on the type of parameters you choose.
|
|
|
|
### Shortcode Template Lookup Order
|
|
|
|
Shortcode templates have a simple [lookup order][]:
|
|
|
|
1. `/layouts/shortcodes/<SHORTCODE>.html`
|
|
2. `/themes/<THEME>/layouts/shortcodes/<SHORTCODE>.html`
|
|
|
|
### Positional vs Named Parameters
|
|
|
|
You can create shortcodes using the following types of parameters:
|
|
|
|
* Positional parameters
|
|
* Named parameters
|
|
* Positional *or* named parameters (i.e, "flexible")
|
|
|
|
In shortcodes with positional parameters, the order of the parameters is important. If a shortcode has a single required value (e.g., the `youtube` shortcode below), positional parameters work very well and require less typing from content authors.
|
|
|
|
For more complex layouts with multiple or optional parameters, named parameters work best. While less terse, named parameters require less memorization from a content author and can be added in a shortcode declaration in any order.
|
|
|
|
Allowing both types of parameters (i.e., a "flexible" shortcode) is useful for complex layouts where you want to set default values that can be easily overridden by users.
|
|
|
|
### Access Parameters
|
|
|
|
All shortcode parameters can be accessed via the `.Get` method. Whether you pass a key (i.e., string) or a number to the `.Get` method depends on whether you are accessing a named or positional parameter, respectively.
|
|
|
|
To access a parameter by name, use the `.Get` method followed by the named parameter as a quoted string:
|
|
|
|
```
|
|
{{ .Get "class" }}
|
|
```
|
|
|
|
To access a parameter by position, use the `.Get` followed by a numeric position, keeping in mind that positional parameters are zero-indexed:
|
|
|
|
```
|
|
{{ .Get 0 }}
|
|
```
|
|
|
|
`with` is great when the output depends on a parameter being set:
|
|
|
|
```
|
|
{{ with .Get "class"}} class="{{.}}"{{ end }}
|
|
```
|
|
|
|
`.Get` can also be used to check if a parameter has been provided. This is
|
|
most helpful when the condition depends on either of the values, or both:
|
|
|
|
```
|
|
{{ or .Get "title" | .Get "alt" | if }} alt="{{ with .Get "alt"}}{{.}}{{else}}{{.Get "title"}}{{end}}"{{ end }}
|
|
```
|
|
|
|
#### `.Inner`
|
|
|
|
If a closing shortcode is used, the `.Inner` variable will be populated with all of the content between the opening and closing shortcodes. If a closing shortcode is required, you can check the length of `.Inner` as an indicator of its existence.
|
|
|
|
A shortcode with content declared via the `.Inner` variable can also be declared without the inline content and without the closing shortcode by using the self-closing syntax:
|
|
|
|
```
|
|
{{</* innershortcode /*/>}}
|
|
```
|
|
|
|
#### `.Params`
|
|
|
|
The `.Params` variable in shortcodes contains the list parameters passed to shortcode for more complicated use cases. You can also access higher-scoped parameters with the following logic:
|
|
|
|
`$.Params`
|
|
: these are the parameters passed directly into the shortcode declaration (e.g., a YouTube video ID)
|
|
|
|
`$.Page.Params`
|
|
: refers to the page's params; the "page" in this case refers to the content file in which the shortcode is declared (e.g., a `shortcode_color` field in a content's front matter could be accessed via `$.Page.Params.shortcode_color`).
|
|
|
|
`$.Page.Site.Params`
|
|
: refers to global variables as defined in your [site's configuration file][config].
|
|
|
|
#### `.IsNamedParams`
|
|
|
|
The `.IsNamedParams` variable checks whether the shortcode declaration uses named parameters and returns a boolean value.
|
|
|
|
For example, you could create an `image` shortcode that can take either a `src` named parameter or the first positional parameter, depending on the preference of the content's author. Let's assume the `image` shortcode is called as follows:
|
|
|
|
```
|
|
{{</* image src="images/my-image.jpg"*/>}}
|
|
```
|
|
|
|
You could then include the following as part of your shortcode templating:
|
|
|
|
```
|
|
{{ if .IsNamedParams }}
|
|
<img src="{{.Get "src" }}" alt="">
|
|
{{ else }}
|
|
<img src="{{.Get 0}}" alt="">
|
|
{{ end }}
|
|
```
|
|
|
|
See the [example Vimeo shortcode][vimeoexample] below for `.IsNamedParams` in action.
|
|
|
|
{{% warning %}}
|
|
While you can create shortcode templates that accept both positional and named parameters, you *cannot* declare shortcodes in content with a mix of parameter types. Therefore, a shortcode declared like `{{</* image src="images/my-image.jpg" "This is my alt text" */>}}` will return an error.
|
|
{{% /warning %}}
|
|
|
|
You can also use the variable `.Page` to access all the normal [page variables][pagevars].
|
|
|
|
A shortcodes can also be nested. In a nested shortcode, you can access the parent shortcode context with [`.Parent` variable][shortcodesvars]. This can be very useful for inheritance of common shortcode parameters from the root.
|
|
|
|
### Checking for Existence
|
|
|
|
You can check if a specific shortcode is used on a page by calling `.HasShortcode` in that page template, providing the name of the shortcode. This is sometimes useful when you want to include specific scripts or styles in the header that are only used by that shortcode.
|
|
|
|
## Custom Shortcode Examples
|
|
|
|
The following are examples of the different types of shortcodes you can create via shortcode template files in `/layouts/shortcodes`.
|
|
|
|
### Single-word Example: `year`
|
|
|
|
Let's assume you would like to keep mentions of your copyright year current in your content files without having to continually review your markdown. Your goal is to be able to call the shortcode as follows:
|
|
|
|
```
|
|
{{</* year */>}}
|
|
```
|
|
|
|
{{< code file="/layouts/shortcodes/year.html" >}}
|
|
{{ now.Format "2006" }}
|
|
{{< /code >}}
|
|
|
|
### Single Positional Example: `youtube`
|
|
|
|
Embedded videos are a common addition to markdown content that can quickly become unsightly. The following is the code used by [Hugo's built-in YouTube shortcode][youtubeshortcode]:
|
|
|
|
```
|
|
{{</* youtube 09jf3ow9jfw */>}}
|
|
```
|
|
|
|
Would load the template at `/layouts/shortcodes/youtube.html`:
|
|
|
|
{{< code file="/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>
|
|
{{< /code >}}
|
|
|
|
{{< code file="youtube-embed.html" copy="false" >}}
|
|
<div class="embed video-player">
|
|
<iframe class="youtube-player" type="text/html"
|
|
width="640" height="385"
|
|
src="http://www.youtube.com/embed/09jf3ow9jfw"
|
|
allowfullscreen frameborder="0">
|
|
</iframe>
|
|
</div>
|
|
{{< /code >}}
|
|
|
|
### Single Named Example: `image`
|
|
|
|
Let's say you want to create your own `img` shortcode rather than use Hugo's built-in [`figure` shortcode][figure]. Your goal is to be able to call the shortcode as follows in your content files:
|
|
|
|
{{< code file="content-image.md" >}}
|
|
{{</* img src="/media/spf13.jpg" title="Steve Francia" */>}}
|
|
{{< /code >}}
|
|
|
|
You have created the shortcode at `/layouts/shortcodes/img.html`, which loads the following shortcode template:
|
|
|
|
{{< code file="/layouts/shortcodes/img.html" >}}
|
|
<!-- image -->
|
|
<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")}}
|
|
<figcaption>{{ if isset .Params "title" }}
|
|
<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 }}
|
|
</p> {{ end }}
|
|
</figcaption>
|
|
{{ end }}
|
|
</figure>
|
|
<!-- image -->
|
|
{{< /code >}}
|
|
|
|
Would be rendered as:
|
|
|
|
{{< code file="img-output.html" copy="false" >}}
|
|
<figure >
|
|
<img src="/media/spf13.jpg" />
|
|
<figcaption>
|
|
<h4>Steve Francia</h4>
|
|
</figcaption>
|
|
</figure>
|
|
{{< /code >}}
|
|
|
|
### Single Flexible Example: `vimeo`
|
|
|
|
```
|
|
{{</* vimeo 49718712 */>}}
|
|
{{</* vimeo id="49718712" class="flex-video" */>}}
|
|
```
|
|
|
|
Would load the template found at `/layouts/shortcodes/vimeo.html`:
|
|
|
|
{{< code file="/layouts/shortcodes/vimeo.html" >}}
|
|
{{ if .IsNamedParams }}
|
|
<div class="{{ if .Get "class" }}{{ .Get "class" }}{{ else }}vimeo-container{{ end }}">
|
|
<iframe src="//player.vimeo.com/video/{{ .Get "id" }}" allowfullscreen></iframe>
|
|
</div>
|
|
{{ else }}
|
|
<div class="{{ if len .Params | eq 2 }}{{ .Get 1 }}{{ else }}vimeo-container{{ end }}">
|
|
<iframe src="//player.vimeo.com/video/{{ .Get 0 }}" allowfullscreen></iframe>
|
|
</div>
|
|
{{ end }}
|
|
{{< /code >}}
|
|
|
|
Would be rendered as:
|
|
|
|
{{< code file="vimeo-iframes.html" copy="false" >}}
|
|
<div class="vimeo-container">
|
|
<iframe src="//player.vimeo.com/video/49718712" allowfullscreen></iframe>
|
|
</div>
|
|
<div class="flex-video">
|
|
<iframe src="//player.vimeo.com/video/49718712" allowfullscreen></iframe>
|
|
</div>
|
|
{{< /code >}}
|
|
|
|
### Paired Example: `highlight`
|
|
|
|
The following is taken from `highlight`, which is a [built-in shortcode][] that ships with Hugo.
|
|
|
|
{{< code file="highlight-example.md" >}}
|
|
{{</* highlight html */>}}
|
|
<html>
|
|
<body> This HTML </body>
|
|
</html>
|
|
{{</* /highlight */>}}
|
|
{{< /code >}}
|
|
|
|
The template for the `highlight` shortcode uses the following code, which is already included in Hugo:
|
|
|
|
```
|
|
{{ .Get 0 | highlight .Inner }}
|
|
```
|
|
|
|
The rendered output of the HTML example code block will be as follows:
|
|
|
|
{{< code file="syntax-highlighted.html" copy="false" >}}
|
|
<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>
|
|
{{< /code >}}
|
|
|
|
{{% note %}}
|
|
The preceding shortcode makes use of a Hugo-specific template function called `highlight`, which uses [Pygments](http://pygments.org) to add syntax highlighting to the example HTML code block. See the [developer tools page on syntax highlighting](/tools/syntax-highlighting/) for more information.
|
|
{{% /note %}}
|
|
|
|
### Nested Shortcode: Image Gallery
|
|
|
|
Hugo's [`.Parent` shortcode variable][parent] returns a boolean value depending on whether the shortcode in question is called within the context of a *parent* shortcode. This provides an inheritance model for common shortcode parameters.
|
|
|
|
The following example is contrived but demonstrates the concept. Assume you have a `gallery` shortcode that expects one named `class` parameter:
|
|
|
|
{{< code file="layouts/shortcodes/gallery.html" >}}
|
|
<div class="{{.Get "class"}}">
|
|
{{.Inner}}
|
|
</div>
|
|
{{< /code >}}
|
|
|
|
You also have an `image` shortcode with a single named `src` parameter that you want to call inside of `gallery` and other shortcodes so that the parent defines the context of each `image`:
|
|
|
|
{{< code file="layouts/shortcodes/image.html" >}}
|
|
{{- $src := .Get "src" -}}
|
|
{{- with .Parent -}}
|
|
<img src="{{$src}}" class="{{.Get "class"}}-image">
|
|
{{- else -}}
|
|
<img src="{{$src}}">
|
|
{{- end }}
|
|
{{< /code >}}
|
|
|
|
You can then call your shortcode in your content as follows:
|
|
|
|
```
|
|
{{</* gallery class="content-gallery" */>}}
|
|
{{</* img src="/images/one.jpg" */>}}
|
|
{{</* img src="/images/two.jpg" */>}}
|
|
{{</* /gallery */>}}
|
|
{{</* img src="/images/three.jpg" */>}}
|
|
```
|
|
|
|
This will output the following HTML. Note how the first two `image` shortcodes inherit the `class` value of `content-gallery` set with the call to the parent `gallery`, whereas the third `image` only uses `src`:
|
|
|
|
```
|
|
<div class="content-gallery">
|
|
<img src="/images/one.jpg" class="content-gallery-image">
|
|
<img src="/images/two.jpg" class="content-gallery-image">
|
|
</div>
|
|
<img src="/images/three.jpg">
|
|
```
|
|
|
|
## More Shortcode Examples
|
|
|
|
More shortcode examples can be found in the [shortcodes directory for spf13.com][spfscs] and the [shortcodes directory for the Hugo docs][docsshortcodes].
|
|
|
|
[basic content files]: /content-management/formats/ "See how Hugo leverages markdown--and other supported formats--to create content for your website."
|
|
[built-in shortcode]: /content-management/shortcodes/
|
|
[config]: /getting-started/configuration/ "Learn more about Hugo's built-in configuration variables as well as how to us your site's configuration file to include global key-values that can be used throughout your rendered website."
|
|
[Content Management: Shortcodes]: /content-management/shortcodes/#using-hugo-s-built-in-shortcodes "Check this section if you are not familiar with the definition of what a shortcode is or if you are unfamiliar with how to use Hugo's built-in shortcodes in your content files."
|
|
[source organization]: /getting-started/directory-structure/#directory-structure-explained "Learn how Hugo scaffolds new sites and what it expects to find in each of your directories."
|
|
[docsshortcodes]: https://github.com/gohugoio/hugo/tree/master/docs/layouts/shortcodes "See the shortcode source directory for the documentation site you're currently reading."
|
|
[figure]: /content-management/shortcodes/#figure
|
|
[hugosc]: /content-management/shortcodes/#using-hugo-s-built-in-shortcodes
|
|
[lookup order]: /templates/lookup-order/ "See the order in which Hugo traverses your template files to decide where and how to render your content at build time"
|
|
[pagevars]: /variables/page/ "See which variables you can leverage in your templating for page vs list templates."
|
|
[parent]: /variables/shortcodes/
|
|
[shortcodesvars]: /variables/shortcodes/ "Certain variables are specific to shortcodes, although most .Page variables can be accessed within your shortcode template."
|
|
[spfscs]: https://github.com/spf13/spf13.com/tree/master/layouts/shortcodes "See more examples of shortcodes by visiting the shortcode directory of the source for spf13.com, the blog of Hugo's creator, Steve Francia."
|
|
[templates]: /templates/ "The templates section of the Hugo docs."
|
|
[vimeoexample]: #single-flexible-example-vimeo
|
|
[youtubeshortcode]: /content-management/shortcodes/#youtube "See how to use Hugo's built-in YouTube shortcode."
|