How do I insert logic for the Table of Contents?

I want the Table of Contents to only appear on pages when there are headings on the page. That is, some of my pages do not have any headings so an empty Table of Contents appears. Can someone point me to the logic that will hide the Table of Contents if there are no headings on the page?

Only thing I can think of is to use a frontmatter variable to disable the ToC for a page. You can’t inspect the page content during template rendering to know on the fly if there are headings.

Not exactly what you want, but you may get some inspiration from this:

Piggybacking on @moorereason 's comment, this is a partial I’m using on a current project that I’m working on:

{{if ne .Params.toc false}}
<aside id="toc">
	<header class="toc-header">
		<a href="#{{.Title | urlize}}">
		<h3 class="{{.Section}}">{{.Title}}</h3>
		</a>
	</header>
	{{.TableOfContents}}
</aside>
{{end}}

Basically, this assumes you want to include the Table of Contents unless the front matter of the page exclusively says otherwise (i.e, the front matter of an .md would need to include toc: false).

HTH

1 Like

Many thanks to moorereason and rdwatters!

I combined the input from both of you and now can control whether the toc appears on a specific page in the front matter.

1 Like

In the mainroad theme it is handled like this:

     <div id="toc" class="well col-md-4 col-sm-6" style="float: right;padding: 0.01em 10px;border-radius: 10px 0px 0px 10px;-moz-border-radius: 10px 0px 0px 10px;-webkit-border-radius: 10px 0px 0px 10px;border: 2px dotted #0040ff;">
     {{ .TableOfContents }}
    </div>
{{ end }}```

And seems to work well, if there are no headers, it will not insert the toc div.

Hi @rdwatters,

Thank you for this example!
I am trying it out, but from some reason, it generates an extra list item. Any idea why?

Check it out here:
Nejlepší Samsung Smart TV aplikace

Here is the example of content:

---
title: 10 Nejlepších aplikací na Smart TV 2017
description: Které z množství aplikací se vyplatí mít nainstalované na své Smart TV? Vybrali jsme 10 nejlepších, které by neměly chybět v žádném obýváku.
date: 2018-05-16
tags:
  - Smart TV
toc: true
layout: blog-detail
hero:
  title: 10 Nejlepších aplikací na Smart TV 2017
  image:
---

Díky posledním modelům Smart TV již uživatelé nemusí používat externí media boxy pro "streamovaní" jejich oblíbených filmů, seriálů a hudby na televize. Samsung Smart Tv je jeden z produktů, které nabízejí skvělý obsah bez potřeby set-top boxu.

<!--more-->

Produkty od Samsungu se systémem Tizen jsou rychlé a přispůsobivé, ale pro správný zážitek z skvělých vlastností televize si uživatelé musejí nainstalovat různé aplikace. Samsung Smart TV aplikace jsou snadno dostupné na Samsung store, kde najdete rozmanitý výběr, který vám poskytne přesně to, co potřebujete. Také jsou neustále přidávány nové aplikace. Zde jsme připravili výběr 10 aplikací, které vám pomůžou si užít vaší Smart TV na maximum.

## Nejlepší Samsung Smart TV aplikace

### 1. Netflix

Netflix je celosvětově známa služba poskytující "streamování" online videí. Zůstává v popředí tohoto odvětví od doby, kdy skoro před 10 lety celý koncept představil. Netflix má působivý výběr všemožných již existujích seriálů a filmů. Společnost také hodně investuje do výroby vlastní exkluzivní tvorby zahrnující například politické drama "House of Cards", nebo komediální sitcom "Unbreakable Kimmy Schmidt". Kdybychom chtěli mluvit o množství obsahu, tak Netflix je v současné době největší dostupná "streamovací" služba. Dostupný obsah je bohužel závislý také na vaší oblasti. Obsah dostupný v České republice nemusí být stejný jako obsah dostupný v USA. Netflix je také výborný začátek pokud jste nováček v oblasti služeb pro "streamování" videa.

![Netflix Samsung aplikace](images/netflix-samsung-aplikace.jpg)

And here is the TOC code:

{{if .Params.toc }}
    <aside>
        {{ .TableOfContents }}
    </aside>
{{end}}