I’m thinking about a way to have different sidebars for pages.
The idea is to allow pages to have their own sidebar, but fall back to a default one. Bonus points for walking through parent pages/folders and use their sidebar until only the top level “default” sidebar is left.
I’m guessing I’d have to use modular pages for that, but since I just started playing with Grav, I’m not sure how I’d do that.
I do something similar to what you are looking for in my Grav Course Hub Project. There I support multiple courses (blogs), where each can contain its own sidebar. If no custom sidebars are found I then use the top-level one.
ooops it’s the “walk through children” which made me think about this kind of recursive mechanism to fetch content. I didn’t think about “inheritence” as you expect…
I’m not sure if this is the best solution since I’m new to Grav and Twig, but it seems to work for now. Thanks to @paulhibbitts for giving me the idea to use uri.path|split(’/’) to be able to move through the levels.
{% set sidebarContent = '' %} {# some default "emptiness"... #}
{% set pathArray = uri.path|split('/') %} {# is there a better option than using the URI? #}
{% for pathPart in pathArray %}
{% set fullPath = fullPath ~ pathPart %}
{% set tmpContent = pages.find(fullPath ~ '/sidebar').content %}
{% if tmpContent is not empty %}
{% set sidebarContent = tmpContent %} {# overwrite old sidebar content with new #}
{% endif %}
{% set fullPath = fullPath ~ '/' %} {# add delimiter to prepare for the next pathPart #)
{% endfor %}
{{ sidebarContent }}