簡體   English   中英

具有嵌套元素的Jekyll YAML文件

[英]Jekyll YAML file with nested elements

我想訪問“ benefit.yml”文件中的嵌套元素,並使用循環調用這些元素。 但是它不起作用,什么也沒有出現。

這是我的“ benefit.yml”:

locales:
  en:
    title: "Games"
    detail: "To relax and take breaks during the day, we play football, table tennis and Xbox"
    icon: benefit1.png
  fr:
    title: "Jeux"
    detail: "To relax and take breaks during the day, we play football, table tennis and Xbox"
    icon: benefit1.png

  en:
    title: "Drink"
    detail: "The fridge is full of beer cans and Coca-Cola, Ice Tea and coffee. You can drink whatever makes you happy, all day!"
    icon: benefit2.png
  fr:
    title: "Boissons"
    detail: "The fridge is full of beer cans and Coca-Cola, Ice Tea and coffee. You can drink whatever makes you happy, all day!"
    icon: benefit2.png

這是我的循環:

  {% for benefit in site.data.benefits.locales.en %}
    <div class="s-column6">
      <div class="kiwup-benefit pb1 mb1">
        <img src="/image/benefit/{{ benefit.icon }}" alt="kiwuper">
        <div class="kiwup-benefit-info">
          <h3 class="h4-like text-dark">{{ benefit.title }}</h3>
          <p>{{ benefit.detail }}</p>
        </div>
      </div>
    </div>
  {% endfor %}

1.您的數據文件錯誤,您的locales.enlocales.fr條目重疊。 正確的嵌套可以是:

語言環境:

games:
  en:
    title: "Games"
    detail: "To relax and take breaks during the day, we play football, table tennis and Xbox"
    icon: benefit1.png
  fr:
    title: "Jeux"
    detail: "To relax and take breaks during the day, we play football, table tennis and Xbox"
    icon: benefit1.png

drink:
  en:
    title: "Drink"
    detail: "The fridge is full of beer cans and Coca-Cola, Ice Tea and coffee. You can drink whatever makes you happy, all day!"
    icon: benefit2.png
  fr:
    title: "Boissons"
    detail: "The fridge is full of beer cans and Coca-Cola, Ice Tea and coffee. You can drink whatever makes you happy, all day!"
    icon: benefit2.png

2.數據文件名

如果您的數據文件名是benefit.yml你訪問你的DATAS site.data.benefit.locales.drink

3.你的循環是錯誤的

然后循環給你類似:

benefit = Array
["en",{"title"=>"Drink", "detail"=>"The fridg...!", "icon"=>"benefit2.png"}]

其中benefit[0] = enbenefit[1] = hash{"title"=>"Drink", "detail"=>"The fridg...!", "icon"=>"benefit2.png"}

如果您想在此處訪問數據,可以執行{{ benefit[1].title }}

games語言環境的完整代碼:

{% for benefit in site.data.benefit.locales.games %}
<div class="s-column6">
  <div class="kiwup-benefit pb1 mb1">
    <img src="/image/benefit/{{ benefit[1].icon }}" alt="kiwuper">
    <div class="kiwup-benefit-info">
      <h3 class="h4-like text-dark">{{ benefit[1].title }}</h3>
      <p>{{ benefit[1].detail }}</p>
    </div>
  </div>
</div>
{% endfor %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM