John Topley
John Topley

Reputation: 115372

Displaying a quote from the first post using Jekyll

I'm trying to set up Jekyll so that a quote from the first post in the list of posts is displayed in a sidebar, but I can't quite work out how to do it. I have the quote text defined as a quote variable within the YML Front Matter in each post's Markdown.

This is the relevant extract from my default.html:

<div id="content">
  {{ content }}
</div>    
<div id="sidebar">
  <blockquote>{{ page.quote }}</blockquote>
</div>    

And this is my index.html:

---
layout: default
quote: ** Can a variable referencing the first post go here? **
---
{% for post in site.posts limit:10 %}
  <h2>{{ post.title }}</h2>
  <div class="post">
    {{ post.content }}
  </div>
{% endfor %}

Upvotes: 0

Views: 2168

Answers (2)

John Topley
John Topley

Reputation: 115372

After much experimentation, I was able to solve the problem using this Liquid snippet within default.html:

<div id="sidebar">
  <blockquote>
  {% if page.quote %}
    {{ page.quote }}
  {% else %}
    {{ site.posts.first.quote }}
  {% endif %}
  </blockquote>
</div>

Upvotes: 2

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

{% for post in site.posts limit:10 %}
  {% if forloop.index0 == 0 %}
    {% assign quote = post.quote %}
  {% endif %}

  <h2>{{ post.title }}</h2>
  <div class="post">
    {{ post.content }}
  </div>
{% endfor %}

and in default.html

<div id="content">
  {{ content }}
</div>    
<div id="sidebar">
  <blockquote>{{ quote }}</blockquote>
</div> 

I don't think you can store references in the YML matter but this should get what you want.

Upvotes: 3

Related Questions