杰基尔 - 展示图像&滑块中的文字

时间:2018-05-17 14:45:12

标签: yaml jekyll liquid static-site

我正在使用Bootstrap-Carousel。

不幸的是,来自front-matter的数据未被呈现。

的index.html:

...
carousel:
  a:
    image: home/athenaBeta.jpeg 
    text: random text 1
  b: 
    image: home/transport.jpeg
    text: |
      random text 2
  c:
    image: home/coffee.jpeg 
    text: |
      random text 3
---
{% include hero-carousel.html %}
...

英雄carousel.html:

  <div id="carousel" class="carousel slide" data-ride="carousel" pause="false">
    <div class="carousel-inner text-center">
      {% for news in page.carousel %}
      {% capture image %}
        {% assign img = news.image %}
      {% endcapture %}
      <div class="item img-cover img-fixed {% if forloop.index == 0 %}active{% endif %}" style="background-image:url({{ image }})">
        <h2>{{ news.text }}</h2>
      </div>
      {% capture i %}{{ i | plus:1 }}{% endcapture %}
      {% endfor %}
    </div>

    <a class="left carousel-control" href="#carousel" role="button" data-slide="prev">
      <span class="fa glyphicon-chevron-left"></span>
    </a>
    <a class="right carousel-control" href="#carousel" role="button" data-slide="next">
      <span class="fa glyphicon-chevron-right"></span>
    </a>
    <a class="arrow-down" href="#content">
      <span class="fa glyphicon-chevron-down"></span>
    </a>
  </div>

它渲染了三张幻灯片,但是没有渲染任何图像或文本。我在浏览器中获得的输出如下:

<div id="carousel" class="carousel slide" data-ride="carousel" pause="false">
  <div class="carousel-inner text-center">
    <div class="item img-cover img-fixed " style="background-image:url(

      )">
      <h2></h2>
    </div>
    <div class="item img-cover img-fixed " style="background-image:url(

      )">
      <h2></h2>
    </div>
    <div class="item img-cover img-fixed " style="background-image:url()">
      <h2></h2>
    </div>
  </div>
  <a class="left carousel-control" href="#carousel" role="button" data-slide="prev">
    <span class="fa glyphicon-chevron-left"></span>
  </a>
  <a class="right carousel-control" href="#carousel" role="button" data-slide="next">
    <span class="fa glyphicon-chevron-right"></span>
  </a>
  <a class="arrow-down" href="#content">
    <span class="fa glyphicon-chevron-down"></span>
  </a>
</div>

1 个答案:

答案 0 :(得分:1)

当您在page.caroussel中循环时,会收到以下内容:

{{ news | inspect }}
==> ["a", {"image"=>"home/athenaBeta.jpeg", "text"=>"random text 1"}]

这是一个包含两个元素的数组。

为了覆盖你的形象,你可以{% assign img = news[1].image %},但你最好重构一下:

...
carousel:
  - image: home/athenaBeta.jpeg
    text: random text 1

  - image: home/transport.jpeg
    text: |
      random text 2

  - image: home/coffee.jpeg
    text: |
      random text 3
---

<div id="carousel" class="carousel slide" data-ride="carousel" pause="false">
  <div class="carousel-inner text-center">
  {% for news in page.carousel %}
    <div class="item img-cover img-fixed {% if forloop.first %}active{% endif %}" style="background-image:url('{{ news.image }}')">
      <h2>{{ news.text }}</h2>
    </div>
  {% endfor %}
</div>