Skip to content

Instantly share code, notes, and snippets.

@lukeholder
Last active June 20, 2018 16:53
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lukeholder/5730111 to your computer and use it in GitHub Desktop.
Save lukeholder/5730111 to your computer and use it in GitHub Desktop.
nested navigation with in template variables.
{% set nav = [
{"href": "http://1.com", "name": "link1","sublinks":null},
{"href": "http://1.com", "name": "link2","sublinks":null},
{"href": "http://1.com", "name": "link3","sublinks":
[{"href": "http://1.com", "name": "link 3.11","sublinks":null},
{"href": "http://1.com", "name": "link 3.22","sublinks":
[{"href": "http://1.com", "name": "link 3.11","sublinks":null},
{"href": "http://1.com", "name": "link 3.22","sublinks":null}
]
}]
},
{"href": "http://1.com", "name": "link4","sublinks":null},
{"href": "http://1.com", "name": "link5","sublinks":null},
] %}
{% macro menu_links(items, outside, inside) %}
{% spaceless %}
{% for item in items %}
<{{inside}}>
<a href="{{ item.href }}">{{ item.name }}</a>
{% if item.sublinks %}
<{{outside}}>
{{ _self.menu_links(item.sublinks, outside, inside) }}
</{{outside}}>
{% endif %}
</{{inside}}>
{% endfor %}
{% endspaceless %}
{% endmacro %}
{% import _self as navi %}
<ul class="main-menu">
{{ navi.menu_links(nav,'ul','li') }}
</ul>
@natebeaty
Copy link

I had to add another {% import _self as navi %} inside the macro, and then change _self.menu_links to navi.menu_links for this to work in Craft 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment