Repository: mkdocs/mkdocs

Issue:
Provide length of TableOfContents
Currently, you can only iter over `TableOfContents`. I would like to know the length of it.


Discussion:
I assume you mean that you would like to have access to the length from within a template. Have you tried Jinja's builtin [length][1] filter? See how to use filters in templates [here][4]. How did that not meet your needs?

Finally, there is no `TableOfContents` in MkDocs. However, there is a [nav][2] (site navigation) and a [page.toc][3] (table of contents for an individual page). You'll need to specify which you mean. And as a reminder, both of those objected can contain nested items. You will only ever be able to get the length of an individual branch at a time.

[1]: http://jinja.pocoo.org/docs/2.9/templates/#length
[2]: http://www.mkdocs.org/user-guide/custom-themes/#nav
[3]: http://www.mkdocs.org/user-guide/custom-themes/#pagetoc
[4]: http://jinja.pocoo.org/docs/2.9/templates/#filters
Yes. It throws saying that no `len` function is available for the `TableOfContents` class. Currently I do `toc | list | length`, which is a pretty stupid hack considering the fact that the length is available on `TableOfContents.item`.

That being said, could I do `toc.items | length` maybe? I don't really use python *or* Jinja, so I might be going at this wrong :)

This is what I'm doing:

```
  {% set toc_ = page.toc %}

  <!--
    Hack: check whether the content contains a h1 headline. If it does, the
    top-level anchor must be skipped, since it would be redundant to the link
    to the current page that is located just above the anchor. Therefore we
    directly continue with the children of the anchor.

    NOTE: Modified to only do this if there is only 1 top level TOC entry.
  -->
  {% if toc_ | first is defined and "\x3ch1 id=" in page.content and toc_ | list | length == 1 %} {% set toc_ = (toc_ | first).children
  %} {% endif %}
```
Oh, you want a `__len__` method added to the [mkdocs.toc.TableOfContents][1] class. Yeah, I agree it is odd there isn't one already. While we're at it, I wonder if we should add any other methods (perhaps contains)? 

[1]: https://github.com/mkdocs/mkdocs/blob/master/mkdocs/toc.py#L25
Sorry. I don't really know what I'm doing, so I might be expressing my needs in the wrong way 😛 .

 Anyways, getting this expression I'm doing here as a first class member might be a good idea, as it seems to be a thing multiple templates does (basically, a method/property/something that returns true if there is only a single top level entry). I don't know what the `x3ch1` check is though.

Alternatively, provide some way of getting the children of the first entry if it's a single top leve, and the whole thing if not, as a convenience.
> Alternatively, provide some way of getting the children of the first entry if it's a single top leve, and the whole thing if not, as a convenience.

Sometimes it helps to understand what you are actually trying to accomplish. Now your request is more clear.  Regardless, my proposed fix above will address your needs.

Resolving patch:
diff --git a/mkdocs/toc.py b/mkdocs/toc.py
--- a/mkdocs/toc.py
+++ b/mkdocs/toc.py
@@ -32,6 +32,9 @@ def __init__(self, html):
     def __iter__(self):
         return iter(self.items)
 
+    def __len__(self):
+        return len(self.items)
+
     def __str__(self):
         return ''.join([str(item) for item in self])
 


Test patch: