bunnycode
bunnycode

Reputation: 275

Nesting an array in Jekyll with YAML

If I have this block in YAML:

- project:
  -
    name: Whiskers
    top_category: Cats
    thumb_url: thumb.jpg
    main:
      - image: cat.jpg

Is it possible to chain one of the lines, as in:

- project:
  -
    name: Whiskers
    top_category: Cats
    thumb_url: thumb.jpg
    main:
      - image_data: cat.jpg, Somealttext!

And then access it as:

<img src="cat.jpg" alt="Somealttext!" />

?

If so, what would the Liquid layout look like?

{% for main in project.main %}
    {{ ??? }}
{% endfor %}

Upvotes: 1

Views: 462

Answers (1)

marcanuy
marcanuy

Reputation: 23942

Let's consider having a data file _data/project.yml:

- project:
  -
    name: Whiskers
    top_category: Cats
    thumb_url: thumb.jpg
    main:
      - image_data: cat.jpg, Somealttext!

Then we access main:

{% assign main = site.data.project[0].project[0].main[0] %}

And split the content of image_data assuming the image_alt won't contain any commas:

{% assign data = main.image_data|split:"," %}

Then we can access its values as:

{{ data[0] }}
<br>
{{ data[1] }}

<img src="{{data[0]}}" alt="{{data[1]}}" />

Upvotes: 1

Related Questions