user12341234 user12341234 - 4 years ago 234
YAML Question

Jekyll Include one YAML file in another YAML file

I have a Jekyll project where two separate pages (A.html and B.html) are displaying different content based on data in YAML files A.yml and B.yml respectively. Each yml file has a bunch of variables that are defined identically. I'd prefer to keep this common list of variables in a third file C.yml and include it into both A.yml and B.yml. How can I do this?

Things I've tried:


  • Using * to reference global data like
    *site.data.C.vars
    - this didn't parse.

  • Put C.yml in _includes directory and use front matter treat the page like a liquid template and call
    {% include C.yml %}
    - this compiled, but the emitted html page had no content whatsoever.



Edit - example usage



Using a common data file in multiple views isn't quite sufficient for me because there would also need to be name-resolution logic in liquid to accompany it. Here's an example of what my data might look like:

A.yml

ingredients:
- avacado: &avacado
name: Avacado
color: Green
foods:
- *octopus_ceviche


B.yml

chefs:
- anthony_bourdain: &anthony_bourdain
name: Anthony Bourdain
hobby: Brazilian Jiu-Jitsu
foods:
- *octopus_ceviche


C.yml

foods:
- octopus_ceviche: &octopus_ceviche
name: Octopus Ceviche
taste: Delicious


If there's no way to include C.yml in A and B, then all the foods need to be shared in both places. If food is used in the md/html page entries need to be accessed by direct hash access (e.g.
{{ site.data.foods[octopus_ceviche] }}
), which a) doesn't seem to work and b) feels like too much logic for a view.

Answer Source

New answer based on the edited question:

*octupus_ceviche is a YAML alias and has nothing to do with Liquid. As I said, YAML files are not processed with Liquid. YAML, however, defines that aliases must point to achors in the same document. One YAML document must reside in one stream which for most YAML processors means that it cannot be split into multiple files.

That being said, a valid option would be to place all data into a single YAML file:

C:
  foods:
    - octopus_ceviche: &octopus_ceviche
        name: Octopus Ceviche
        taste: Delicious
A:
  ingredients:
    - avacado: &avacado
        name: Avacado
        color: Green
        foods:
          - *octopus_ceviche
B:
  chefs:
    - anthony_bourdain: &anthony_bourdain
        name: Anthony Bourdain
        hobby: Brazilian Jiu-Jitsu
        foods:
          - *octopus_ceviche

You may leave out A, B and C if their child keys are disjoint as they are in this example. Note however that the anchor must always be located in front of the alias (textually), even though YAML defines that mapping keys have no order. That's why I moved C in front.

Nota Bene: Anchors and aliases in YAML have been designed to serialize cyclic structures. Using them as named, reusable values is generally fine. But actually, you do not need a list with all defined “variables”, you can also just define them on first occurrence. Example:

A:
  ingredients:
    - avocado: &avocado
        name: Avocado
        color: Green
        foods:
          - &octopus_ceviche
            name: Octopus Ceviche
            taste: Delicious
B:
  chefs:
    - anthony_bourdain: &anthony_bourdain
        name: Anthony Bourdain
        hobby: Brazilian Jiu-Jitsu
        foods:
          - *octopus_ceviche

But this can be less readable of course. ymmv.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download