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.)
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
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.
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 }}
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.
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.
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.
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.