2013-10-11 39 views
4

說我想有一個頁面,像這樣的內容在頁面中添加屬性:從傑基爾插件

<h1>{{page.comment_count}} Comment(s)</h1> 
{% for c in page.comment_list %} 
<div> 
    <strong>{{c.title}}</strong><br/> 
    {{c.content}} 
</div> 
{% endfor %} 

有默認名爲comment_countcomment_list頁面上沒有變量;相反,我希望將這些變量添加到Jekyll插件的頁面中。哪裏是安全的地方,我可以填充這些領域,而不會干擾Jekyll現有的代碼?

或者是否有更好的方式來實現這樣的評論列表?

+0

如果有幫助,這是目前用來獲取這些屬性的「黑客」,但它很髒並且不保證與未來版本的Jekyll兼容:https://github.com/IQAndreas/ jekyll-static-comments/blob/master/static_comments.rb有沒有更好的解決方案呢? – IQAndreas

回答

3

不幸的是,目前還沒有添加這些屬性的可能性,而沒有與內部的Jekyll東西搞混。我們正在爲#after_initialize等添加鉤子,但還沒有。

我最好的建議是添加這些屬性,就像我在我的博客上用my Octopress Date plugin所做的那樣。它採用哲基爾V1.2.0的Jekyll::Post#to_liquid方法通過send(attr)Post添加這些屬性,這些屬性收集:

class Jekyll::Post 

    def comment_count 
    comment_list.size 
    end 

    def comment_list 
    YAML.safe_load_file("_comments/#{self.id}.yml") 
    end 

    # Convert this post into a Hash for use in Liquid templates. 
    # 
    # Returns <Hash> 
    def to_liquid(attrs = ATTRIBUTES_FOR_LIQUID) 
    super(attrs + %w[ 
     comment_count 
     comment_list 
    ]) 
    end 
end 

super(attrs + %w[ ... ])將確保所有的舊的屬性還包括,然後收集的返回值方法對應於String陣列中的條目。

這是迄今爲止擴展帖子和頁面的最佳方式。

+0

這個實現了嗎? – 0xcaff

+0

從Jekyll 3.2.0開始,我相信這不起作用了。 –