It's unbelievable how difficult is to find solid information about slightly more advanced techniques with Hugo.
After quite a while searching I’ve found this article with a very nice way to implement breadcrumbs for Hugo.
I modified it a little bit to not add an unnecessary link to the last trail and to make use of i18n to translate the URL segments into something more human-readable. I also made a small workaround to remove some unwanted trails that doesn't match a translated entry:
{{- $url := replace .Permalink ( printf "%s" ( "/" | absLangURL ) ) "" -}}
{{- $url := replace .Permalink ( printf "%s" ( "/" | absLangURL ) ) "" -}}
{{- $.Scratch.Add "path" ( "/" | absLangURL ) -}}
{{- $.Scratch.Add "breadcrumb" (slice (dict "url" ( "/" | absLangURL ) "name" "home" "position" 1 )) -}}
{{- range $index, $element := split $url "/" -}}
{{- $.Scratch.Add "path" $element -}}
{{- $.Scratch.Add "path" "/" -}}
{{- if ne $element "" -}}
{{- $.Scratch.Add "breadcrumb" (slice (dict "url" ($.Scratch.Get "path") "name" . "position" (add $index 2))) -}}
{{- end -}}
{{- end -}}
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [{{ range $.Scratch.Get "breadcrumb" }}{{ if ne .position 1 }},{{ end }}{
"@type": "ListItem",
"position": {{ .position }},
"item": {
"@id": "{{ .url }}",
"name": "{{ .name }}"
}
}{{ end }}]
}
</script>
<nav class="breadcrumb">
{{- $length := len ($.Scratch.Get "breadcrumb") -}}
{{- range $index, $.Scratch.Get "breadcrumb" -}}
<!--
Assigning a constant as default value for i18n function
if it doesn't match any entry
Then check if current Breadcrumb item matches this constant,
which means that part shouldn't be one of Breadcrumb's trail
-->
{{- $i18n := i18n ( print "breadcrumbs-" .name ) | default "__INTERNAL__" -}}
{{- if not ( eq ($i18n) "__INTERNAL__" ) -}}
{{ if eq ( (int .position) ) $length }}
<span class="breadcrumb-item active">{{ $i18n }}</span>
{{- else -}}
<a class="breadcrumb-item" href="{{ .url }}" >{{ $i18n }}</a>
{{- end -}}
{{- end -}}
{{ end }}
</nav>
It works very nicely except that as far as I could notice so far while navigating through Pagination links everything starts duplicating N times as far as the Pagination Page increases (i.e twice of Page 2, thrice for Page 3 and so on).
I've searched even more and found a similar implementation in which there's no duplication... but the links are all messed up, the URL of each trail is a concatenation of all links of all trails o.O
I try to avoid asking things on Hugo's "forum" because the answers given there are most of the times hard to understand, incomplete or with "pseudo-pseudo-codes".
Does anyone know how to make this work and could lend me a hand?
I've been forced to stop coding but since this question popped up a notification for me today, let me show what I did to solve the problem proposed in the question back there, even though I can't really explain why this happens -OR- if this still happens nowadays in newer versions of Hugo (I stopped using v0.28)
Well, after MUCH testing, I simply initialized a Template Variable with the scope of the “dot” that’s passed to the Partial Template when it's called and, in the Partial, instead of calling the Scratch from the context of the dollar-sign (or whatever it’s called in Hugo/Go) I used this variable I initialized:
list.html
partials/breadcrumbs.html
And from them on, instead of, for example:
I used:
And all Pagination elements worked without duplicates.