Skip to content

Instantly share code, notes, and snippets.

@semireg
Last active December 28, 2015 11:29
Show Gist options
  • Save semireg/7493380 to your computer and use it in GitHub Desktop.
Save semireg/7493380 to your computer and use it in GitHub Desktop.
Using jekyll for a website with two-levels of navigation. This is one step closer to dynamic front-matter.

_data/nav.yml

LabelScope:
  alt: Tour
  subdirs:
    - Download
    - Support
    - Printers

MASS:
  alt: Tour
  subdirs:
    - Download
    - Support
    - Scales

Blog:
  name:

Contact Us:
  name:

Top-level nav:

  {% for link in site.data.nav %}
    {% assign link_name = link[0] %}
    {% assign link_url = link_name | downcase | replace: ' ','-' %}
    {% if page.url contains link_url %}
      <dd class="active">
    {% else %}
      <dd>
    {% endif %}
    <a href="/{{link_url}}">{{link_name}}</a></dd>
  {% endfor %}

Second-level nav:

<h1>{{page_title}}</h1>

{% if tree %}
  <dl class="sub-nav">

  {% assign tree_name_url = page_title | downcase | replace: ' ','-' | prepend: '/' | append: '/' %}
  {% if page.url == tree_name_url %}
    <dd class="active">
  {% else %}
    <dd>
  {% endif %}
  <a href="{{tree_name_url}}">{{tree.alt}}</a></dd>

  {% for link in tree.subdirs %}
    {% assign link_name = link %}
    {% assign link_url = link_name | downcase | replace: ' ','-' %}
    {% if page.url contains link_url %}
      <dd class="active">
    {% else %}
      <dd>
    {% endif %}
    <a href="{{tree_name_url}}{{link_url}}">{{link_name}}</a></dd>
  {% endfor %}

  </dl>
{% endif %}

Use this to setup tree, page_title and page_section.

{% for link in site.data.nav %}
  {% assign temp_link_name = link[0] %}
  {% assign temp_link_name_url = temp_link_name | downcase | replace: ' ','-' %}
  {% assign temp_url_parts = page.url | split:'/' %}
  {% if temp_link_name_url == temp_url_parts[1] %}
    {% assign page_title = temp_link_name %}
    {% assign tree = link[1] %}
  {% endif %}
{% endfor %}

{% for link in tree.subdirs %}
  {% assign temp_link_name = link %}
  {% assign temp_link_url = temp_link_name | downcase | replace: ' ','-' %}
  {% if page.url contains temp_link_url %}
    {% assign page_section = temp_link_name %}
  {% endif %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment