First/Last item of iteration

Hello,

I recently imported my WordPress blog to Hugo. Still working on the details and on getting my theme over to Hugo.

How can I test a loop if I am in the last iteration?

Sample:

<div class="col-md-9 is--taglist">
  <i class="icon-tags" aria-hidden="true"></i>&nbsp;
  {{ with .Params.tags }}
    {{ range . }}
      <a href="{{ "tags" | absURL }}{{ . | urlize }}" rel="tag">{{ . }}</a>,
    {{ end }}
  {{ end }}
</div>

This would print a list of tags, separated by comma. How can I prevent a comma after the last tag? If that is a very simple thing in Go I would appreciate a nudge into the direction of the right documentation :wink:

See: delimit

Hmm, that would work if my tags would not be wrapped by links or can I use delimit like a wrapper?

Right. See this old thread. The tip in the first post should have you covered. Also there is a more elaborate workaround further down.

1 Like

perfect, thanks!

my final partial looks like this:

  {{ with .Params.tags }}
    <i class="icon-tags" aria-hidden="true"></i>&nbsp;
    {{ range $i, $e := . }}
      {{ if $i }}, {{ end }}
      <a href="{{ "tags/" | absURL }}{{ $e | urlize }}" rel="tag">{{ $e }}</a>
    {{ end }}
  {{ end }}
7 Likes

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