3
votes

I'm moving a blog to Jekyll, and I'd like to offer an RSS feed. I need to exclude some javascript-dependent features, e.g. slideshows, from the RSS content. Is there any way to tell a post (or an {% include %} in the post) whether it's being inserted into the index.html file or the feed.xml file?

With much simplification, here's how I'd like it to look:

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 %}

The problem is that the include_slideshow variable from the YAML front matter of index.html and feed.xml is not available to _posts/2013-10-01-random-post.html. Is there a different way to accomplish this?

I'm deploying via GitHub pages, so Jekyll extensions are not an option.

1

1 Answers

2
votes

You can play around with some split filters. You don't need to identify the layout being rendered, but manipulate the content itself when necessary.

This example works for multiple chunks of slideshows, properly identified by the html comment, on your content:

index.html - filtering slideshow content

---
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 - also filtering slideshow content

---
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 (your post layout) - please notice you don't need to change your post template since it will display the slideshow

---
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!