How to iterate over leafs (index.md) and branches (_index.md)?

Dear community

Is it somehow possible to iterate over leafs and branches and not only leafs?

I’ve the following structure:

.
├── branch-one
│   ├── _index.md
│   ├── subbranch-one
│   │   ├── _index.md
│   │   ├── subsubleaf-one
│   │   │   └── index.md
│   │   └── subsubleaf-two
│   │       └── index.md
│   ├── subleaf-one
│   │   └── index.md
│   └── subleaf-two
│       └── index.md
├── branch-three
└── branch-two

My list.html template looks like this:

{{ range .Pages }}
{{ .Content }}
{{ end }}

If I visit http://localhost:1313/branch-one/ I get the output of the two subleafs (subleaf-one, subleaf-two), but not from the subbranch.

We have some valid use cases where we also want to list the branches. For example we have the following tree:

  • offers
    • consulting
    • trainings
      • angular
      • social engineering

Now we want when someone list all offers, there is a general information about trainings. What are the options in that case?

If I add an index.md along to the _index.md the index.md is ignored.

Here is a demo page: https://github.com/openscript/list-of-index-pages

Thank you in advance. :slight_smile:

1 Like

Seems like I can iterate somehow over the branches with the data from .Sections. Now I only need to combine .Sections and .Pages and order them alphabetically somehow. :slight_smile:

Why I don’t want to use .Sections for everything instead of Branch and Leaf bundles?

Just in case anyone is still looking for the answer:

You’ll probably want to use the append function, like so:

{{ $collection1 := .Pages }}
{{ $collection2 := .Sections }}
{{ $combined := $collection1 | append $collection2 }}
{{ range $combined }}
    {{.Content}}
{{ end }}
1 Like