How to get the category name when in a range?

Hey,

I’m working with a range, for example:

{{ range first 1 .Pages }}
    <p><a href="{{ .RelPermalink }}">Continue reading...</a></p>
    <h1>{{ .Section.Title }}</h1>
    <h2>{{ .Title }}</h2>
    <p>{{ .Summary }}</p>
{{ end }}

This code is in category.html in my theme. I’d like the H1 element to display the category name, but I can’t seem to figure out how to do that. Is there a variable for getting single category names from category list pages?

Best Regards,
Jonathan Clarke

I guess I could upgrade to v0.48 and use variables like so:

{{ $cat := .Title }}
{{ range first 1 .Pages }}
    <p><a href="{{ .RelPermalink }}">Continue reading...</a></p>
    <h1>{{ $cat }}</h1>
    <h2>{{ .Title }}</h2>
    <p>{{ .Summary }}</p>
{{ end }}

But that feels like overkill.

Try:

  <h1>{{ $.Title }}</h1>
1 Like

Works! Thanks, bep!

Also, note that you don’t need v0.48 (or Go 1.11) to use the variable construct you mention. The problem pre 0.48 was when setting variables on the inside.

1 Like

OK, good to know, thanks.