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

Is it possible that the “section.pages” as in Sections and Pages 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!

1 Like

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 %}

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!

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

1 Like

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

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

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

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

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.

The book theme has something similar: book/index.html at master · getzola/book · GitHub except you can extract book/index.html at master · getzola/book · GitHub 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 you can do something like

{% 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

1 Like