Alexandre Babouin
Alexandre Babouin

Reputation: 71

Jekyll - group posts by date

I really need help with Jekyll. I'm desesperately trying to generate pages where posts are grouped by date.

What I want is to have a home page with all posts of the current month and then the possibility to browse every months. So I've got to generate an index page and generate all other monthly pages.

I'm new to ruby and Jekyll so I'm really exploring. My home page is working with some ugly code but my monthly pages aren't working : the post content is not "compiled" and appear in raw textile.

My generator class looks like this :

class MonthlyGenerator < Generator
safe true

def group_by_month(posts)
  months = []
  posts_by_month = {}
  posts.reverse.each do |post|
    key = Time.utc(post.date.year, post.date.month)
    if posts_by_month.has_key?(key)
      posts_by_month[key] << post
    else
      posts_by_month[key] = [post]
      months << key
    end
  end
  return [months,posts_by_month]
end

def generate(site)
  data = group_by_month(site.posts)
  months = data[0]
  posts_by_month = data[1]
  months.each_with_index do |month, index|
    if index >= 0 && index < months.length
      nextMonth = months[index+1]
    else 
      nextMonth = false
    end
    if index > 0 && index <= months.length
      previousMonth = months[index-1]
    else 
      previousMonth = false
    end 
    posts = posts_by_month[month]

    dir = "/"+month.year.to_s+"-"+month.mon.to_s
    write_monthly_posts(site, dir, month, posts, nextMonth, previousMonth)
  end
end

def write_monthly_posts(site, dir, month, posts, nextMonth, previousMonth)
    monthlypage = MonthlyPage.new(site, dir, month, posts, nextMonth, previousMonth)
    monthlypage.render(site.layouts, site.site_payload)
    monthlypage.write(site.dest)
    site.pages << monthlypage
end
end

And my template monthly.html :

<div class="span3">
  <h1>{{ page.month }}</h1>
</div>
<div class="span9">
  {% for post in page.posts %}
      <a href="{{ post.url }}">{{ post.title | truncate:65 }}</a>
      {{ post.content }}
  {% endfor %}
</div>

The output is this one :

<div class="span3">
    <h1>Sat Nov 01 00:00:00 UTC 2008</h1>
</div>
<div class="span9">

      <a href="/cat1/2008/11/19/test2.html">Test2</a>
       h1. test 

p(meta). 1nounmjlkjlktest2

2008 is a leap year. That means that three hundred and sixty six days 


<iframe class="video" src="http://player.vimeo.com/video/?title=0&amp;byline=0&amp;portrait=0" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
</iframe>

</div>

the content is still in textile. Any idea ? Thanks a lot ! Alexandre


update :

the solution :

{{ post.content | textilize }}

doesn't work because it doesn't take into account values set in my post.textile file.


Solution :

I found something, If I force a post to render in the loop, it does the job :

def group_by_month(posts, site) 
    months = [] posts_by_month = {} 
    posts.reverse.each do |post| 
        post.render(site.layouts, site.site_payload) 
        key = Time.utc(post.date.year, post.date.month) 
        if posts_by_month.has_key?(key) 
            posts_by_month[key] << post 
        else 
            posts_by_month[key] = [post] 
            months << key 
        end 
    end 
    return [months,posts_by_month] 
end

Upvotes: 1

Views: 860

Answers (2)

Alexandre Babouin
Alexandre Babouin

Reputation: 71

Found Solution :

I found something, If I force a post to render in the loop, it does the job :

def group_by_month(posts, site) 
    months = [] posts_by_month = {} 
    posts.reverse.each do |post| 
        **post.render(site.layouts, site.site_payload)**
        key = Time.utc(post.date.year, post.date.month) 
        if posts_by_month.has_key?(key) 
            posts_by_month[key] << post 
        else 
            posts_by_month[key] = [post] 
            months << key 
        end 
    end 
    return [months,posts_by_month] 
end

Upvotes: 1

huon
huon

Reputation: 102276

The content is still in Textile because monthly.html is a HTML file, and jekyll doesn't run any text processor over HTML files (other than Liquid, which just does a substitution with the text in page.content, no conversion). However, jekyll has some built-in Liquid filters to help with this, specifically textilize to convert Textile to HTML. You would use it like this:

{{ post.content | textilize }}

Upvotes: 0

Related Questions