3
votes

I have a jekyll project with two pages, each backed by YAML maps that both reference each other. For example:

a: &a
  name: "Ay"
  parents: []
  children: [*b]

b: &b 
  name: "Bee"
  parents: [*a]
  children: []

Vanilla YAML seems not to support using an alias/anchor before its been defined, which invalidates this strategy. Is there any way, perhaps using liquid-fu that would let me generate pages that enumerate an entry's parents and children?

2

2 Answers

3
votes

You just need to give the value on first occurrence:

a: &a
  name: "Ay"
  parents: []
  children:
    - &b
      name: "Bee"
      parents: [*a]
      children: []
b: *b

The alias/anchor construct has been designed specifically for this use-case. Since the parsed YAML data does not distinguish the place where the object is anchored and the place where it is referenced, this is equivalent to what you want to have.

0
votes

As the question above is worded, @flyx’s is the most appropriate answer, however given external constraints (see my other question) I ended writing my own plugin to let data files textually include one another through liquid.

The goals of this plugin are to let the data be:

  1. DRY - (don’t repeat yourself) each model should be defined only once.
  2. Grouped - all similar data should be defined in the same format next to each other.
  3. Separated - different data should be defined in different places.

@flyx’s solutions here fail goals #2 and #3, requiring all different types of data to be defined in the same place and intermixing the definitions of foods and ingredients.

My proposed solution allows textual inclusion of one data file into another. This allows different models to be defined in different files, yet referenced from other files as if they were defined in the same place, in an arbitrary order. Applied to this problem, my solution would like this:

A.yml

{% include_relative_once _data/B.yml %}

a: &a
  name: "Ay"
  parents: []
  children: [*b]

B.yml

{% include_relative_once _data/A.yml %}

b: &b 
  name: "Bee"
  parents: [*a]
  children: []

For the plugin itself, see this gist