# Hugo Shortcodes Updated  2022-January-10

## News

2022-January-12  Hugo v0.92.0 released. To keep up with Hugo releases, see discourse.gohugo.io/c/announcements, github.com/gohugoio/hugo/releases, old.reddit.com/r/gohugo/, or twitter.com/@GoHugoIO.

2021-December-23  Published this evolving⁠[1] article. Before today, most of this article was part of Infinite Ink’s Hugo Tips and Fragments.

## Prerequisites

This article assumes you know the basics about the Hugo static site generator. To learn about Hugo, see Infinite Ink’s #gohugo Portal.

## What is a Hugo shortcode

A Hugo shortcode is a chunk of Go Template code that you can use within a Hugo content file. For more information, see…

## One-off custom shortcodes

Reusable and one-off Hugo shortcodes usually live in a Hugo project’s layouts/shortcodes/ directory but, starting with Hugo v0.52, you have the option to put a shortcode directly into a content file by using an “inline shortcode.” This is especially useful for one-off shortcodes such as the ones discussed in shortcode 1 and shortcode 2 below.

To use inline shortcodes you need to set enableInlineShortcodes to true in your Hugo config file by putting this in your config.yaml:

enableInlineShortcodes: true

Or this in your config.toml:

enableInlineShortcodes = true

 ℹ Because sometimes Hugo variables are case sensitive, it’s best to specify the enableInlineShortcodes site variable using the camelCase⁠🐫 that is specified in gohugo.io/getting-started/configuration/.

### 1. “Hugo version” inline shortcode

Infinite Ink’s I Use This (www.ii.com/uses/) page displays this:

• hugo v0.92.0+extended

Each time Hugo builds the Infinite Ink website, the part after the string “hugo ” is generated by the following fragment in Infinite Ink’s uses.adoc[2] content source file.

v{{< hv.inline >}}
{{- hugo.Version -}}
{{< /hv.inline >}}+extended

To learn about this contextless⁠[3] hugo function, including hugo.Version (used above) and hugo.IsProduction (used in the next section), see gohugo.io/functions/hugo/.

 ℹ The dashes[4] in the delimiters {{-  and  -}} above tell hugo to trim whitespace around the output of hugo.Version. These trim-whitespace delimiters must be specified exactly like this, i.e.: a trailing space in the left delimiter, a leading space in the right delimiter, and no whitespace between either curly bracket and its dash.

### 2. “Writing In Progress” inline shortcode

On Infinite Ink’s To-⁠Do, Doing, and Done Lists⁠👷‍♀, in the section Writing In Progress & Experiments, there is a list of articles that have not yet been published. For production — i.e. what you see — this is a list of titles without links, but for me, when I am developing the Infinite Ink website with the hugo server command, this is a list of titles with links to these unpublished articles. I do this by putting my writing-in-progress articles in a directory named content/wip/[5][6] and by putting the following inline shortcode in Infinite Ink’s website-todo-done.adoc[2] content file.

{{< wip.inline >}}
<ul>
{{ range (where site.RegularPages.ByTitle "Section" "eq" "wip") }}
<li>
{{ if hugo.IsProduction }}
<em>{{ .Title }}</em>
{{ else }}  <!-- not production -->
<a href="{{ .RelPermalink }}"><em>{{ .Title }}</em></a>
{{ end }}  <!-- end if-else -->
</li>
{{ end }}  <!-- end range -->
</ul>
{{< /wip.inline >}}

 💡 In the highlighted line above: The parentheses surrounding the where clause are not needed, but I like to use them anyway because it reminds me that the syntax of the Go HTML range function is range COLLECTION. In this context, site.RegularPages is equivalent to .Site.RegularPages. I prefer to use site.RegularPages because it is a more general way to collect a project’s regular pages. To learn about Hugo’s contextless⁠[3] global site function, see gohugo.io/variables/site/#get-the-site-object-from-a-partial. The "eq" in the phrase "Section" "eq" "wip" is not needed because it is the default operator in a where clause. I like to specify it anyway as a note to self about the general syntax of the Go HTML where function.

 ℹ I do things to ensure that no WIP[5] article is mentioned anywhere on Infinite Ink other than on Infinite Ink’s To-⁠Do, Doing, and Done Lists⁠👷‍♀. For example, I specify appropriate build options.

## Reusable custom shortcodes

The shortcodes in this section reside in Infinite Ink’s layouts/shortcodes/ directory and can be called from any content file by using the syntax specified in the USAGE comment at the top of the shortcode.

### 3. build-date

{{/*

USAGE:

{{< build-date >}}

Path: layouts/shortcodes/build-date.html
Purpose: display current date

*/}}

{{- now.Format "2006-01-02" -}}

Here it is in action…

page built 2022-01-12
----------
👆
generated by build-date shortcode

#### About dates in Hugo📆

The string 2006-01-02 in the above build-date shortcode specifies the date format. Note that 2006, 01, and 02 are not a random year, month, and day. Instead they come from the Go reference date, which is Jan 2 15:04:05 2006 MST.

You can remember the reference date with the following mnemonic.

Jan 2 15:04:05 2006 MST
1 2  3  4  5    6  -7

### 4. omnicomment

{{/*

USAGE:

{{< omnicomment >}}
commented
out
{{< /omnicomment >}}

Forked from: github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/todo.html

Path: layouts/shortcodes/omnicomment.html

Purpose: comment in content file written in any markup language Hugo supports

Why 1: no comment translation needed when change file from, e.g., .md to .adoc

Why 2: comment out big chunk that contains multiple markup comments[7]

*/}}

{{ if .Inner }}{{ end }}

Note that this is a no-op shortcode.

### 5. years-since

{{- /*

USAGE:

{{< years-since YYYY >}}

EXAMPLE:

{{< years-since 2013 >}}

Path: layouts/shortcodes/years-since.html

Why: as time goes on, this will still be the approximate number of years since YYYY

*/ -}}

{{- $thisyear := int (now.Format "2006") -}} {{-$thatyear := .Get 0 -}}
{{- sub $thisyear$thatyear -}}

{{- /* clear trailing whitespace */ -}}
 ‼ To avoid whitespace problems, I use the Go Template trim-whitespace delimiters {{-  and  -}} throughout the above years-since shortcode.

This years-since shortcode is used in the following sections of Infinite Ink articles.

• Endnotes section of Commenting Code Cheatsheet,

• What is Markdown? section of Ordinary and Extraordinary Markdown,

• and right here to state that the number of years Hugo has existed is ~9.

#### ️About variable names in Hugo

To learn about Hugo variable names, see Infinite Ink’s Variable and Parameter Names in Hugo (featuring camelCase⁠🐫 and snake_case⁠🐍).

### 6. get-leaf-text

{{- /*

USAGE:

{{< get-leaf-text "filename" >}}

EXAMPLE:

{{< get-leaf-text "config.py" >}}

Path: layouts/shortcodes/get-leaf-text.html
Purpose: get content of text file located in current leaf bundle

*/ -}}

{{- $filename := .Get 0 -}} {{-$file := .Page.Resources.GetMatch $filename -}} {{-$file.Content -}}

Below are three examples.

#### Example 1: Get and highlight Python code

{{< highlight python "hl_lines=15" >}}
{{< get-leaf-text "config.py" >}}
{{< /highlight >}}

The above example is used in the source of Infinite Ink’s qutebrowser’s Template config.py.

#### Example 2: Get and render Pandoc Markdown

{{% get-leaf-text "mainmatter-get.pdmd" %}}
👆                                    👆
Note                                  Note

#### Example 3. Get and highlight raw (unrendered) Pandoc Markdown

{{< highlight markdown >}}
{{< get-leaf-text "mainmatter-get.pdmd" >}}
{{< /highlight >}}

Examples 2 and 3 are both used in the source of Infinite Ink’s Links and Footnotes in Markdown. Note that Example 2 uses the {{% and %}} shortcode delimiters and Example 3 uses the {{< and >}} shortcode delimiters.

 💡 You are less likely to have a problem if get-leaf-text’s argument (the file name) does not use one of the 13 file extensions that Hugo interprets as a known Markup language (.ad, .adoc, .asciidoc, .htm, .html, .markdown, .md, .mdown, .pandoc, .pdc, .org, or .rst). This is why I use the file extension .pdmd, which is meaningless to Hugo, rather than .pandoc or .pdc, which are meaningful to Hugo, in examples 2 and 3 above.

The highlight shortcode is discussed in shortcode 10 below.

### 7. get-assets-text

{{- /*

USAGE:

{{< get-assets-text "relative/path/to/filename" >}}

EXAMPLE:

{{< get-assets-text "qb/config-fragment1.py" >}}

Path: layouts/shortcodes/get-assets-text.html
Purpose: get content of text file located below assets/text/

*/ -}}

{{- $file := resources.Get (printf "text/%s" ($.Get 0)) -}}
{{- $file.Content -}} The above get-assets-text EXAMPLE is used in Infinite Ink’s…  💡 To learn about the Hugo assets directory, which is where global resources are located, see gohugo.io/hugo-pipes/introduction/. ### 8. html4-get-bundle-image🖼️ {{/* USAGE: {{< html4-get-bundle-image "filename" "alt text" "width" "link" >}} NOTE: parameters 1 & 2 required, parameters 3 & 4 optional EXAMPLE 1: {{< html4-get-bundle-image "gologo.png" "Go Logo" >}} EXAMPLE 2: {{< html4-get-bundle-image "gologo.png" "Go Logo" "50%" >}} EXAMPLE 3: {{< html4-get-bundle-image "gologo.png" "Go Logo" "25%" "https://blog.golang.org/go-brand" >}} PATH: layouts/shortcodes/html4-get-bundle-image.html PURPOSE: get image located in current bundle */}} {{$filename := .Get 0 }}
{{ $alt := .Get 1 }} {{$width := .Get 2 }}
{{ $link := .Get 3 }} {{$file := .Page.Resources.GetMatch $filename }} {{ with$link }}
<a href="{{ . }}">
{{ end }}
<img
src="{{ $file.RelPermalink }}" alt="{{$alt }}"
{{ with $width }} width="{{ . }}" {{ end }} > {{ if$link }}
</a>
{{ end }}

Below are three examples.

#### Example 1: Unspecified width

Shortcode call
{{< html4-get-bundle-image "gologo.png" "Go Logo" >}}

#### Example 2: 50% width

Shortcode call
{{< html4-get-bundle-image "gologo.png" "Go Logo" "50%" >}}

#### Example 3: 25% width and linked

Shortcode call
{{< html4-get-bundle-image "gologo.png" "Go Logo" "25%" "https://blog.golang.org/go-brand" >}}

#### HTML4 img tag vs HTML5 img tag

 ℹ Valid attributes of the HTML img tag changed in HTML5. For example, using width="50%" and width="25%", as I do in examples 2 and 3 above, is valid in HTML4 but invalid in HTML5. That’s why this shortcode is called html4-get-bundle-image. To learn about this, see stackoverflow.com’s Specifying width and height as percentages without skewing photo proportions in HTML. Even though it’s invalid nowadays, it works in most (maybe all) browsers.🤷‍♀️ In both HTML4 and HTML5, it is valid to use alt="" as an img attribute, but you should do this only if an image is for decoration and is not important to the meaning of a web page. For example, the images on Infinite Ink’s #gohugo Portal and #golang Portal are specified with alt="" because they are just for decoration.

#### Bundle image alternative: Use Markdown image syntax

If your leaf bundle looks something like this…

.
└── hugo-tips-fragments/
├── index.md
└── gologo_fuchsia.png

then you can use this Markdown image syntax in index.md to reference the image:

![Alt text](gologo_fuchsia.png)
------------------
👆
path relative to index.md

which renders as:

 ℹ Common Mark’s Markdown image syntax does not support resizing an image. For a relative path like this to work, the image usually must be part of a Hugo bundle.

{{/*

USAGE:

{{% embed-twitter-list listname %}}

EXAMPLE:

{{% embed-twitter-list zeitgeist %}}

Purpose: Embed heading and Twitter list in a page written in AsciiDoc

*/}}

{{ $listbasename := .Get 0 }} === @nm/lists/{{-$listbasename }}

++++

## Endnotes

1. Many Infinite Ink articles, including this one, are evergreen and regularly updated.
3. You can tell that the Hugo functions hugo and site are contextless by noticing that they do not have a leading dot (.). To learn more about these two global functions, see the Hugo v0.53 release notes, especially the relevant GitHub commit and GitHub issues.
4. The Unicode name for the “dash” character (-) is “hyphen-minus,” but its proper name is rarely used. Instead it is usually called “dash,” “hyphen,” or “minus.” Details are at wikipedia.org’s Hyphen-minus.
5. WIP can mean “work in progress” or “writing in progress.”
6. The front matter of some of Infinite Ink’s WIP articles include draft: true. Only the titles of the WIP articles that are not drafts are listed when I publish Infinite Ink’s To-⁠Do, Doing, and Done Lists⁠👷‍♀ page.
7. By “multiple markup comments,” I mean markup comments that are not omnicomments. This gets around the problem of trying to nest markup comments, which does not work (because the first close-comment markup tag will end the outermost open-comment markup tag).
8. Another example of a shortcode and function having the same name: the relref shortcode and relref function.

## Comments and questions 📝 👍 👎 🤔

Your public comment or question might immediately improve this page or help me to (eventually) improve this page.