Comma-separated tags

I was using this to list the tags, translate them and show quantity:

{{ range $name, $items := .Site.Taxonomies.tags }}
<li class="small">
  <a class="cat-item d-flex justify-content-between text-dark"
  href="{{ `tags/` | relLangURL }}{{ T $name | urlize | lower }}">{{ $name | humanize }}<span>{{len $items}}</span></a>
</li>
{{- end }}

This worked for single tags, but now I want it to work for comma-separated tags.

I’ve seen this post:

This would be ideal, but I could not make it work.

At least maybe there is a way to use the T function with comma-separated tags?
Thanks

I must admit that I did not get the complete thing you want to have as result.

Your loop ranges one tag at a time and you create a link and the number of pages. and you have an li around each entry. where do you want to place a comma.

something like

  • one,
  • two

would be rather uncommon cause the

  • already works like that

    at least for me it would be good to have an example:

    • input
    • output

    for now here’s a list of translated tags , separated – if that helps :wink:

    {{  delimit (apply .Params.tags "T" "_") ", "}}
    

    The difference with yours and the one from the solution is that you range over

    • the tag name
    • list of pages

    where as the solution in the example issue loops over

    • index
    • name

    no index to check and in your case. I guess you want the , inside the <li>

    <ul>
       {{ $idx := 0 }}
       {{ $len := sub (len .Site.Taxonomies.tags) 1 }}
       {{ range $name, $items := .Site.Taxonomies.tags }}
          <li>
             <a href="{{- `tags/` | relLangURL -}}{{- T $name -}}">{{- $name | humanize -}}</a>&nbsp;{{ len $items }}
             {{- if lt $idx $len -}},{{ end -}}
          </li>
          {{ $idx = add $idx 1 }}
       {{ end }}
    </ul>
    

    p.s. href simplified

  • Dear irkode, thank you very much for looking into this!
    Sorry I didn’t explain my case well enough - I’ll try to once again:

    With frontmatter tags key being at the top level of data, looking like this (1 tag per file):

    tags: 'womens-tops'
    

    I used this tag filter to find files having the tag and show their number:

    <li>
      {{- if isset .Site.Taxonomies "tags" }}
      {{- if not (eq (len .Site.Taxonomies.tags) 0) }}
      <ul class="list-unstyled">
          {{ range $name, $items := .Site.Taxonomies.tags }}
          <li class="small">
            <a class="cat-item d-flex justify-content-between text-dark"
            href="{{ `tags/` | relLangURL }}{{ $name | urlize | lower }}">{{ T $name | humanize }}<span>{{len $items}}</span></a>
          </li>
          {{- end }}
      </ul>
      {{- end }}
      {{- end }}
    </li>
    

    The output was a clickable link something like this:

    Womens tops       3
    

    Now the situation evolved to 1 file holding data on more than 1 product and there is necessity to have a few tags at the top level of data (taxonomy does not work with nested tags):

    tags: ["womens-tops", "womens-pants"]
    

    and I am trying to adjust the search code to accomodate a few tags.

    The ideal output would be clickable translated links something like this:

    Womens tops       3
    Womens pants      2
    

    where 2 and 3 are number of items having the tags, but so far I could not achieve it.
    The best I could achieve so far - with top-level frontmatter key (without suare brackets):

    tags: “womens-tops”, “womens-pants”

    the output would be a clickable link of the kind:

    Womens tops, Womens pants      1
    

    where 1 is a number of files having the tags, and at that I can not apply the T function to translate the tags, even using the delimit function.

    I tried your filter code, but it does the same thing that my code does.

    jmooring had a good idea here: Add commas between tags range, but I could not make it work.

    I will appreciate if you can suggest any solution for my problem.
    Thanks!

    This for me looks pretty much the thing you have and I did.

    comment: except the comma, and I mixed up name and translation (corrected in repo below)

    guess I’m still missing something. there’s no search involved here… just a range

    to adjust the search code to accomodate

    sometimes it’s best to have a REAL example to visualize. So I pushed my stuff to a repo to show what I understood.

    • I took a standard hugo new theme which has three posts.
    • I changed the frontmatter to use your tags
    • I added a translation file i18n/en.yaml (just prepends a EN to see translation works)
    • I edited layouts/_default/single.html to show the tags.

    it produces clickable links on the post page to the tags - try it out:

    git clone --depth 1 --single-branch -b  topic-52634-commaTags https://github.com/irkode/hugo-forum topic-52634-commaTags
    cd topic-52634-commaTags
    hugo server
    

    please check out the files match your intended stuff

    • front matter in pages
    • translation
    • single partial
    • RESULT HTML

    If that does not match your intention please elaborate

    Yes, I should better call if a filter than search.
    Here is the real thing: GMall
    It filters out files that have the tags mentioned in the filter.
    I’m trying to relate your model to my situation.
    Yes, I see there are 2 content files, both with tags of type:

    tags: ["womens-tops", "womens-pants"]
    

    and I see that they are separated. They should point to the same file. Figuring out.
    Thank you!

    In fact I though my model was yours having tags translated and linked to the tags page … which imho is the way tags behave in hugo

    sounds like you want to have two tags but only have one page for the tags… sure taxonomy is the right thing for that?

    Guess it won’t make sense to code here without the - for me still hidden - big picture

    Hints Your site:

    • at the homepage has price filters which I assume are not your tags…so another different thing
    • after selecting some deeper site I got locked out by my browser because of third-party-cookies that I reject per default (in fact chrome does). at least for Germany this will be a legal problem cause you have no cookie info and selection when I start visting.
    • the modile style is far tzo small. the items inside the filters are nearly unreadable

    sry to be of not more help here

    At last I’ve got the filter working and it works fine - splits and translates the tags, and has a correct clickable link! It didn’t work because of a typo in frontmatter - the tags were wrongly quoted.
    Thank you for your hints. Please explain more clearly the 1st and the 2nd one. Locked out of your browser??
    The 3rd one - make text bigger, or images as well?
    Thank you!