rubyjekyllliquidoctopress

Add properties to a page from a Jekyll plugin


Say I want to have a page with content like this:

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

There are no variables on the page named comment_count or comment_list by default; instead I want these variables to be added to the page from a Jekyll plugin. Where is a safe place I can populate those fields from without interfering with Jekyll's existing code?

Or is there a better way of achieving a list of comments like this?


Solution

  • Unfortunately, there isn't presently the possibility to add these attributes without some messing with internal Jekyll stuff. We're on our way to adding hooks for #after_initialize, etc but aren't there yet.

    My best suggestion is to add these attributes as I've done with my Octopress Date plugin on my blog. It uses Jekyll v1.2.0's Jekyll::Post#to_liquid method to add these attributes, which are collected via send(attr) on the 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[ ... ]) will ensure that all the old attributes are still included, then collect the return values of the methods corresponding to the entries in the String array.

    This is the best means of extending posts and pages so far.