How to get sub-section from template; ex. content/en/docs/api/overview.md

Url: http://localhost:1313/docs/api/overview/
Content file : content/en/docs/api/overview.md

For a sidebar generator, I need to read sub-section/ api

Can read section and page slug ; docs and overview via

{{ $section := .Section }}

{{ $url := split .Permalink "/" }}
{{ $urlPageSlug := index $url (sub (len $url) 2) }}

but if use {{ $subSection := index $url (sub (len $url) 3) }} it gets localhost:1313 for http://localhost:1313/docs/page/

Can anybody help me on this?

Thanks

If any ref needed :

Extracting the value from .Permalink or .RelPermalink is a bit fragile. The URLs can be changed in front matter, site configuration, etc.

Option 1 - Extract the value from the current section’s file path

With this structure:

content/docs/
└── api/
    ├── _index.md
    └── overview.md

You can do:

{{ $key := "" }}
{{ with .CurrentSection.File }}
  {{ $key = path.BaseName .Dir }}
{{ end }}
{{ $key }} --> api

Option 2 - Extract the value from the current section’s title

content/docs/api/_index.md

+++
title = 'API'
draft = false
+++
{{ .CurrentSection.Title }} --> API

{{ .CurrentSection.Title | lower }} --> api
1 Like

Looks like I can catch docs and api via
{{ $.Section | lower }}
{{ $.CurrentSection.Title | lower }}
Thanks

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