首页 文章

Jekyll:将变量从索引传递到帖子

提问于
浏览
3

我想提供RSS提要 . 我需要排除一些与javascript相关的功能,例如幻灯片,来自RSS内容 . 有没有办法告诉帖子(或邮件中的 {% include %} )是否被插入 index.html 文件或 feed.xml 文件?

通过简化,我希望看起来像这样:

index.html

---
layout: page
title: Index
include_slideshow: true
---
{% for post in paginator.posts %}
  {{ post.content }}
{% endfor %}

feed.xml

---
layout: nil
include_slideshow: false
---
{% for post in site.posts limit:10 %}
  {{ post.content }}
{% endfor %}

_posts/2013-10-01-random-post.html

---
layout: post
title: Random Post    
---
This is a random post.
{% if include_slideshow %}
INSERT SLIDESHOW HERE.
{% else %}
NOTHING TO SEE HERE, MOVE ALONG.
{% endif %}

问题是来自 index.htmlfeed.xml 的YAML前端的 include_slideshow 变量不可用于 _posts/2013-10-01-random-post.html . 有没有不同的方法来实现这一目标?

我正在通过GitHub页面部署,因此Jekyll扩展不是一个选项 .

1 回答

  • 1

    您可以使用一些拆分过滤器 . 您不需要识别正在渲染的布局,但在必要时操纵内容本身 .

    此示例适用于您的内容的多个幻灯片块, properly identified by the html comment

    index.html - 过滤幻灯片内容

    ---
    layout: page
    title: Index
    ---
    {% for post in paginator.posts %}
      {%capture post_content %}
      {% assign slideshows = post.content | split: "<!-- SLIDESHOW_CONTENT -->" %}
      {% for slide in slideshows %}
          {% if forloop.first %}
              {{ slide }}
          {% else %}
              {% assign after_slide = slide | split: "<!-- END_SLIDESHOW_CONTENT -->" %}
              {{ after_slide[1] }}
           {% endif %}
      {% endfor %}
      {%endcapture%}
      {{ post_content }}
    {% endfor %}
    

    feed.xml - 也过滤幻灯片内容

    ---
    layout: nil
    ---
    {% for post in site.posts limit:10 %}
      {%capture post_content %}
      {% assign slideshows = post.content | split: "<!-- SLIDESHOW_CONTENT -->" %}
      {% for slide in slideshows %}
          {% if forloop.first %}
              {{ slide }}
          {% else %}
              {% assign after_slide = slide | split: "<!-- END_SLIDESHOW_CONTENT -->" %}
              {{ after_slide[1] }}
           {% endif %}
      {% endfor %}
      {%endcapture%}
      <content type="html">{{ post_content | xml_escape }}</content>
    {% endfor %}
    

    post.html (您的帖子布局) - 请注意您不需要更改帖子模板,因为它会显示幻灯片

    ---
    layout: nil
    ---
    <article>
    ...
    <section>{{ content }}</section>
    ...
    </article>
    

    _posts/2013-10-01-random-post.html

    ---
    layout: post
    title: Random Post    
    ---
    <!-- SLIDESHOW_CONTENT -->
    <p>INSERT SLIDESHOW 0 HERE</p>
    <!-- END_SLIDESHOW_CONTENT -->
    
    This is a random post.
    
    Before slideshow 1!
    
    <!-- SLIDESHOW_CONTENT -->
    <p>INSERT SLIDESHOW 1 HERE</p>
    <!-- END_SLIDESHOW_CONTENT -->
    
    After slideshow 1!
    
    
    Before slideshow 2!
    
    <!-- SLIDESHOW_CONTENT -->
    <p>INSERT SLIDESHOW 2 HERE</p>
    <!-- END_SLIDESHOW_CONTENT -->
    
    After slideshow 2!
    

相关问题