How do I delimit a taxonomy list?

Hello - I wonder if someone more experienced could give me a hand. I’ve got a partial “listtop.html” which I use at the top of various lists, with the goal of having its contents show or hide depending upon the context.

One aspect of the partial is, I’d like to show a linked list of my taxonomy “topics”. This works:

{{ range $name, $taxonomy := .Site.Taxonomies.topics }}
  <a href="/topics/{{ $name | urlize }}"> {{ $name }}</a>, 
{{ end }} 

… but it has a small irritation. The comma shows at the end of the list.

I therefore tried the technique described on this page in the docs, where a single post template is listing tag links, but removing the trailing comma with delimit. To wit, my single has:

...
{{ partial "post/tag/list" . }}
...

Then layouts/partials/post/tag/list.html is:

{{ with .Params.tags }}
<div class="tags-list">
  <span class="dark-red">Tags</span><span class="decorative-marker">//</span>
 {{ delimit (apply (apply (sort .) "partial" "post/tag/link" ".") "chomp" ".") ", " }}
</div>
{{ end }}

And the layouts/partials/post/tag/link.html it’s calling is:

<a class="post-tag post-tag-{{ . | urlize }}" href="/tags/{{ . | urlize }}">{{ . }}</a>

This works fine for a single.html, and it serves up a clean list of linked tags, with no trailing comma. Now however, when I try this same technique for a taxonomy overall, I cannot seem to get it to work.

I tried:

{{ with Site.Taxonomies.topics }}
<div>
    {{ $sort := sort . }}
    {{ $links := apply $sort "partial" "topic_link" "." }}
    {{ $clean := apply $links "chomp" "." }}
    {{ delimit $clean ", " }}
</div>
{{ end }}

… calling layouts/partials/topic_link.html:

<a href="/topics/{{ . | urlize }}">{{ . }}</a>

The above gives the error:

ERROR: 2015/04/26 html/template: "partials/topic_list.html" is an incomplete template in partials/topic_list.html”.

I tried variations, like with "Site.Taxonomies.topics" to force a string, but in that case, I get an error related to sort.

It appears that taxonomies are a different animal, but I am not sure what kind of animal!

I wonder, can someone point me in the right direction? Thank you in advance.

Also, does anyone know if there is a reason to put a partial in a sub-path?

As above, if I put a partial in post/tag/ for instance, does that have a functional purpose, such as to target the partial only to tags in posts, for instance.

Or is it simply logical organization?

Simply logical

1 Like

Thanks @spf13 :slight_smile:

Ok, @spf13 found what might be a bug in hugo or (the more likely) my code :slight_smile:
My hugo version:

~ % hugo version
Hugo Static Site Generator v0.14-DEV BuildDate: 2015-05-02T08:58:57+09:00

I have my single.html loading a partial to list my tags, delimiting so there’s no trailing comma. I’m using the example from the docs,

{{ delimit (apply (apply (sort .) "partial" "post/tag/link" ".") "chomp" ".") ", " }}

When I run hugo server, I get this error:

ERROR: 2015/05/02 template: partials/post/tag/list.html:4:20: executing "partials/post/tag/list.html" at <apply (sort .) "part...>: error calling apply: Too many arguments in partials/post/tag/list.html

… and the tags list is not rendered in my single.html.

It’s easy enough to revert to the more verbose example with a variable on each line, but, is this method of listing no longer valid in --HEAD?

fyi, @bep fixed this with this commit. Thanks @bep!