diff --git a/pages/10.cookbook/01.general-recipes/docs.md b/pages/10.cookbook/01.general-recipes/docs.md index cdcf0c347..238bc3b71 100644 --- a/pages/10.cookbook/01.general-recipes/docs.md +++ b/pages/10.cookbook/01.general-recipes/docs.md @@ -15,6 +15,7 @@ This page contains an assortment of problems and their respective solutions rela 1. [Create a private area](#create-a-private-area) 1. [Add JavaScript to the footer](#add-javascript-to-the-footer) 1. [Override the default logs folder location](#override-the-default-logs-folder-location) +1. [Split vertical menu system](#split-vertical-menu-system) ### Change the PHP CLI version @@ -472,3 +473,96 @@ return [ ``` This basically overrides the `log` stream with the `grav-logs/` folder rather than the default `logs/` folder as defined in `system/src/Grav/Common/Config/Setup.php`. + +### Split vertical menu system + +To create a vertical, collapsible, hierarchical menu of pages you need a Twig-loop, a bit of CSS, and a bit of JavaScript. The final result will, when using the Antimatter-theme, look like this: + +![Vertical Menu](vertical_menu.png) + +Let's start with Twig: + +``` +
    + {% for page in pages.children.visible %} + {% if page.children.visible is empty %} +
  1. + {{ page.title }} + {% else %} +
  2. + {{ page.title }} +
      + {% for child in page.children.visible %} + {% if child.children.visible is empty %} +
    1. + {{ child.title }} + {% else %} +
    2. + {{ child.title }} +
        + {% for subchild in child.children.visible %} +
      1. {{ subchild.title }}
      2. + {% endfor %} +
      + {% endif %} +
    3. + {% endfor %} +
    + {% endif %} +
  3. + {% endfor %} +
+``` + +This creates an ordered list which iterates over all visible pages within Grav, going three levels deep to create a structure for each level. The list wrapped around the entire structure has the class *tree*, and each list-item has the class *parent* if it contains children or *item* if it does not. + +Clicking on a parent opens the list, whilst regular items link to the page itself. You could add this to virtually any Twig-template in a Grav theme, provided that Grav can access the visible pages. + +To add some style, we add some CSS: + +``` + +``` + +This should generally be placed before the Twig-structure, or ideally be streamed into the [Asset Manager](/themes/asset-manager) in your theme. The effect is to add **[+]** after each parent-item, indicating that it can be opened, which disappears when opened. + +Finally, let's add a bit of JavaScript to [handle toggling](http://stackoverflow.com/a/36297446/603387) the *open*-class: + +``` + +``` + +This should always be placed **after** the Twig-structure, also ideally in the [Asset Manager](/themes/asset-manager). diff --git a/pages/10.cookbook/01.general-recipes/vertical_menu.png b/pages/10.cookbook/01.general-recipes/vertical_menu.png new file mode 100644 index 000000000..ac7b28e22 Binary files /dev/null and b/pages/10.cookbook/01.general-recipes/vertical_menu.png differ