Shortcodes - template

While investigating snippets and shortcodes I’ve come across “template” in an example shortcode. Attached code has the following use:
{{- template “tip” . -}}

I can figure out what it does and when I use the code it seems clear but I’d like to read about it in the documentation to be 100% certain on it. Can’t find it anywhere in the docs.

{{- if .Inner -}}
  {{- template "tip" . -}}
{{- end -}}
{{- define "tip" -}}
{{- $title := cond (.IsNamedParams) (.Get "name") (.Get 0) -}}
<div class="shortcode-tip">
  {{- with $title -}}
  <h4 id="{{ . | urlize }}">{{ . }}</h4>
  {{- end -}}
  {{ safeHTML .Inner | markdownify }}
</div>
{{- end -}}

Thanks

Shortcodes are typically custom made, except for the few built-in shortcodes.

The one above defines a custom tip template that takes both a named or a positional parameter (see: here).

For a further explanation you should contact the author of the theme you’re using.

1 Like

For further explanation, read this page first: Create Your Own Shortcodes | Hugo :wink:

1 Like

Thanks, I’m not using a theme as I find building my own is great way to get more familiar with as much as possible.

I found the Hugo docs helpful and I know how to create a shortcode but when I went to look at some custom shortcodes such as the ones I found on Forestry’s GitHub I came across template.
https://github.com/forestryio/forestry.io/blob/master/hugo/layouts/shortcodes/tip.html

As I said I can understand that if there is no value in the snippet (.Inner) it won’t do what’s inside the define “tip”. I’m not really interested in the specifics of this shortcode I was just investigating shortcodes in general but I hadn’t come across template before.

Is template actually in the Hugo documentation? It appears to be similar to partial but I can’t find it anywhere in the documentation.

The template function is documented over here:

For a discussion about template vis-à-vis partial see this topic:

2 Likes

Perfect, that’s exactly what I was looking for. Not sure why that wasn’t appearing when I used the “Search the Docs”. Thanks