【问题标题】:Include variable array value包含变量数组值
【发布时间】:2018-04-05 17:46:07
【问题描述】:

我有一些组件按钮:

{% 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"
%}

我收到此错误:

液体异常:包含标签的语法无效:type = "button" 内容 = site.data.contentful.spaces.links.navbar.[0] class= "pretty-button" 有效语法:{% include file.ext param='value' param2='值' %}

不能将数组值赋给包含变量吗?

感谢您的帮助!

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    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" %}
    

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

    【讨论】:

    • {% capture button_content %} {{site.data.contentful.spaces.links.navbar[0].item_name}}{% endcapture %} 是解决方案。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2021-11-29
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多