How to get current taxonomy?

Hello, in my current code I have

{{ $currentCategory := .Params.categories }}

and this worked because I only had one taxonomy that I used, called category.

However I now have more taxonomies, and I want $currentCategory to be whatever taxonomy the current page belongs to.

Example for categories:

      {{ if .Params.categories }}
      <div>
        {{ range .Params.categories }}
        <a href="category/{{ . | urlize }}/">{{ . }}</a>
        {{ end }}
      </div>
      {{ end }}

for tags:

        {{ if .Params.tags }}
          <footer>
              <ul>
                  {{ range .Params.tags }}
                  <li>#<a href="{{ "tag/" | absURL }}{{ . | urlize }}/">{{ . | lower }}</a>
                  </li>
                  {{ end }}
              </ul>
          </footer>
          {{ end }}
1 Like

Okay this makes sense, however what’s actually happening when I try to implement it doesn’t make sense to me:

This works:

{{ $currentCategory := .Params.categories }}
{{ print $currentCategory }}

However, this results in an undefined variable:

{{ if .Params.categories }}
    {{ $currentCategory := .Params.categories }}
{{ else if .Params.illustrations }}
    {{ $currentCategory := .Params.illustrations }}
{{ end }}
{{ print $currentCategory }}

if I print the $currentCategory within the if block, then it prints just fine, but it seems as if the variable gets erased after the if block closes? What’s going on?

How are you defining these taxonomies? Does it mean some posts have more than one taxonomy? Please share your code.

Posts have one taxonomy, either categories: category-name or illustrations: illustration-name or rule34: r34-name

This is what I’m trying to modify: layouts/partials/prev_next_post.html · master · indeedwatson / cheeky-icarus · GitLab

Have you noticed range, which will bring what’s needed?

I did but I’m not sure how to adapt that logic to my current code, where all I need to do is define a variable.

In any regular programming language, I feel this would be a very simple thing to do, such as in the if block that I defined.

Additionally, I copy pasted your code and it results in an error: can’t iterate over category-name

Could you paste example of your frontmatter to see how you specify your categories etc.

title: page 12
categories: bath-house
date: 2022-03-01
page-number: 012
thumb: "/img/Bath House/thumbs/012.png"
---

{{< figure src="/img/Bath House/012.png" alt="page 12"
link="/img/Bath House/012.png" target="_blank">}}

Could you try to change this bit to

categories: 
- bath-house

Every category in separate line with - on front
And use my code for categories. Paste output/error in full here to look at as it shall work.

Ps. Avoid white spaces in your file names for images as this can cause issues on various hosting environments (for example can work locally but will not work online). Use underscore instead.

categories:

  • bath-house

I implemented this and now your code returns no errors. However, I still don’t understand how your code addresses my specific use case, or how to adapt it.

I’d really appreciate an explanation for why the if block that I posted, to define $currentCategory is not working. What am I doing wrong? Why does the variable get defined while inside the if block, but then doesn’t exist outside of it? This is very confusing and counter intuitive.

thanks, appreciate the tip

Variables initialized ( := ) within a block are not available outside of the block.
Initialize ( := ) before the block, then assign ( = ) within the block.

{{ $foo := "" }}
{{ if true }}
  {{ $foo = "wibble" }}
{{ end }}
{{ $foo }}  --> wibble

See https://pkg.go.dev/text/template#hdr-Variables

@jmooring You have saved me once again, thank you!

My code looks a bit messy now but it works.

@idarek There’s a simpler way to render terms:

{{ with .GetTerms "tags" }}
  <p>Tags:
    {{ range . }}
      <a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
    {{ end }}
  </p>
{{ end }}

See https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template

3 Likes

Hi Joe. Is there a difference between $foo := "" and $foo := slice?

{{ $foo := "" }}
{{ printf "%T" $foo }} --> string

{{ $foo := slice }}
{{ printf "%T" $foo }} --> []interface {}

{{ $foo := "" }}
{{ if true }}
  {{ $foo = slice "a" 1 }}
{{ end }}
{{ printf "%T" $foo }} --> []interface {}

{{ $foo := slice }}
{{ if true }}
  {{ $foo = "a" }}
{{ end }}
{{ printf "%T" $foo }} --> string
6 Likes

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