液体:动态隐藏占位符链接
liquid: dynamically hide placeholder links
我的 _data
文件夹中有一个 YAML 文件,其中包含主导航中的链接列表。
_data/nav.yml
:
main:
- title: Link A
- url: "/path/to/linkA"
- title: Link B
- url: "/path/to/linkB"
- title: Link C
- url: "#"
然后我使用 liquid 动态生成链接。这是我的头文件的相关部分。
_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>
导航输出为:
Link A :: Link B :: Link C
我希望输出如下所示
Link A :: Link B
因为 Link C
是占位符。
如何动态隐藏占位符链接,即以“#”作为 href 的链接?
首先,您需要将 yaml 文件中的导航项正确分组为:
main:
-
title: Link A
url: "/path/to/linkA"
-
title: Link B
url: "/path/to/linkB"
-
title: Link C
url: "#"
然后您可以使用 unless
标签避免 link c
:
<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>
我的 _data
文件夹中有一个 YAML 文件,其中包含主导航中的链接列表。
_data/nav.yml
:
main:
- title: Link A
- url: "/path/to/linkA"
- title: Link B
- url: "/path/to/linkB"
- title: Link C
- url: "#"
然后我使用 liquid 动态生成链接。这是我的头文件的相关部分。
_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>
导航输出为:
Link A :: Link B :: Link C
我希望输出如下所示
Link A :: Link B
因为 Link C
是占位符。
如何动态隐藏占位符链接,即以“#”作为 href 的链接?
首先,您需要将 yaml 文件中的导航项正确分组为:
main:
-
title: Link A
url: "/path/to/linkA"
-
title: Link B
url: "/path/to/linkB"
-
title: Link C
url: "#"
然后您可以使用 unless
标签避免 link c
:
<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>