| 1 |
--- |
| 2 |
title: Inner |
| 3 |
description: Returns the content between opening and closing shortcode tags, applicable when the shortcode call includes a closing tag. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: template.HTML |
| 9 |
signatures: [SHORTCODE.Inner] |
| 10 |
--- |
| 11 |
|
| 12 |
This content: |
| 13 |
|
| 14 |
```md {file="content/services.md"} |
| 15 |
{{</* card title="Product Design" */>}} |
| 16 |
We design the **best** widgets in the world. |
| 17 |
{{</* /card */>}} |
| 18 |
``` |
| 19 |
|
| 20 |
With this shortcode: |
| 21 |
|
| 22 |
```go-html-template {file="layouts/_shortcodes/card.html"} |
| 23 |
<div class="card"> |
| 24 |
{{ with .Get "title" }} |
| 25 |
<div class="card-title">{{ . }}</div> |
| 26 |
{{ end }} |
| 27 |
<div class="card-content"> |
| 28 |
{{ .Inner | strings.TrimSpace }} |
| 29 |
</div> |
| 30 |
</div> |
| 31 |
``` |
| 32 |
|
| 33 |
Is rendered to: |
| 34 |
|
| 35 |
```html |
| 36 |
<div class="card"> |
| 37 |
<div class="card-title">Product Design</div> |
| 38 |
<div class="card-content"> |
| 39 |
We design the **best** widgets in the world. |
| 40 |
</div> |
| 41 |
</div> |
| 42 |
``` |
| 43 |
|
| 44 |
> [!NOTE] |
| 45 |
> Content between opening and closing shortcode tags may include leading and/or trailing newlines, depending on placement within the Markdown. Use the [`strings.TrimSpace`][] function as shown above to remove carriage returns and newlines. |
| 46 |
|
| 47 |
> [!NOTE] |
| 48 |
> In the example above, the value returned by `Inner` is Markdown, but it was rendered as plain text. Use either of the following approaches to render Markdown to HTML. |
| 49 |
|
| 50 |
## Use RenderString |
| 51 |
|
| 52 |
Let's modify the example above to pass the value returned by `Inner` through the [`RenderString`][] method on the `Page` object: |
| 53 |
|
| 54 |
```go-html-template {file="layouts/_shortcodes/card.html"} |
| 55 |
<div class="card"> |
| 56 |
{{ with .Get "title" }} |
| 57 |
<div class="card-title">{{ . }}</div> |
| 58 |
{{ end }} |
| 59 |
<div class="card-content"> |
| 60 |
{{ .Inner | strings.TrimSpace | .Page.RenderString }} |
| 61 |
</div> |
| 62 |
</div> |
| 63 |
``` |
| 64 |
|
| 65 |
Hugo renders this to: |
| 66 |
|
| 67 |
```html |
| 68 |
<div class="card"> |
| 69 |
<div class="card-title">Product design</div> |
| 70 |
<div class="card-content"> |
| 71 |
We produce the <strong>best</strong> widgets in the world. |
| 72 |
</div> |
| 73 |
</div> |
| 74 |
``` |
| 75 |
|
| 76 |
You can use the [`markdownify`][] function instead of the `RenderString` method, but the latter is more flexible. See [details][]. |
| 77 |
|
| 78 |
## Alternative notation |
| 79 |
|
| 80 |
Instead of calling the shortcode with the `{{</* */>}}` notation, use the `{{%/* */%}}` notation: |
| 81 |
|
| 82 |
```md {file="content/services.md"} |
| 83 |
{{%/* card title="Product Design" */%}} |
| 84 |
We design the **best** widgets in the world. |
| 85 |
{{%/* /card */%}} |
| 86 |
``` |
| 87 |
|
| 88 |
When you use the `{{%/* */%}}` notation, Hugo renders the entire shortcode as Markdown, requiring the following changes. |
| 89 |
|
| 90 |
First, configure the renderer to allow raw HTML within Markdown: |
| 91 |
|
| 92 |
{{< code-toggle file=hugo >}} |
| 93 |
[markup.goldmark.renderer] |
| 94 |
unsafe = true |
| 95 |
{{< /code-toggle >}} |
| 96 |
|
| 97 |
This configuration is not unsafe if _you_ control the content. Read more about Hugo's [security model][]. |
| 98 |
|
| 99 |
Second, because we are rendering the entire shortcode as Markdown, we must adhere to the rules governing [indentation][] and inclusion of [raw HTML blocks][] as provided in the [CommonMark][] specification. |
| 100 |
|
| 101 |
```go-html-template {file="layouts/_shortcodes/card.html"} |
| 102 |
<div class="card"> |
| 103 |
{{ with .Get "title" }} |
| 104 |
<div class="card-title">{{ . }}</div> |
| 105 |
{{ end }} |
| 106 |
<div class="card-content"> |
| 107 |
|
| 108 |
{{ .Inner | strings.TrimSpace }} |
| 109 |
</div> |
| 110 |
</div> |
| 111 |
``` |
| 112 |
|
| 113 |
The difference between this and the previous example is subtle but required. Note the change in indentation, the addition of a blank line, and removal of the `RenderString` method. |
| 114 |
|
| 115 |
```diff |
| 116 |
--- layouts/_shortcodes/a.html |
| 117 |
+++ layouts/_shortcodes/b.html |
| 118 |
@@ -1,8 +1,9 @@ |
| 119 |
<div class="card"> |
| 120 |
{{ with .Get "title" }} |
| 121 |
- <div class="card-title">{{ . }}</div> |
| 122 |
+ <div class="card-title">{{ . }}</div> |
| 123 |
{{ end }} |
| 124 |
<div class="card-content"> |
| 125 |
- {{ .Inner | strings.TrimSpace | .Page.RenderString }} |
| 126 |
+ |
| 127 |
+ {{ .Inner | strings.TrimSpace }} |
| 128 |
</div> |
| 129 |
</div> |
| 130 |
``` |
| 131 |
|
| 132 |
> [!NOTE] |
| 133 |
> Don't process the `Inner` value with `RenderString` or `markdownify` when using [Markdown notation][] to call the shortcode. |
| 134 |
|
| 135 |
[CommonMark]: https://spec.commonmark.org/current/ |
| 136 |
[Markdown notation]: /content-management/shortcodes/#notation |
| 137 |
[`RenderString`]: /methods/page/renderstring/ |
| 138 |
[`markdownify`]: /functions/transform/markdownify/ |
| 139 |
[`strings.TrimSpace`]: /functions/strings/trimspace/ |
| 140 |
[details]: /methods/page/renderstring/ |
| 141 |
[indentation]: https://spec.commonmark.org/current/#indented-code-blocks |
| 142 |
[raw HTML blocks]: https://spec.commonmark.org/current/#html-blocks |
| 143 |
[security model]: /about/security/ |