Files
hugo/content/en/methods/page/RenderShortcodes.md
T
Bjørn Erik Pedersen e9fbadacc3 Squashed 'docs/' changes from 8390a4a3a..f0f4bcb24
f0f4bcb24 Update Batch.md
15a8b1de6 Update Batch.md
eb841ce66 Update theme
751097f24 Use CI_PAGES_URL in GitLab Pages workflow
8584a0581 Fix typos
b030a7149 Update configuration.md
f7ec2ee9c Revert "Update shortcode.md"
f8188f1c8 Update shortcode.md
083fc39c6 Update shortcode.md
d4bdfb96b Update Filter.md
ef00649db Document wrapperClass highlighting option
e227dc422 Hugo 0.140.2
b95d28444 Fix description of the the hugo server command
41ca381b0 Fix documentation of Fragments
73a6cd966 Update theme
2573fd120 netlify: Hugo 0.140.1
90b19ec0d Merge branch 'tempv0.140.1'
ad2e9aeec Update gocloud and docs for S3-Compatible Endpoints
34b30bad2 Remove indentation from include shortcode examples
97b4ef544 Update related.md
7234e0189 Update related.md
32cc191f6 Clarify Name and Title menu entry methods
bce733c63 Update theme
2f1843d10 Use .Page.GetPage in examples of an include shortcode
9e1c85bbd Revert "Document PAGE.String"
bb30e7c08 Document PAGE.String
2d151d77f Fix link
fbe2d442f Miscellaneous updates for v0.140.0
4d3195223 Update JS docs vs Hugo v0.140
8c190aabc netlify: Hugo 0.140.0
826005ffe Merge branch 'tempv0.140.0'
107289a2c Update theme (again)
9884425f9 Update theme
ff0a358d3 Update Go tool version in hugo.work
c9b23cf6d tpl/tplimpl: Update details shortcode
f29f19aff tpl/tplimpl: Add details shortcode

git-subtree-dir: docs
git-subtree-split: f0f4bcb243
2025-01-06 18:09:20 +01:00

2.8 KiB

title, description, categories, keywords, action, toc
title description categories keywords action toc
RenderShortcodes Renders all shortcodes in the content of the given page, preserving the surrounding markup.
related returnType signatures
methods/page/Content
methods/page/Summary
methods/page/ContentWithoutSummary
methods/page/RawContent
methods/page/Plain
methods/page/PlainWords
methods/page/RenderString
template.HTML
PAGE.RenderShortcodes
true

{{< new-in 0.117.0 >}}

Use this method in shortcode templates to compose a page from multiple content files, while preserving a global context for footnotes and the table of contents.

For example:

{{< code file=layouts/shortcodes/include.html >}} {{ with .Get 0 }} {{ with $.Page.GetPage . }} {{- .RenderShortcodes }} {{ else }} {{ errorf "The %q shortcode was unable to find %q. See %s" .Name . .Position }} {{ end }} {{ else }} {{ errorf "The %q shortcode requires a positional parameter indicating the logical path of the file to include. See %s" .Name .Position }} {{ end }} {{< /code >}}

Then call the shortcode in your Markdown:

{{< code file=content/about.md lang=md >}} {{%/* include "/snippets/services" /%}} {{%/ include "/snippets/values" /%}} {{%/ include "/snippets/leadership" */%}} {{< /code >}}

Each of the included Markdown files can contain calls to other shortcodes.

Shortcode notation

In the example above it's important to understand the difference between the two delimiters used when calling a shortcode:

  • {{</* myshortcode */>}} tells Hugo that the rendered shortcode does not need further processing. For example, the shortcode content is HTML.
  • {{%/* myshortcode */%}} tells Hugo that the rendered shortcode needs further processing. For example, the shortcode content is Markdown.

Use the latter for the "include" shortcode described above.

Further explanation

To understand what is returned by the RenderShortcodes method, consider this content file

{{< code file=content/about.md lang=text >}} +++ title = 'About' date = 2023-10-07T12:28:33-07:00 +++

{{</* ref "privacy" */>}}

An emphasized word. {{< /code >}}

With this template code:

{{ $p := site.GetPage "/about" }}
{{ $p.RenderShortcodes }}

Hugo renders this:;

https://example.org/privacy/

An *emphasized* word.

Note that the shortcode within the content file was rendered, but the surrounding Markdown was preserved.

Limitations

The primary use case for .RenderShortcodes is inclusion of Markdown content. If you try to use .RenderShortcodes inside HTML blocks when inside Markdown, you will get a warning similar to this:

WARN .RenderShortcodes detected inside HTML block in "/content/mypost.md"; this may not be what you intended ...

The above warning can be turned off is this is what you really want.