Debian Codegraph @

docs/content/en/content-management/shortcodes.md

raw
1 ---
2 title: Shortcodes
3 description: Use embedded, custom, or inline shortcodes to insert elements such as videos, images, and social media embeds into your content.
4 categories: []
5 keywords: []
6 aliases: [/extras/shortcodes/]
7 ---
8
9 ## Introduction
10
11 {{% glossary-term shortcode %}}
12
13 There are three types of shortcodes: embedded, custom, and inline.
14
15 ## Embedded
16
17 Hugo's embedded shortcodes are pre-defined templates within the application. Refer to each shortcode's documentation for specific usage instructions and available arguments.
18
19 {{% render-list-of-pages-in-section path=/shortcodes %}}
20
21 ## Custom
22
23 Create custom shortcodes to simplify and standardize content creation. For example, the following _shortcode_ template generates an audio player using a [global resource](g):
24
25 ```go-html-template {file="layouts/_shortcodes/audio.html"}
26 {{ with resources.Get (.Get "src") }}
27 <audio controls preload="auto" src="{{ .RelPermalink }}"></audio>
28 {{ end }}
29 ```
30
31 Then call the shortcode from within markup:
32
33 ```md {file="content/example.md"}
34 {{</* audio src=/audio/test.mp3 */>}}
35 ```
36
37 Learn more about creating shortcodes in the [shortcode templates][] section.
38
39 ## Inline
40
41 An inline shortcode is a _shortcode_ template defined within content.
42
43 Hugo's security model is based on the premise that template and configuration authors are trusted, but content authors are not. This model enables generation of HTML output safe against code injection.
44
45 To conform with this security model, creating _shortcode_ templates within content is disabled by default. If you trust your content authors, you can enable this functionality in your project configuration:
46
47 {{< code-toggle file=hugo >}}
48 [security]
49 enableInlineShortcodes = true
50 {{< /code-toggle >}}
51
52 For more information see [configure security][].
53
54 The following example demonstrates an inline shortcode, `date.inline`, that accepts a single positional argument: a date/time [layout string][].
55
56 ```md {file="content/example.md"}
57 Today is
58 {{</* date.inline ":date_medium" */>}}
59 {{- now | time.Format (.Get 0) -}}
60 {{</* /date.inline */>}}.
61
62 Today is {{</* date.inline ":date_full" /*/>}}.
63 ```
64
65 In the example above, the inline shortcode is executed twice: once upon definition and again when subsequently called. Hugo renders this to:
66
67 ```html
68 <p>Today is Jan 30, 2025.</p>
69 <p>Today is Thursday, January 30, 2025</p>
70 ```
71
72 Inline shortcodes process their inner content within the same context as regular _shortcode_ templates, allowing you to use any available [shortcode method][].
73
74 > [!NOTE]
75 > You cannot [nest](#nesting) inline shortcodes.
76
77 Learn more about creating shortcodes in the [shortcode templates][] section.
78
79 ## Calling
80
81 Shortcode calls involve three syntactical elements: tags, arguments, and notation.
82
83 ### Tags
84
85 Some shortcodes expect content between opening and closing tags. For example, the embedded [`details`][] shortcode requires an opening and closing tag:
86
87 ```md
88 {{</* details summary="See the details" */>}}
89 This is a **bold** word.
90 {{</* /details */>}}
91 ```
92
93 Some shortcodes do not accept content. For example, the embedded [`instagram`][] shortcode requires a single _positional_ argument:
94
95 ```md
96 {{</* instagram CxOWiQNP2MO */>}}
97 ```
98
99 Some shortcodes optionally accept content. For example, you can call the embedded [`qr`][] shortcode with content:
100
101 ```md
102 {{</* qr */>}}
103 https://gohugo.io
104 {{</* /qr */>}}
105 ```
106
107 Or use the self-closing syntax with a trailing slash to pass the text as an argument:
108
109 ```md
110 {{</* qr text=https://gohugo.io /*/>}}
111 ```
112
113 Refer to each shortcode's documentation for specific usage instructions and available arguments.
114
115 ### Arguments
116
117 Shortcode arguments can be either _named_ or _positional_.
118
119 Named arguments are passed as case-sensitive key-value pairs, as seen in this example with the embedded [`figure`][] shortcode. The `src` argument, for instance, is required.
120
121 ```md
122 {{</* figure src=/images/kitten.jpg */>}}
123 ```
124
125 Positional arguments, on the other hand, are determined by their position. The embedded `instagram` shortcode, for example, expects the first argument to be the Instagram post ID.
126
127 ```md
128 {{</* instagram CxOWiQNP2MO */>}}
129 ```
130
131 Shortcode arguments are space-delimited, and arguments with internal spaces must be quoted.
132
133 ```md
134 {{</* figure src=/images/kitten.jpg alt="A white kitten" */>}}
135 ```
136
137 Shortcodes accept [scalar](g) arguments, one of [string](g), [integer](g), [floating point](g), or [boolean](g).
138
139 ```md
140 {{</* my-shortcode name="John Smith" age=24 married=false */>}}
141 ```
142
143 You can optionally use multiple lines when providing several arguments to a shortcode for better readability:
144
145 ```md
146 {{</* figure
147 src=/images/kitten.jpg
148 alt="A white kitten"
149 caption="This is a white kitten"
150 loading=lazy
151 */>}}
152 ```
153
154 Use a [raw string literal](g) if you need to pass a multiline string:
155
156 ```md
157 {{</* myshortcode `This is some <b>HTML</b>,
158 and a new line with a "quoted string".` */>}}
159 ```
160
161 Shortcodes can accept named arguments, positional arguments, or both, but you must use either named or positional arguments exclusively within a single shortcode call; mixing them is not allowed.
162
163 Refer to each shortcode's documentation for specific usage instructions and available arguments.
164
165 ### Notation
166
167 Shortcodes can be called using two different notations, distinguished by their tag delimiters.
168
169 Notation|Example
170 :--|:--
171 Markdown|`{{%/* foo */%}} ## Section 1 {{%/* /foo */%}}`
172 Standard|`{{</* foo */>}} ## Section 2 {{</* /foo */>}}`
173
174 #### Markdown notation
175
176 Hugo processes the shortcode before the page content is rendered by the Markdown renderer. This means, for instance, that Markdown headings inside a Markdown-notation shortcode will be included when invoking the [`TableOfContents`][] method on the `Page` object.
177
178 #### Standard notation
179
180 With standard notation, Hugo processes the shortcode separately, merging the output into the page content after Markdown rendering. This means, for instance, that Markdown headings inside a standard-notation shortcode will be excluded when invoking the `TableOfContents` method on the `Page` object.
181
182 By way of example, with this _shortcode_ template:
183
184 ```go-html-template {file="layouts/_shortcodes/foo.html"}
185 {{ .Inner }}
186 ```
187
188 And this markdown:
189
190 ```md {file="content/example.md"}
191 {{%/* foo */%}} ## Section 1 {{%/* /foo */%}}
192
193 {{</* foo */>}} ## Section 2 {{</* /foo */>}}
194 ```
195
196 Hugo renders this HTML:
197
198 ```html
199 <h2 id="heading">Section 1</h2>
200
201 ## Section 2
202 ```
203
204 In the above, "Section 1" will be included when invoking the `TableOfContents` method, while "Section 2" will not.
205
206 > [!NOTE]
207 > The shortcode author determines which notation to use. Consult each shortcode's documentation for specific usage instructions and available arguments.
208
209 ## Nesting
210
211 Shortcodes (excluding [inline](#inline) shortcodes) can be nested, creating parent-child relationships. For example, a gallery shortcode might contain several image shortcodes:
212
213 ```md {file="content/example.md"}
214 {{</* gallery class="content-gallery" */>}}
215 {{</* image src="/images/a.jpg" */>}}
216 {{</* image src="/images/b.jpg" */>}}
217 {{</* image src="/images/c.jpg" */>}}
218 {{</* /gallery */>}}
219 ```
220
221 The shortcode templates section provides detailed explanations and examples of [nesting][] and [rendering order][].
222
223 [`TableOfContents`]: /methods/page/tableofcontents/
224 [`details`]: /shortcodes/details/
225 [`figure`]: /shortcodes/figure/
226 [`instagram`]: /shortcodes/instagram/
227 [`qr`]: /shortcodes/qr/
228 [configure security]: /configuration/security/
229 [layout string]: /functions/time/format/#layout-string
230 [nesting]: /templates/shortcode/#nesting
231 [rendering order]: /templates/shortcode/#rendering-order
232 [shortcode method]: /templates/shortcode/#methods
233 [shortcode templates]: /templates/shortcode/