{{- /* Path: layouts/shortcodes/tootpick-partial-wrapper.html Created: 2023-March Usage: {{< tootpick-partial-wrapper >}} */ -}} {{ partial "tootpick-partial" page }} 👆 context
If you want to use a chunk of Go Template code[2] in both a layout file and a content file, and if you want to keep your Hugo project DRY, then you can do the following.
Put the chunk of Go Template code in a Hugo partial.
Create a Hugo shortcode that calls the partial.
Below is an example.
When I was creating
the
Infinite Ink
article titled
Create a “Share on Mastodon” link with Hugo and Tootpick🐘,
I wanted to
display the results of
that article’s
tootpick-partial
within
the
main matter[3]
of
that
article.
But, at that point,
I
could not
because
I did
not yet have a
tootpick-partial-wrapper
shortcode.
Below I describe how I’ve
set things up so it’s now possible to do that.
Near the bottom of some Infinite Ink articles, there is a share-on-mastodon link. Here is a Hugo partial that is similar[4] to the partial that generates Infinite Ink’s share-on-mastodon links:
{{- /*
Path: layouts/partials/tootpick-partial.html
Created: 2023-February
*/ -}}
{{ $text := (printf "Check out "%s" at https://www.ii.com%s #InfiniteInk" .Title .RelPermalink) }}
<a href="https://tootpick.org/#plustospace=yes&text={{- $text | plainify | htmlUnescape | urlquery -}}"
rel="nofollow">
<nobr><img src="/img/mastodon/logo-purple.svg" width="16" style="max-width: 100%;"> Share on Mastodon</nobr>
</a>
For details about this partial, see Infinite Ink’s Create a “Share on Mastodon” link with Hugo and Tootpick🐘.
In Hugo, it’s not possible to
directly
call a partial
from
the source of
a content file.
Instead, you can
use
a Hugo shortcode
and within that shortcode,
call the partial.
For example, here is the shortcode that
Infinite Ink
uses to call the above tootpick-partial.html
:
{{- /* Path: layouts/shortcodes/tootpick-partial-wrapper.html Created: 2023-March Usage: {{< tootpick-partial-wrapper >}} */ -}} {{ partial "tootpick-partial" page }} 👆 context
‼ | In Hugo v0.111.0 and earlier,
the {{ partial "tootpick-partial" .Page }} 👆 This is because
the
global[5] |
The source of this section of this article includes this:
If this article was helpful to you, you can help others and Infinite Ink by clicking this {{< tootpick-partial-wrapper >}} link.
This is rendered as:
If this article was helpful to you, you
can help others and Infinite Ink by clicking
this
Share on Mastodon
gohugo.io: Partial Templates
ii.com: Hugo Shortcodes
For more about Hugo, see Infinite Ink’s…
Hugo’s Markup Languages: AsciiDoc, HTML, Markdown,
TGIH: Themeless & Gitless Introduction to the Hugo SSG (a tutorial👩🏫)
A Way to Compare Hugo’s Markup Languages (featuring inline footnotes)📊
Transforming Text with Hugo (featuring plainify
, htmlUnescape
, and more)
Hugo’s .RenderString
Method (featuring AsciiDoc admonitions in Markdown and Go HTML)
Configuring Security in Hugo (featuring settings needed to use Asciidoctor and Pandoc)🔒
Hugo Shortcodes: How to Include Go Templates in Hugo Content Files
🔗 Linkified Section Headings in Hugo-Generated Web Pages (featuring Markdown and AsciiDoc examples)
Variable and Parameter Names in Hugo (featuring camelCase🐫 and snake_case🐍)
hugo
function, global page
function, and global site
function.@nm@mathstodon.xyz
or
#InfiniteInk
in it.