YAML数组的字符串过滤器?

时间:2017-05-20 16:12:09

标签: arrays yaml jekyll

在Jekyll中是否有一种方法可以在此YAML哈希中为{{ data[1] }}添加字符串过滤器,以便在" amet"之后使用逗号。不打破阵列?

- project:
  -
    name: Name
    main:
      - image: image.png, Lorem ipsum dolor sit amet, consectetur adipiscing elit., category, Main Category, placeholdertext

编辑:@marcanuy询问如何定义data[1]。它是for循环中的索引位置,我将其用于alt标记:alt="{{ data[1] | strip }}",所以:

{% for main in project.main %}

    {% assign main = site.data.catalog[0].project[0].main[0] %}
    {% assign data = main.image|split:"," %}

2 个答案:

答案 0 :(得分:0)

有人回答:创建一个新数组,删除第一个项目(图像名称),然后用逗号连接其余元素以恢复原始文本:

Image name: {{ data |first | strip }}
{% assign text = data | shift %}
Image text: {{ text | join: "," }}

输出:

Image name: image.png

Image text: Lorem ipsum dolor sit amet, consectetur adipiscing elit., category, Main Category, placeholdertext

答案 1 :(得分:0)

也许您可以简化目录文件结构。

_data / catalog.yml

包含数组中的所有项目:

-
  name: Name1
  main:
    image:
      url: image1.png
      alt: 1Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      category: 1Main Category
      placeholderText: 1placeholdertext
-
  name: Name2
  main:
    image:
      url: image2.png
      alt: 2Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      category: 2Main Category
      placeholderText: 2placeholdertext

现在,您可以获得以下内容:

{% for project in site.data.catalog  %}
  {% assign mainImg = project.main.image %}
    <img src="{{ site.base }}{{ maybe.img.path.from.config }}{{ mainImg.url }}" alt="{{ mainImg.alt }}">
{% endfor %}