--- title: Shortcode Templates linktitle: Shortcode Templates description: date: 2017-02-01 publishdate: 2017-02-01 lastmod: 2017-02-01 categories: [templates] tags: [shortcodes] weight: 100 draft: false aliases: [] toc: true --- ## Creating 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. In this sense, you can think of shortcodes as the intermediary between [page and list templates][templates] and [basic content files][]. ### 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. ### Deciding on Shortcode and Parameter Type You can create the following types of shortcodes * Positional parameters * Named parameters * Positional *or* named parameters (i.e, "flexible") * Single-word shortcodes * Nested #### Positional Parameters In shortcodes with positional parameters, the order of the parameters is important. you can choose if the shortcode will use _positional parameters_, or _named parameters_, or _both_. A good rule of thumb is that if a shortcode has a single required value in the case of the `youtube` example below, then positional works very well. For more complex layouts with optional parameters, named parameters work best. Allowing both types of parameters is useful for complex layouts where you want to set default values that can be overridden. ### Accessing Parameters To access a parameter in any shortcode, use the `.Get` method. Whether you pass a key (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, the `.Get` method followed by the named parameter as a quoted string. Named parameters are less terse but do not require that a content author be mindful of the order of parameters. ```golang {{ .Get "class" }} ``` To access a parameter by position, the `.Get` method can be used, keeping in mind that the first positional parameter within the shortcode declaration starts at `0`: ```golang {{ .Get 0 }} ``` `with` is great when the output depends on a parameter being set: ```golang {{ 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: ```golang {{ or .Get "title" | .Get "alt" | if }} alt="{{ with .Get "alt"}}{{.}}{{else}}{{.Get "title"}}{{end}}"{{ end }} ``` If a closing shortcode is used, the variable `.Inner` 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` and provide a warning to the user. A shortcode with `.Inner` content can be used without the inline content, and without the closing shortcode, by using the self-closing syntax: ```golang {{* innershortcode /*/>}} ``` The variable `.Params` contains the list of parameters in case you need to do more complicated things than `.Get`. It is sometimes useful to provide a flexible shortcode that can take named or positional parameters. To meet this need, Hugo shortcodes have `.IsNamedParams`, a boolean available that can be used such as `{{ if .IsNamedParams }}...{{ else }}...{{ end }}`. See the [example Vimeo shortcode][vimeoexample] below for an example. 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. ## Custom Shortcode Examples The following are examples of the different types of shortcodes you can create via 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: ```markdown {{* year */>}} ``` {{% code file="/layouts/shortcodes/year.html" %}} ```golang {{ .Page.Now.Year }} ``` {{% /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]: ```golang {{* youtube 09jf3ow9jfw */>}} ``` Would load the template at `/layouts/shortcodes/youtube.html`: {{% code file="/layouts/shortcodes/youtube.html" %}} ```html
``` {{% /code %}} {{% output file="youtube-embed.html" %}} ```html ``` {{% /output %}} ### 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" %}} ```golang {{* 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" %}} ```html{{ .Get "caption" }} {{ with .Get "attrlink"}} {{ end }} {{ .Get "attr" }} {{ if .Get "attrlink"}} {{ end }}
{{ end }}
<html>
<body> This HTML </body>
</html>