1

I have a YAML file in my _data folder that contains a list of links I have in my main nav.

_data/nav.yml:

main:

  - title: Link A
  - url: "/path/to/linkA"

  - title: Link B
  - url: "/path/to/linkB"

  - title: Link C
  - url: "#"

I then use liquid to dynamically generate the links. Here's the relevant portion of my header file.

_includes/header.html:

<nav class="quick-links">
  {% for item in site.data.nav.main %}
    <a href="{{ item.url }}">{{ item.title }}</a>
    {% if forloop.last == false %} :: {% endif %}
  {% endfor %}
</nav>

The nav outputs as:

Link A :: Link B :: Link C

I'd like the output to look like the following

Link A :: Link B

since Link C is a placeholder.

How can I dynamically hide placeholder links, i.e. links with "#" as the href?

marcanuy
  • 23,118
  • 9
  • 64
  • 113
duplic8
  • 109
  • 1
  • 9

1 Answers1

1

First you need to group correctly nav items in yaml file as:

main:
  -
    title: Link A
    url: "/path/to/linkA"
  -
    title: Link B
    url: "/path/to/linkB"
  -
    title: Link C
    url: "#"

Then you can avoid the link c using the unless tag:

<nav class="quick-links">
  {% for item in site.data.nav.main %}
  {% unless item.url contains "#" %}
    <a href="{{ item.url }}">
    {{ item.title }}
    </a>
    {% if forloop.last == false %} :: {% endif %}
  {% endunless%}
  {% endfor %}
</nav>
marcanuy
  • 23,118
  • 9
  • 64
  • 113