# Section Depth

**URL:** <https://discourse.gohugo.io/t/section-depth/27451>\
**Category:** support\
**Created:** [August 9, 2020, 11:33am UTC](https://discourse.gohugo.io/t/section-depth/27451 "2020-08-09T11:33:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [August 9, 2020, 11:33am UTC](https://discourse.gohugo.io/t/section-depth/27451/1 "2020-08-09T11:33:30Z")

</div>

I have the following directory structure:

```auto
.
├── _index.md
├── policy.md
├── contact
│ ├── _index.md
│ ├── thanks.md
├── disclaimer.md
├── consulting
│ ├── _index.md
│ └── strategy.md
├── mission.md
├── services
│ ├── _index.md
│ ├── webdevelopment.md
│ ├── apps.md
│ └── design.md
└── privacy.md

```

I need to access the “section” depth. Example:

```auto
.
├── _index.md <=== 0
├── policy.md
├── contact <=== 1
│ ├── _index.md 
│ ├── thanks.md
├── disclaimer.md
├── consulting <=== 1
│ ├── _index.md  
│ └── strategy.md
├── mission.md
├── services <=== 1
│ ├── _index.md <=== 1
│ ├── webdevelopment.md <=== 2
│ ├── apps.md <=== 2
│ └── design.md <=== 2
└── privacy.md

```

What would be a easy way to achieve this? Currently I’m thinking of using [urls.Parse](https://gohugo.io/functions/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](https://gohugo.io/content-management/sections/#example-breadcrumb-navigation). So expected output for `services` would be: `1. Home / 2. Services / 3. Webdevelopment`.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [August 9, 2020, 2:23pm UTC](https://discourse.gohugo.io/t/section-depth/27451/2 "2020-08-09T14:23:04Z")

</div>

Use `add`, e.g.:

```auto
{{ $level := add .level 1 }}
{{ template "breadcrumbnav" (dict "p1" .p1.Site.Home "p2" .p2 "level" $level ) }}

```

---

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [August 9, 2020, 2:41pm UTC](https://discourse.gohugo.io/t/section-depth/27451/3 "2020-08-09T14:41:29Z")

</div>

Thanks for your reply Bep.

I’m getting the following error:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [August 12, 2020, 2:31pm UTC](https://discourse.gohugo.io/t/section-depth/27451/4 "2020-08-12T14:31:17Z")

</div>

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.

Any help would be greatly appriciated.

---

<div class="post-metadata">

**Author:** ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)\
**Post date:** [August 12, 2020, 3:02pm UTC](https://discourse.gohugo.io/t/section-depth/27451/5 "2020-08-12T15:02:23Z")

</div>

> [@dfsoeten](#):
>
> I forgot to mention. I’m using the [breadcrumb example](https://gohugo.io/content-management/sections/#example-breadcrumb-navigation). So expected output for `services` would be: `1. Home / 2. Services / 3. Webdevelopment` .

If you use a HTML `ol` list to generate your breadcrumb, your depth is automagically generated!

And `ol` is a valid choice: [Breadcrumb Example | APG | WAI | W3C](https://www.w3.org/TR/wai-aria-practices-1.1/examples/breadcrumb/index.html)

---

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [August 12, 2020, 3:13pm UTC](https://discourse.gohugo.io/t/section-depth/27451/6 "2020-08-12T15:13:20Z")

</div>

Thanks for your reply @.

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](https://developers.google.com/search/docs/data-types/breadcrumb) which have a position as a [required property](https://developers.google.com/search/docs/data-types/breadcrumb#structured-data-type-definitions).

Thanks for your suggestion though.

My code currently, looks like this:

```auto
{{ $level := 3 }}

{{ if not .IsHome }}
    <script type="application/ld+json">
        {
            "@context": "https://schema.org",
            "@type": "BreadcrumbList",
            "itemListElement": [
                {{ template "sd-breadcrumb" (dict "p1" . "p2" . "level" $level) }}
            ]
        }
    </script>

{{ end }}

{{ define "sd-breadcrumb" }}
    {{ if .p1.Parent }}
        {{ template "sd-breadcrumb" (dict "p1" .p1.Parent "p2" .p2 "level" (add .level -1)) }}
    {{ else if not .p1.IsHome }}
        {{ template "sd-breadcrumb" (dict "p1" .p1.Site.Home "p2" .p2 "level" (add .level -1)) }}
    {{ end }}
        {
        "@type": "ListItem",
        "position": {{ .level }},
        "name": {{ .p1.Title }},
        "item": {{ .p1.Permalink }}
        },
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)\
**Post date:** [August 12, 2020, 6:48pm UTC](https://discourse.gohugo.io/t/section-depth/27451/7 "2020-08-12T18:48:39Z")

</div>

> [@dfsoeten](#):
>
> That’s a cute workaround. But my issue above was just as an example.

So a quick’n dirty answer:

```
{{ define "breadcrumbs" }}
{{ $position := ( sub .position 1 ) }}
{{ with .context.Parent }},
{
"@type": "ListItem",
"position": {{ $position }},
"item": {
"@id": {{ .Permalink }},
"name": {{ .Title }}
}
}
{{ template "breadcrumbs" ( dict "context" . "position" $position ) }}
{{ end }}
{{ end }}

{{ $position := 0 }}
<script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "BreadcrumbList",
      "itemListElement": 
      [{
        "@type": "ListItem",
        "position": {{ $position }},
        "item": {
          "@id": {{ .Permalink }},
          "name": {{ .Title }}
        }
    }
        {{ template "breadcrumbs" ( dict "context" . "position" $position ) }}
      ]
    }
</script>

```

This file is `partials/ld-breadcrumb.html`, called by `{{ partial "ld-breadcrumb.html" . }}`.

---

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [August 12, 2020, 6:54pm UTC](https://discourse.gohugo.io/t/section-depth/27451/8 "2020-08-12T18:54:46Z")

</div>

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.

Thank you very much!

It’s a wild hack, but I’ll mark it as a solution.

---

<div class="post-metadata">

**Author:** ![dfsoeten](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/dfsoeten/32/12100_2.png) [@dfsoeten](https://discourse.gohugo.io/u/dfsoeten)\
**Post date:** [August 13, 2020, 11:34am UTC](https://discourse.gohugo.io/t/section-depth/27451/10 "2020-08-13T11:34:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nfriedli](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/nfriedli/32/26027_2.png) [@nfriedli](https://discourse.gohugo.io/u/nfriedli)\
**Post date:** [August 13, 2020, 1:42pm UTC](https://discourse.gohugo.io/t/section-depth/27451/11 "2020-08-13T13:42:03Z")

</div>

I have another solution. Only in my head…

1. Build a slice with absolute permalinks. Current page, mother page, grand mother page, etc.
2. Sort the slice. The shorter permalink is in position 1, etc. (NB: slice length give you the depth…)
3. Range the slice to create your ld+json with a position increment.

I’ll try to code that today.
