2012-03-30 37 views
1

我真的需要Jekyll的幫助。 我非常努力地生成帖子按日期分組的頁面。Jekyll - 按日期分組的帖子

我想要的是擁有當前月份的所有帖子的主頁,然後可以瀏覽每個月。所以我必須生成一個索引頁面並生成所有其他每月頁面。

我是新來的紅寶石和傑基爾,所以我真的在探索。 我的主頁正在使用一些難看的代碼,但我的月刊不工作:帖子內容不是「編譯」並出現在原始紡織品中。

我的生成器類看起來是這樣的:

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 

而且我的模板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> 

輸出是這一個:

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

內容仍處於紡織品。 有什麼想法? 非常感謝! 亞歷山大


更新:

解決方案:

{{ post.content | textilize }} 

不起作用,因爲它沒有考慮到我的post.textile文件中設置帳戶值。


解決方案:

我發現了一些東西,如果我強迫一個帖子在循環渲染,它的工作:

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 

回答

1

找到解決方案:

我發現了一些東西,如果我強迫一個帖子在循環渲染,它的工作:

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 
0

內容仍然是紡織,因爲monthly.html是一個HTML文件,並且jekyll不會在HTML文件上運行任何文本處理器(Liquid除外,它只是用page.content中的文本進行替換,不進行轉換)。但是,jekyll有一些built-in Liquid filters來幫助這個,特別是textilize將紡織品轉換爲HTML。你會使用這樣的:

{{ post.content | textilize }} 
+0

你好,非常感謝你的回答,我試圖做到這一點,但它不考慮我的紡織文件中的變量(在_post文件夾中)。這是我的問題! – 2012-04-02 13:33:21

+0

剛剛找到了一個解決方案,如果我做了一箇中間渲染的帖子,它的工作原理:'def group_by_month(posts,site) months = [] posts_by_month = {} posts.reverse.each do | post |' * * post.render(site.layouts,網站。site_payload)** '鍵= Time.utc(post.date.year,post.date.month) 如果posts_by_month.has_key?(鍵) posts_by_month [關鍵] <<發佈 其他 posts_by_month [關鍵] = [帖] 月<<關鍵 結束 結束 回報[月,posts_by_month] end' – 2012-04-02 14:09:29

+0

@AlexandreAssouad,如果編輯成你的問題(「** **的解決方案」的標題下),該代碼就會越清晰。 – huon 2012-04-03 08:54:18