Skip to content

Instantly share code, notes, and snippets.

@willbroderick
Last active July 4, 2021 05:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbroderick/7152327 to your computer and use it in GitHub Desktop.
Save willbroderick/7152327 to your computer and use it in GitHub Desktop.
Generic recursive nav markup generator for Shopify, works with non-latin alphabet and to any depth.
{% comment %}
Usage: {% include 'recursive-navigation-helper' with 'main-menu' %}
{% endcomment %}
{% comment %} Sanity check to prevent infinite recursion {% endcomment %}
{% assign sanity = sanity | plus: 1 %}
{% if sanity < 10000 %}
{% if linklists[recursive-navigation-helper].links.size > 0 %}
<ul>
{% for link in linklists[recursive-navigation-helper].links %}
<li{% if link.active %} class="active"{% endif %}>
<a href="{{ link.url }}">{{ link.title }}</a>
{% comment %} ADVANCED MODE - uses 'sub' prefix and namespacing: sub-clothes-large {% endcomment %}
{% if settings.adv_nav %}
{% comment %} Remove 'main-menu' as we don't use that in our namespacing {% endcomment %}
{% assign current_menu_handle = recursive-navigation-helper | remove: 'main-menu' %}
{% comment %} Append new handle to extend namespace {% endcomment %}
{% assign sub_menu_list_handle_stub = current_menu_handle | append: '-' | append: link.title | handleize %}
{% comment %} If handle doesn't begin with 'sub-', prepend it {% endcomment %}
{% assign split_nav_handle_first = recursive-navigation-helper | split: '-' | first %}
{% unless split_nav_handle_first == 'sub' %}
{% assign sub_menu_list_handle_stub = 'sub-' | append: sub_menu_list_handle_stub %}
{% endunless %}
{% else %}
{% assign sub_menu_list_handle_stub = link.title | handleize %}
{% endif %}
{% comment %} Include new menu - recursive {% endcomment %}
{% include 'recursive-navigation-helper' with sub_menu_list_handle_stub %}
</li>
{% endfor %}
</ul>
{% else %}
{% comment %} Check for foreign-language links {% endcomment %}
{% assign has_drop_down = false %}
{% if recursive-navigation-helper == '' %}
{% for i in (0..8) %}
{% unless has_drop_down %}
{% if forloop.first %}
{% assign temp = 'link-list' %}
{% else %}
{% capture temp %}link-list-{{ i }}{% endcapture %}
{% endif %}
{% if linklists[temp] and linklists[temp].links.size > 0 and linklists[temp].title == link.title %}
{% assign has_drop_down = true %}
{% assign child_list_handle = temp %}
{% endif %}
{% endunless %}
{% endfor %}
{% endif %}
{% if has_drop_down %}
{% include 'recursive-navigation-helper' with child_list_handle %}
{% endif %}
{% endif %}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment