Skip to content

Instantly share code, notes, and snippets.

@jonginn
Last active July 13, 2016 12:20
Show Gist options
  • Select an option

  • Save jonginn/2511e53d9e546456fba4 to your computer and use it in GitHub Desktop.

Select an option

Save jonginn/2511e53d9e546456fba4 to your computer and use it in GitHub Desktop.
Easy Timber Navigation
{% macro link(item) -%}
<a href="{{ item.get_link }}" class="c-nav__link">
{{ item.title }}
</a>
{%- endmacro %}
{% macro item(item, loop) -%}
{% import _self as nav %}
<li class="c-nav__item c-nav__item--level-{{ item.level }} {{ item.classes|join(' ')}} {{ item.get_children ? 'has-children' }} {{ item.current ? 'is-active' }} c-nav__item--id-{{ loop.index0 }}">
{% if item.get_children %}
{{ nav.link(item) }}
<ul class="c-nav__dropdown-menu c-nav__dropdown-menu--level-{{ item.level }}">
{% for child in item.get_children %}
{{ nav.item(child, loop) }}
{% endfor %}
</ul>
{% else %}
{{ nav.link(item) }}
{% endif %}
</li>
{%- endmacro %}
{% macro menu(menu) -%}
{% import _self as nav %}
<ul id="global-navigation" class="c-nav">
{% for items in menu %}
{{ nav.item(items, loop) }}
{% endfor %}
</ul>
{%- endmacro %}
{% import 'nav_macros.twig' as nav %}
{{ nav.menu(primary_menu.get_items) }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment