How do I determine if a particular taxonomy is being listed in taxonomy.html?

In /layouts//default/taxonomy.html, I use

{{ define "main" }}

{{.Title}}  {{ .Type | singularize | title }} Archive

etc....

to output the title of the taxonomy (tag or category) and if the page is listing that taxonomy (tag or category) archive

But how can I determine if one particular tag or category is being listed so I can add some specific content for that taxonomy?

I.e., how do I do this?

{{ if taxonomy eq “my-taxonomy-name” }}
do this
{{ end }}

I do that on my website like this:

I have added the following to the top of layouts/_default/baseof.html: {{ .Scratch.Set "currentPage" .Permalink}}

and then, I call my block with full scope like this: {{ block "main" . }}

and in the block, I use something like:

{{ range .Params.tags }}

    {{ if in ($.Scratch.Get "currentPage") . }}

        {{ $.Scratch.Set "class" "customClass" }}

        {{ else }}

            {{ $.Scratch.Set "class" "" }}
		
    {{ end }}

    <a class = "{{ $.Scratch.Get `class` }}">

       <!-- content -->

    </a>

{{ end }}

So, as you can see, I use it to add a custom class to that element and handle the rest in CSS. That’s working fine for me. You can see a working demo on my website: https://hrishikeshk.netlify.app/index.html (if you navigate to any taxonomy page, you’d see that particular taxonomy disabled).

I don’t know if there’s any better solution available, but, if there’s, I’d like to know too, because the problem in using my method is that, the URL can’t contain any of the taxonomy words.

I typically do this sort of thing by checking the value of the URL from within the relevant list template.

Something like: {{ if in .Permalink “my-taxonomy-name” }}<--- ... --->{{ end }}

Much simpler than .Scratch constructs.
Fast also.

Thanks! That works. I didn’t know Hugo could parse URLs.

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