Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

Count the entries of a menu

  • #10184
    Avatar photo[anonymous]

    Hello

    I would like to find out how many entries a navigation has (only the main menu – no sub-entries). Something like:

    {{menus.mainMenu.items.count}}

    Is this possible?

    Thank you very much!

    #10185
    Avatar photo[anonymous]

    I don’t know if it’s the best solution, but that’s how I did it:

    helpers.js

    let themeHelpers = {
        // Counts all elements of an array
        countArray: function(arrayContent) {
            if (arrayContent.constructor === Array) {
                return arrayContent.length;
            } else {
                return 0;
            }
        }
    };
    module.exports = themeHelpers;

    in the template:

    {{#checkIf (countArray menus.mainMenu.items) '==' 4}}
    show something...
    {{/checkIf}}

    Works for me…