Dear inhabitants of Kirby land!
I got a question: If you have a menu, and wanna apply classes to the <li> element (pretty common). So, I want to add one class if it has a submenu, and another class if the corresponding page is opened.
Do you have functions / best practices to make that happen with as little / dry code as possible? I mean I could do an endless series of if / elseif / elseif / else, … but it seems tedious
So this item should either have the class is-open or has-submenu (classnames just examples)? Never both, if it has a submenu and is open? You don’t need if-statements if you use the e()/ecco() or r() helpers (or the ternary operator). For example:
Ok, then the second example above should work for you. It adds the is-active class if the page is currently open und the has-submenu class if the page has visible children. If both conditions are true, both classes are added, if none of the conditions is true, no class is added.
If you need more complex conditions than the above, it might in fact make sense to export the logic to a function/custom page method.