I have an list of several tags.
I can get first N or last N, but I don’t know exactly how many tags I have.
How can I split them?
I use this snippet
<ul class="tags">
{{ $posts := where .Site.Pages "Type" "post" }}
{{ range $name, $taxonomy := .Site.Taxonomies.tags }}
<li><a href="/tags/{{ $name | urlize }}/">{{ $name | humanize }} ({{ $taxonomy.Count }})</a></li>
{{ end }}
</ul>
So I worked out a solution but it a misread on {{ $posts := where .Site.Pages "Type" "post" }}
, which as a set of pages can be iterated through pretty easily using a combination of div
and first
and after.
But what I think you’re trying to do is list all the tags from your taxonomy into two columns. Since you can’t use first
and last
with taxonomies, at least from some very simple local testing, you can use $.Scratch
here. Note: I feel like this is a bit hackish and verbose, so maybe someone else has a better suggestion:)
{{ $sitetags := .Site.Taxonomies.tags }}
{{ $tagshalf := int (div (len $sitetags) 2) }}
{{ $.Scratch.Set "firsthalf" 0 }}
{{ $.Scratch.Set "secondhalf" 0 }}
<ul>
{{ range $name, $taxonomy := $sitetags }}
{{ $.Scratch.Add "firsthalf" 1}}
{{ if le ($.Scratch.Get "firsthalf") $tagshalf }}
<li><a href="/tags/{{$name | urlize}}">{{ $name }} ({{ $taxonomy.Count }})</a></li>
{{ end }}
{{ end }}
</ul>
<ul>
{{ range $name, $taxonomy := $sitetags }}
{{ $.Scratch.Add "secondhalf" 1}}
{{ if gt ($.Scratch.Get "secondhalf") $tagshalf }}
<li><a href="/tags/{{$name | urlize}}">{{ $name }} ({{ $taxonomy.Count }})</a></li>
{{ end }}
{{ end }}
</ul>
@khabaroff Of course this depends on your browser support, but you might want to consider CSS columns:
http://caniuse.com/#search=column
1 Like
That’s awesome! Thank you!
1 Like