How to limit number of tags printed

I use range in .Site.Taxonomies.tags to iterate through tags. This works as it goes through the map but unfortunately it prints all the tags. How to limit number of tags shown ? Since this range is a map I cannot use array functions like first 5 etc. This is my code:

{{ range $name, $items := .Site.Taxonomies.tags }}
<div class="tag">
<a href="{{ $.Site.BaseURL }}/tags/{{ $name | urlize | lower }}" class="tag-text">{{ $name }}</a>
</div>
{{ end }}

There are tons of examples in the forum about range first (use the search button) and of course it’s in the docs

Seems that you didn’t read the question carefully. You cannot use first with Maps

Ok. I’m sorry I misread your post.
But why do you need it as a map? What you want to do is not supported.

1 Like

Maybe I’m getting you wrong - but what about:

{{- $.Scratch.Set "myLimit" 0 }}
{{- range $name, $items := .Site.Taxonomies.tags }}
  {{- $.Scratch.Set "myLimit" (add ($.Scratch.Get "myLimit") 1) }}
  {{- if le ($.Scratch.Get "myLimit") ($.Site.Param `tagsLimit`) }}
    <div class="tag">
      <a href="{{ $.Site.BaseURL }}/tags/{{ $name | urlize | lower }}" class="tag-text">{{ $name }}</a>
    </div>
  {{- end }}
{{- end }}

with config.toml

[params]
tagsLimit = 5
3 Likes

@it-gro this is very cool! Thanks! :four_leaf_clover:

1 Like

Thank you it-gro, works like a charm!