Dynamically range in taxonomy?


I’m trying to create my own breadcrumb navigation that will also show the page number in case of paginated pages. It was easy to integrate it for my section pages and I’ve got it working to some extent for my taxonomy term page also, however, it’s limited to just one taxonomy.

Here’s my code:

<!-- rest of the code -->

{{ else if eq .Kind "term" }}

  <!-- parent links in the breadcumb -->

  {{ range .Site.Taxonomies.tags }}

    <!-- rest of the code -->

  {{ end }}

{{ end }}

So, instead of declaring .Site.Taxonomies.tags, I want to dynamically fill in the taxonomy name, depending on which taxonomy page is being rendered. For example, on a page showing posts tagged ‘A’, the breadcrumb should range though .Site.Taxonomies.tags and if the page is showing posts categorized ‘B’, the breadcrumb should range though .Site.Taxonomies.catgories.

I tried:

  1. {{ range .Site.Taxonomies.(lower .Section) }}, where .Section is returning Tags and Categories. I got the error unexpected <.> in operand.

{{ $taxonomy:= (printf ".Site.Taxonomies.%s" (lower .Section)) }}

{{ range $taxonomy }}

I got the error: range can't iterate over .Site.Taxonomies.tags

But, it’s working perfectly fine if I statically assign it like {{ range .Site.Taxonomies.tags }}.

Here’s my complete breadcrumb partial:

{{- $foo := "tags" -}}
{{- range (index .Site.Taxonomies $foo) -}}
{{- end -}}

Works perfectly. Thanks a lot!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.