<!DOCTYPE html>
<html>

  <head>
      <title>Nunjucks macro recursive</title>
  </head>

  <body>
    <h1>Nunjucks macro recursive</h1>
    
    <script>console.clear();</script>
    <script src="https://cdn.rawgit.com/mozilla/nunjucks/2.x/browser/nunjucks.min.js"></script>
    <script src="index.js"></script>
  </body>

</html>
(function(nunjucks){
       
    nunjucks.configure('modules');
    
    nunjucks.render('home.njk.html', { menu: getMenuItems() }, function(err, html) {
        if (err) { console.log('render error', err); }
        document.body.innerHTML += html;
    });
    
    function getMenuItems() {
        return [
            {
                text: 'File',
                items: [
                    {
                        text: 'Close',
                        url: '#close'
                    },
                    {
                        text: 'Print...',
                        url: '#print'
                    }
                ]
            },
            {
                text: 'Edit',
                items: [
                    {
                        text: 'Copy',
                        url: '#copy'
                    },
                    {
                        text: 'Paste',
                        url: '#paste'
                    },
                    {
                        text: 'Find',
                        url: '#find',
                        items: [
                            {
                                text: 'Find...',
                                url: '#find'
                            },
                            {
                                text: 'Find previous',
                                url: '#find-prev'
                            },
                            {
                                text: 'Find next',
                                url: '#find-next'
                            }
                        ]
                    }
                ]
            },
            {
                text: 'Help',
                url: '#help'
            }
        ];
    }

}(window.nunjucks));
# Nunjucks macro recursive

Response to https://github.com/mozilla/nunjucks/issues/416
{% from "menu.njk.html" import menuItem %}

<nav>
    <h2>Site menu</h2>
    <ul>
        {% for item in menu %}
        {{ menuItem(item) }}
        {% endfor %}
    </ul>
</nav>
{% macro menuItem(item) %}
<li>
    {% if item.url %}
        <a href="{{ item.url }}">{{ item.text }}</a>
    {% else %}
        {{ item.text }}
    {% endif %}
    {% if item.items %}
    <ul>
        {% for item in item.items %}
        {{ menuItem(item) }}
        {% endfor %}
    </ul>
    {% endif %}
</li>
{% endmacro %}