What would be a easy way to achieve this? Currently I’m thinking of using urls.Parse and counting the slashes (/), but that seems unnecessarily complex. Any help would be greatly appriciated.
Edit: I forgot to mention. I’m using the breadcrumb example. So expected output for services would be: 1. Home / 2. Services / 3. Webdevelopment.
Failed to render pages: render of "page" failed: execute of template failed: template: _default/single.html:9:7: executing "_default/single.html" at <partial "structuredData/breadcrumb.html" .>: error calling partial: ".../layouts/partials/structuredData/breadcrumb.html:1:17": execute of template failed: template: partials/structuredData/breadcrumb.html:1:17: executing "partials/structuredData/breadcrumb.html" at <.level>: can't evaluate field level in type *hugolib.pageState
I’ve managed to get it to work by using {{ $level := 3 }} and {{ template "sd-breadcrumb" (dict "p1" .p1.Parent "p2" .p2 "level" (add .level 1)) }}. However, this is the exact inverse of what I would like to have happen.
The result of the above is 3. Home / 2. Services / 1. Webdevelopment.. To fix this I can do {{ template "sd-breadcrumb" (dict "p1" .p1.Parent "p2" .p2 "level" (add .level -1)) }} and give $level a default value of e.g. 3. But this will break when I have a breadcrumb with a depth of not 3.
So I still need to obtain the current page depth somehow.
That’s a cute workaround. But my issue above was just as an example. My “real-world” application for this is to implement Breadcrumbs as Structured Data which have a position as a required property.
Wow. That seems to work @nfriedli! The only thing I changed was {{ $position := ( sub .position 1 ) }} to {{ $position := ( add .position 1 ) }}. Furthermore, I assumed the order mattered (ie: position 1 at index 0 in the array) but the rich snippet testing tool isn’t complaining about it.
Hmm. Yeah. If An array of breadcrumbs listed in a specific order. is a requirement your solution is indeed invalid. Weird that somehow the testing tool sees it as valid structured data.
Edit:@bep Would you be open for a pull request implementing a .Page.Depth attribute?