Insert TOC at top of page

Hi there
Is it possible to insert a toc of a page at the top of that page (wiki like)?

So far I tried several things:


 {{ .TableOfContents }}
 {{ .Page.TableOfContents }}
 {{ .Summary }}
 {{ .TableOfContents }}
 {{ .EverythingElse }}

and in my content md-file:

{{< toc >}}

# Title
## bla1
## bla2

but the output show no toc…

Have you put the TOC directly in your template? That may be what you need to do, and you can use the example partial in the TOC doc to test it.

If that works, but the shortcode doesn’t, I imagine the shortcode doesn’t have access to the TOC data at render time.

I’m pretty new in hugo :wink:

For testing reasons I put some stuff into layouts\partials\header.html

{{ .Title }}

works fine, but

{{ .TableOfContents }}

is resulting in an emtpy string…

What do I have to do?

ok. Finally I got it. Using

{{ .Page.TableOfContents }}

inside page default html works fine…

But what’s the best way to toggle the toc option inside a md? sometimes I don’t want to see a toc at the top (that’s why tried partials)

You can achieve that with front matter (toc = true ) and then check for it in a shortcode (if toc = true … else …).