# How to know the section depth

**URL:** <https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302>\
**Category:** support\
**Created:** [February 21, 2022, 4:57pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302 "2022-02-21T16:57:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![checking12](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@checking12](https://discourse.gohugo.io/u/checking12)\
**Post date:** [February 21, 2022, 4:57pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/1 "2022-02-21T16:57:32Z")

</div>

I am doing a TOC for my content folder. Where I have different view for every level/depth of sections.  
My question is how can I know the depth of a particular section/folder.

For example

```auto
content
└── docs ////// level1
    ├── doc1-folder  
          └── d1_a.md //// level 2
          
 
    └── doc2-folder /// level 2
          └── d2_a.md /// 
          └── doc3-folder
                   └── d3_a.md /// level 3

```

How can I know these levels ? I am looping the section using `range` , Do we have any parameter using which I can know the level/ depth of the section and perform action accordingly.

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [February 21, 2022, 5:54pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/2 "2022-02-21T17:54:05Z")

</div>

It’s not a complete answer, but would `.File.Path` used with [`split`](https://gohugo.io/functions/split) and [`len`](https://gohugo.io/functions/len) help?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 21, 2022, 6:06pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/3 "2022-02-21T18:06:38Z")

</div>

> [@kaushalmodi](#):
>
> `.File.Path` used with [`split`](https://gohugo.io/functions/split) and [`len`](https://gohugo.io/functions/len)

Good thinking, but that will fail with leaf bundles. So then you look at `.RelPermalink` instead, but that’s subject to user modification through front matter (`url`) and site configuration (`permalinks`).

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 21, 2022, 8:05pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/4 "2022-02-21T20:05:43Z")

</div>

To render the section depth as follows:

```nohighlight
content/
├── s1/
│ ├── s1-1/
│ │ ├── s1-1-1/
│ │ │ ├── foo.md --> section depth = 3
│ │ │ └── _index.md --> section depth = 3
│ │ ├── foo.md --> section depth = 2
│ │ └── _index.md --> section depth = 2
│ ├── foo.md --> section depth = 1
│ └── _index.md --> section depth = 1
├── foo.md --> section depth = 0
└── _index.md --> section depth = 0

```

Use this partial:

> **layouts/partials/render-section-depth.html**
>
> ```auto
> {{ $page := . }}
> {{ $depth := 0 }}
> 
> {{ if reflect.IsMap . }}
> {{ $page = .page }}
> {{ $depth = .depth }}
> {{ end }}
> 
> {{ with $page.Parent }}
> {{ if $page.IsSection }}
> {{ $depth = add $depth 1 }}
> {{ end }}
> {{ partial "render-section-depth" (dict "page" . "depth" $depth) }}
> {{- else }}
> {{ $depth }}
> {{- end }}
> {{- /**/ -}}
> 
> ```

---

<div class="post-metadata">

**Author:** ![checking12](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@checking12](https://discourse.gohugo.io/u/checking12)\
**Post date:** [February 21, 2022, 10:23pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/5 "2022-02-21T22:23:56Z")

</div>

@jmooring Seems to be promising, but for some reason it is counting for every section as previous count +1

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 21, 2022, 10:32pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/6 "2022-02-21T22:32:38Z")

</div>

Try this and see if it behaves as you expect.

```bash
git clone --single-branch -b hugo-forum-topic-37302 https://github.com/jmooring/hugo-testing hugo-forum-topic-37302
cd hugo-forum-topic-37302
hugo server

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [February 21, 2022, 10:33pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/7 "2022-02-21T22:33:47Z")

</div>

Also, are you passing the page context to the partial? Example:

```auto
{{ partial "render-section-depth.html" . }}

```

---

<div class="post-metadata">

**Author:** ![checking12](https://avatars.discourse-cdn.com/v4/letter/c/48db29/32.png) [@checking12](https://discourse.gohugo.io/u/checking12)\
**Post date:** [February 21, 2022, 11:01pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/8 "2022-02-21T23:01:52Z")

</div>

@jmooring Its working. Thanks!! Sorry, there was some mistake with my `{{ template }}`

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [February 23, 2022, 11:01pm UTC](https://discourse.gohugo.io/t/how-to-know-the-section-depth/37302/9 "2022-02-23T23:01:55Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
