# Section.pages to search recursively for all sub-folders/directories

**URL:** <https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372>\
**Category:** Support\
**Created:** [April 5, 2020, 11:48am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372 "2020-04-05T11:48:25Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ikevin8me](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@ikevin8me](https://zola.discourse.group/u/ikevin8me)\
**Post date:** [April 5, 2020, 11:48am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/1 "2020-04-05T11:48:25Z")

</div>

Is it possible that the “section.pages” as in [Sections and Pages](https://www.getzola.org/documentation/templates/pages-sections/) search all sub-folders/directories recursively? Is there a setting or way to do this?

(It seems that mine only return the pages in the directory.)

Thanks!

---

<div class="post-metadata">

**Author:** ![Roman](https://avatars.discourse-cdn.com/v4/letter/r/ed8c4c/32.png) [@Roman](https://zola.discourse.group/u/Roman)\
**Post date:** [April 5, 2020, 4:15pm UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/2 "2020-04-05T16:15:00Z")

</div>

maybe I misunderstood you, but try to use the macro like :

```
{% macro tree() %}
        {% for subsection in section.subsections | sort %}
            {% set subsection_item = get_section(path=subsection) %}  
               {{ subsection_item.title }}
            {% endfor %}
{% endmacro tree %}
```

---

<div class="post-metadata">

**Author:** ![ikevin8me](https://avatars.discourse-cdn.com/v4/letter/i/8c91f0/32.png) [@ikevin8me](https://zola.discourse.group/u/ikevin8me)\
**Post date:** [April 6, 2020, 5:48am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/3 "2020-04-06T05:48:29Z")

</div>

I think your code is right (using the “subsections”), but I’m not sure how to use “macro tree()”. I’m getting this error:

> Failed to build the site  
> Error: Error parsing templates  
> Reason:
> 
> - Failed to parse “/path/zola/templates/articles/index.html”  
> → 65:17  
> |  
> 65 | {% macro tree() %}␊  
> | ^—  
> |  
> = unexpected tag; expected an `else` tag, an endfor tag (`{% endfor %}`), or some content

Can you provide some help how to use macro tree()?

Thanks!

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [April 6, 2020, 9:32am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/4 "2020-04-06T09:32:03Z")

</div>

That depends on your usecase. You can make all subdirectories `transparent` if that’s what your site wants to do.

Macros have to be defined in another file and imported: [https://tera.netlify.com/docs/#macros](https://tera.netlify.com/docs/#macros)

---

<div class="post-metadata">

**Author:** ![krisradio](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/krisradio/32/178_2.png) [@krisradio](https://zola.discourse.group/u/krisradio)\
**Post date:** [April 10, 2020, 2:06am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/5 "2020-04-10T02:06:36Z")

</div>

What are the conditions for the the subsection field to be included in the context?

The Sections and Pages docs say:

> Whichever template you decide to render, you will get a `section` variable in your template with the following fields:

> ```
> // Direct subsections to this section, sorted by subsections weight
> // This only contains the path to use in the `get_section` Tera function to get
> // the actual section object if you need it
> subsections: Array<String>;
> 
> ```

But I’m not getting a `subsections` _or_ `pages` variable for nexted `index.md` files (i.e., one directory below content). And for `_index.md` files, they redirect to root\* instead of making a page, so I can’t check the output of `{{ __tera_context }}`.

\*_when_ `redirect_to=""` _is set in the front matter_

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [April 10, 2020, 9:25am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/6 "2020-04-10T09:25:10Z")

</div>

It’s only the direct subsections. Subsections of subsections are not going to be included.

---

<div class="post-metadata">

**Author:** ![DoNotResuscitate](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/donotresuscitate/32/872_2.png) [@DoNotResuscitate](https://zola.discourse.group/u/DoNotResuscitate)\
**Post date:** [December 1, 2022, 1:17pm UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/7 "2022-12-01T13:17:17Z")

</div>

Is there any way to do a recursive subsections of subsections? If I want to display all nested folders in a sidebar, is there any way other than explicitly defining each sublevel? E.g [obsidian-zola/docs-sidebar.html at main · DoNotResuscitate/obsidian-zola · GitHub](https://github.com/DoNotResuscitate/obsidian-zola/blob/main/zola/templates/macros/docs-sidebar.html)

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [December 1, 2022, 7:19pm UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/8 "2022-12-01T19:19:00Z")

</div>

You should be able to do it with a recursive macro?

---

<div class="post-metadata">

**Author:** ![DoNotResuscitate](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/donotresuscitate/32/872_2.png) [@DoNotResuscitate](https://zola.discourse.group/u/DoNotResuscitate)\
**Post date:** [December 1, 2022, 10:55pm UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/9 "2022-12-01T22:55:36Z")

</div>

Do you have an example I can reference? I’m not sure how to go about doing this. I haven’t been able to find a template either with somebody using a non hard limit defined sidebar.

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [December 2, 2022, 9:03am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/10 "2022-12-02T09:03:40Z")

</div>

The book theme has something similar: [book/index.html at master · getzola/book · GitHub](https://github.com/getzola/book/blob/master/templates/index.html#L30-L54) except you can extract [book/index.html at master · getzola/book · GitHub](https://github.com/getzola/book/blob/master/templates/index.html#L33-L51) into a separate macro that takes `current_path`, `level` and `section` as parameters. Level represents the level of nesting so you can change the styling. Then if you don’t need to the pages like [book/index.html at master · getzola/book · GitHub](https://github.com/getzola/book/blob/master/templates/index.html#L40-L51) you can do something like

```auto
{% for subsection in section.subsections %}
  {{ self::render_section_in_sidebar(current_path=current_path, level=level+1, section=subsection }}
{% endfor %}

```

Note the `self::` part when calling itself recursively

---

<div class="post-metadata">

**Author:** ![RenaudDenis](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/renauddenis/32/1289_2.png) [@RenaudDenis](https://zola.discourse.group/u/RenaudDenis)\
**Post date:** [August 20, 2024, 6:39am UTC](https://zola.discourse.group/t/section-pages-to-search-recursively-for-all-sub-folders-directories/372/11 "2024-08-20T06:39:08Z")

</div>

> You can make all subdirectories `transparent` if that’s what your site wants to do.

Omg. I just lost like 2h on this.  
Thanks!
