我有一些组件按钮:
{% 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:包含标记的语法无效:type =&#34; button&#34; content = site.data.contentful.spaces.links.navbar。[0] class = &#34;漂亮按钮&#34;有效语法:{%include file.ext param =&#39; value&#39; 参数2 =&#39;值&#39; %}
是否无法将数组值分配给包含变量?
Thanx任何帮助!
答案 0 :(得分:2)
include标记当前不使用navbar[0]
等语法解析变量值。只有“简单引用的字符串”或“包含字母数字和/或连字符的变量”。
content = site.data.contentful.spaces.links.navbar[0].item_name
将被标记为
,但 content = site.data.contentful.spaces.links.navbar.item_name
将被传递以进行评估。
您可以使用capture
标记预先评估已标记的变量,然后通过一个简单的变量插入:
{% 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" %}
请注意,由于解析正则表达式中忽略多行字符串的错误,include标记在一行中定义。该补丁包含在jekyll-3.8.0.pre.rc1