I’m new here so welcome everybody. First I have to admit I’m not a programmer. I’m rather a kinda old-school webmaster, doing a bit of everything, but never managed to really learn PHP.
Grav is my next CMS of choice, after I realized the WordPress is going the wrong way. For a couple of months I’ve managed to explore solutions for my typical problems myself, but right now I got stuck.
I have a web site with 3 levels of navigation (I’m doing a silo type of navigation). Main menu was no problem as it just displays all of the pages. But I have a problem with context submenu on subpages (additional menu - so a user doesn’t has to use a complex main menu to find pages within one level of navigation).
What I need is simply to show 1st level page’s children on every page of that level (with active indicator). So from the whole tree of navigation I need just one branch. Problem is I don’t know how to do it with twig.
I’m building my custom theme, I have ‘borrowed’ the main menu code from default Quark theme.
I want that menu to be placed in the sidebar. So if a page has no children I will display there a text block.
By ‘active indicator’ I mean adding a class ‘active’ so I can style the current page link, depending on which child or grandchild page is currently displayed.
What I need is:
if I’m on the e.g. 02.parent page that has the children (and grandchildren) I want to show only the part of the whole tree that relates to this page (if a page has no children I’ll show a text block)
I want to have that menu on parent page, its children and grandchildren (but without the first level parent page - it’s always visible in the main menu). I will use just a nested list, so it may be the same on every page within the current branch (even better, user might browse the whole branch without the need to move back to the higher level page)
well, to show directly what I need I have to show a sample… it’s my very old project made on WordPress: Packages | Bobrowy Dwór - you’ll see a brown submenu on the left (looks like an open book). That’s exactly what I want to achieve. I may show also the real project we’re talking about but it’s on production server and unfinished.
In this case, can’t find any method to get all ancestors, so probably will need to impelement your own logic using parent
I see book shows siblings there (you’ll have to include current page yourself, because siblings are just siblings; Or get parent and then all parents children, which will include current page too)
On top lebel (page from the link) it looks like I’d like to to be. But on the children pages it shows just the children of the current page. I know it’s quite logical. I have figured out that the clue is somewhere in this: {{ macro2.contextmenu(page) }}. If I use “pages” it iterates through all the pages of the site (like in top menu), if I use “page” it iterates only through the current page. Is it possible to iterate through the whole node (so e.g. starting with the page “klimatyzacja”) even if I’m on the lowest child? I guess such a context menu is a common pattern, so there must be a way to get this.
I used to work with the Get Simple CMS which is also a flat file CMS. There is the “i18n” plugin that also creates the menues (but you have to define the structure of a menu first). With that plugin I was able to display the menu starting on any level I wanted. So if I wanted to get such a submenu I just started on second level - and I got what I wanted.
If you were to rename i to page, you’d see you are getting page.children of a children of a children… recursively. Exact same with parent - get a page.parent of a parent of a parent… Of course your list will become reversed, so you’ll need a way to reverse it again
If I get it right, if Child1 had children and you were on Child2 > Grandchild2, there shouldn’t be neither Child1 nor its children listed, but in your example there will be. Or I misunderstood both the OP question and your example?
Anyway it works as I intended to. Thank you very much again.
You see, I’m not a programmer but I used to work with many, so I know there might be misunderstandings because we think on different levels of abstraction I’m mostly working with HTML+CSS and design, but I know quite enough to build a web site myself with the help of documentation (if written well) and some snippets.
I knew about Grav for a few years, but at the first look it was too difficult for me. In the meanwhile I’ve tried Middleman static site generator and so ERB templating seems quite similar to TWIG, what helped me a lot. I did my first Grav web site just a few months ago. I was looking for an alternative to the WordPress which is becoming a page builder (I used to love it earlier, now I hate it), and I prefer to have some control over the code…