包含变量数组值

Include variable array value

我有一些组件按钮:

{% assign class = "c-button " | append: include.class %}
{% assign type = include.type | default: "button" %}
{% assign content = include.content %}

{% if content %}
  <button class="{{ class }}"
          type="{{ type }}">{{ content }}</button>
{% endif %}

现在我想包含一个按钮,其中包含一些值和数组中的内容:

{% include components/button.html
  type = "button"
  content = site.data.contentful.spaces.links.navbar[0].item_name
  class = "pretty-button"
%}

我收到此错误:

Liquid Exception: Invalid syntax for include tag: type = "button" content = site.data.contentful.spaces.links.navbar.[0] class = "pretty-button" Valid syntax: {% include file.ext param='value' param2='value' %}

是否无法将数组值分配给包含变量?

感谢您的帮助!

include 标签目前不解析语法如 navbar[0] 的变量值。仅 "simple quoted strings" 或 "variables comprising alphanumericals and/or a hyphen".

content = site.data.contentful.spaces.links.navbar[0].item_name 将被标记
content = site.data.contentful.spaces.links.navbar.item_name 将通过评估。

您可以使用 capture 标记到 pre-eval 标记的变量,然后通过简单变量插入:

{% capture my_content %} site.data.contentful.spaces.links.navbar[0].item_name {% endcapture %}
{% include components/button.html type = "button" content = my_content class = "pretty-button" %}

请注意,由于解析正则表达式中忽略多行字符串的错误,包含标记在单行中定义。补丁包含在 jekyll-3.8.0.pre.rc1