Jekyll在另一个YAML文件中包含一个YAML文件 [英] Jekyll Include one YAML file in another YAML file

查看:226
本文介绍了Jekyll在另一个YAML文件中包含一个YAML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jekyll项目,其中两个单独的页面(A.html和B.html)分别基于YAML文件A.yml和B.yml中的数据显示不同的内容.每个yml文件都有一堆定义相同的变量.我希望将此通用变量列表保留在第三个文件C.yml中,并将其同时包含在A.yml和B.yml中.我该怎么办?

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?

我尝试过的事情:

  • 使用*引用像*site.data.C.vars这样的全局数据-这没有解析.
  • 将C.yml放在_includes目录中,并使用前端事物将页面视为液体模板,然后调用{% include C.yml %}-已编译,但是发出的html页面没有任何内容.
  • 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.

在多个视图中使用公用数据文件对我来说还不够,因为还需要液态的名称解析逻辑来配合它.这是我的数据的示例:

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

如果无法在A和B中包含C.yml,则所有食物都必须在这两个地方共享.如果在md/html页面中使用了food,则需要通过直接哈希访问(例如{{ site.data.foods[octopus_ceviche] }})来访问条目,这a)似乎不起作用,b)感觉视图逻辑太多.

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.

推荐答案

基于已编辑问题的新答案:

New answer based on the edited question:

*octupus_ceviche是YAML 别名,与Liquid无关.如我所说,YAML文件不会用Liquid处理.但是,YAML定义别名必须指向同一文档中的 achors .一个YAML文档必须驻留在一个 stream 中,对于大多数YAML处理器而言,这意味着它不能拆分为多个文件.

*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.

话虽如此,一个有效的选择是将所有数据放入单个YAML文件中:

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

如果子键不相交(如本例所示),则可以省略ABC.但是请注意,即使YAML定义了映射键没有顺序,锚也必须始终位于别名的前面(按文本形式).这就是为什么我将C放在前面的原因.

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 :YAML中的锚和别名已设计为序列化循环结构.将它们用作已命名的可重用值通常很好.但是实际上,您不需要包含所有已定义变量"的列表,也可以仅在首次出现时对其进行定义.示例:

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.

这篇关于Jekyll在另一个YAML文件中包含一个YAML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆