Creating taxonomic link partial for multilingual translations

I currently have two taxonomies I’m managing on my site:

  • tags
  • categories

I loop over these in my layout to output a comma-separated list of terms for a post byline, e.g.

Published by Josh Habdas 9 Mar, 2017 in technology and design and tagged javascript, open source, programming and web development using 881 words.

When output, individuals terms are converted to hyperlinks using apply with a partial:

{{ with .Params.categories }}
  in <span itemprop="articleSection">{{ delimit (apply (apply (sort .) "partial" "post/category-link.html" ".") "chomp" ".") ", " " and " }}</span>
{{ end }}
{{ with .Params.tags }}
  and tagged {{ delimit (apply (apply (sort .) "partial" "post/tag-link.html" ".") "chomp" ".") ", " " and " }}
{{ end }}

With the following partials used, shown here combined for brevity:

<a href="{{ "/" | relURL }}categories/{{ . | urlize }}/">{{ . }}</a>
<a href="{{ "/" | relURL }}tags/{{ . | urlize }}/">{{ . }}</a>

I’d ideally like to DRY up this code, but moreso I want to remove the hardcoding of the taxonomy name from within the partial URL, so I can control taxonomy names from config without forcing a refactor. Something like this:

<a href="{{ "/" | relURL }}{{ ($.Site.GetPage "taxonomy" "categories").Data.Plural }}/{{ . | urlize }}/">{{ . }}</a>

But when do I receive the following error:

ERROR: 2017/05/02 13:43:47 general.go:236: theme/partials/post/category-link.html is an incomplete or empty template

The same holds true anytime I try and access $.Site from within a partial applied in this manner.

Assuming I’m doing something dumb (because I usually am), why does the above snippet throw an error? And what would be the suggested approach to handle this scenario for the purposes of a translated site without altering the way with which I’m outputting the byline?

@jhabdas This is still technically checking for tags, but if it’s about getting rid of the hardcoded taxonomy name:

{{ with .Params.tags }}
    {{ range . }}
        <a href="{{with $.Site.GetPage "taxonomyTerm" "tags" }}{{ .Permalink }}{{end}}{{.}}">{{.}}</a>
    {{ end }}
{{ end }}
1 Like

Thank you for the snippet. Like $.Site, use of $.Permalink is not available and throws when I apply it to my partials. It seems as though partials applied using delimit do not have access to the $ scope. And I need delimit so I can properly format and punctuate the byline (as far as I know), as sacrificing grammar will impact the quality of translations and accessibility of the site.

Sorry I wasn’t of more help, but if the idea is that you want to really genericize the templating and just have the output reflect whatever taxonomies are in a configuration while still avoiding any hardcoded values, this is as far as I got in local testing. You’ll still have to be clever w/r/t your commas, but I can check on this tomorrow :smile:

{{ range $tax,$val := .Site.Taxonomies }}
	        {{with $.Param $tax}}
	        	{{ $taxlen := sub (len .) 1}}
		        {{ range $ind,$val := . }}
		        	<a href="{{with $.Site.GetPage "taxonomyTerm" $tax}}{{.Permalink}}{{end}}{{.}}">{{.}}{{if lt $ind $taxlen}}, {{end}}</a>
		        {{ end }}
	        {{ end }}
        {{ end }}

Need to work on the , and for the final values, but maybe the above helps?

1 Like

…I’m going out on a limb and guessing that CSS pseudo selectors are totally out of the question for the comma delimiting…:bowing_man:t2:

Firefox doesn’t read them. And I’m thankful for that, because they’re often used for applying style. If you’re ever curious Cmd+F5 on macOS to open VoiceOver and then point both Chrome and Firefox at https://hackcabin.com/, close your eyes and try and imagine being a blind user as Chrome reads the pseudo-applied === below the headers :cry:.

Note on spec, in Chrome even using the media attribute in an attempt to disable aural,speech user agents from reading pseudo elements doesn’t work. Sure glad I can see! But for translations we need real content either way, and I also have a requirement of keeping this code like poetry because it’s for a poem I’m writing titled After Dark.

Yeah, I know it’s bad news…hence the bowing emoji.

But I think that what I started above is pretty darn abstracted in that you can use it with literally any taxonomy. You’ll just have to add some ugly if-else statements to take care of the conjunction/serial comma.

We can do better.

I’m looking at the delimit tests and it looks like we can pass a map so I may try and use that to squeak more than just a simple string into the link partials.

The following felt promising:

{{ $plural := ($.Site.GetPage "taxonomy" "categories").Data.Plural }}
{{ delimit (apply (apply (sort .) "partial" "taxonomy-link.html" (dict "taxonomy" $plural "term" .)) "chomp" ".") ", " " and " }}

Where taxonomy link contains:

<a href="{{ "/" | relURL }}{{ .taxonomy }}/{{ .term | urlize }}/">{{ .term }}</a>

But doesn’t work for multiple items because not using the "." syntax desired by apply.go#applyFnToThis, sends an []string (string array) into the partial when what I need the actual value which changes with each iteration of delimit.

There are a number of related threads in the discussion forum and IMO none of them provide a satisfactory answer so I’m going to restate my original question again in hopes this thread doesn’t turn into a deluge of “here’s how I did it” responses:

Assuming I’m doing something dumb (because I usually am), why does the above snippet throw an error? And what would be the suggested approach to handle this scenario for the purposes of a translated site without altering the way with which I’m outputting the byline?

I’d really like to figure this out so I can translate my theme.

…or 120-word comments on accessibility. Your original post has been edited 4 times.

One piece to consider, as per my previous comments, is the difference between taxonomyTerm and taxonomy.

I hope you find the exact solution you are looking for :smile:

@jhabdas After seeing @moorereason 's clever use of Scratch here, I’m wondering if he has any suggestions:

https://discuss.gohugo.io/t/coding-challenge-create-array-from-value-in-array-of-maps-in-frontmatter/6421/3?

I greatly appreciate your help. But this is exactly what I didn’t want to happen. Here’s my question again:

Assuming I’m doing something dumb (because I usually am), why does the above snippet throw an error? And what would be the suggested approach to handle this scenario for the purposes of a translated site without altering the way with which I’m outputting the byline?

I doubt I’d use a Scratch here. Back to your original attempt to solve the issue, consider passing a slice to the partial.

{{ with .Params.categories }}
  in <span itemprop="articleSection">{{ delimit (apply (apply (sort .) "partial" "post/category-link.html" (slice "." "$")) "chomp" ".") ", " " and " }}</span>
{{ end }}

Then update your partial to access the items by index. For example,

$tag := index . 0
$page := index . 1

I didn’t mean to imply Scratch was the answer as much as after reading that realized that you would probably have a better suggestion for @jhabdas than the hackery I was putting together…

…all within the context of making sure he gets the exact answer he is looking for :smile: Thanks @moorereason .

1 Like

This look very promising. I’d seen your use of slice while sleuthing around the repo yesterday. I will give this a shot soon and let you know how it goes as it appears to do exactly what I was looking for. Regardless, I want to lay down a big thanks to everyone for being patient as I grumble my way towards an elegant solution. :imp:

1 Like