【问题标题】:liquid: dynamically hide placeholder links液体:动态隐藏占位符链接
【发布时间】:2023-03-10 09:50:01
【问题描述】:

我的 _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 jekyll liquid


    【解决方案1】:

    首先,您需要将 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-05
      • 2016-09-14
      • 2018-02-16
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      相关资源
      最近更新 更多