Listing members of a taxonomy in shortcode

Is it possible to list the members of a taxonomy in a shortcode? I have a taxonomy of “states” and the following frontmatter:

+++ states = ["MA", "CT"] +++

The shortcode is:

{{ range .Page.Site.Taxonomies.states.ct }} {{ .Render "summary" }} {{ end }}

I have also tried:

{{ range $.Site.Taxonomies.states.ct }}

But nothing shows up. There is no error message. What am I doing wrong? How can I get a list of available taxonomy terms and their template variable names for debugging? (I just had to guess that “CT” would become “ct” from the docs.)

Have you tried dropping the .Page context to make it use .Site.Taxonomies ?

Yes, that didn’t work either. :frowning:

I think you’re doing it wrong. Have you tried a dev version of hugo which may give you more info on failures?

I get the following errors when testing your shortcode:

ERROR: 2016/07/20 07:19:07 shortcode.go:565: error processing shortcode shortcodes/test.html
 ERR: template: shortcodes/test.html:2:3: executing "shortcodes/test.html" at <.Render>: Render is not a field of struct type hugolib.WeightedPage
  1. The .Render function appears to be only available on Page content, not Taxonomies. Inside your range, you are in the context of a Taxonomy entry.
  2. By performing a range over .Site.Taxonomies.[taxonomies].[term] you are then operating in the context of the Term, not the array of terms.

Hugo by default renders page content in list context at /[taxonomy]/[term]/ using the template in layouts/_default/list.html but I think you want to include something like that inside your page content.

Probably what you want to do is this:

{{ $state := .Get "state" }}
{{ with (index .Page.Site.Taxonomies.states $state) }}
{{ range .Pages }}
{{ .Render "summary" }}
{{ end }}
{{ end }}

then call the shortcode as

{{% myshortcode state="ct" %}}

Thanks, switching to that at least causes an error message. With hugo I have a really hard time figuring out what values I am getting out of things. Your code (copied verbatim) causes this error:

Started building site ERROR: 2016/07/20 15:20:35 shortcode.go:565: error processing shortcode shortcodes/practice_areas.html ERR: template: shortcodes/practice_areas.html:6:11: executing "shortcodes/practice_areas.html" at <index .Page.Site.Tax...>: error calling index: index of untyped nil

The reason for the shortcode is we need to put these content cards within some page content, but not all. Hugo doesn’t seem to have a way to make a specific page that uses its own entirely custom template as a one-off.

Using hugo 0.16? Have you tried with HEAD?

Hi @waddles, I too get stumped with no error messages when running my Hugo locally. How do I get the “dev version” ? Do I need to install Go for that?

@Shiva https://gohugo.io/overview/installing/#installing-from-source yes but go makes it really easy compared to traditional build environments.

1 Like

Thanks.

I finally got around to installing hugo from source, and yes I got the same error message error calling index: index of untyped nil

This would be a lot easier to debug if there were a way to get hugo to just print out the contents of all the structs. I am honestly just guessing, there has to be a better way.

I would say no, but I’m currently reworking this, so it will soon.

Watch this issue:

Thanks! By the way I understand that this isn’t what a shortcode is for, the issue is we have a one-off page that needs to have a lot of specific data on it. As far as I can tell, there is no way to create a one-off layout for a single page.

There is, but it isn’t very well documented.

Add it as a HTML file to /content with no front-matter, i.e. /content/one-off.html. It will not get any layout, but you should be able to list taxonomies etc.

Hmmm, it was working for me but I’m not sure I did a dry run so it may have been due to Iterating over items in a taxonomy does not always work · Issue #2323 · gohugoio/hugo · GitHub