Jarvis
Jarvis

Reputation: 81

Generator plugin doesn't render data

I am writing a jekyll plugin to render an alternative layout for my posts in category "portfolio". The files are generated in the partial folder but the data is not rendered. What am I doing wrong?

Generator:

module Jekyll
  class PartialGenerator < Generator
    def generate(site)
      site.categories['portfolio'].each do |post|
        site.pages <<  PartialPage.new(site, site.source, post)
      end
    end
  end

  class PartialPage < Page
    def initialize(site, base, post)
      @site = site
      @base = base
      @dir  = 'partials'
      @name = "#{post.id}.html".tr('/','')


      self.process(name)
      self.read_yaml(File.join(base, '_layouts'), "partial.html")
      self.data['page'] = post
    end
  end
end

The partial.html layout includes <h1>{{ page.title }}</h1> and the generated output is <h1></h1>

I've tried puts(post.title) in the initialize method and it prints the correct titles on the console.

Upvotes: 0

Views: 231

Answers (1)

Jarvis
Jarvis

Reputation: 81

I found the solution myself now.

The data you put into self.data[]is available as property of page in the template/layout.

So self.data[‘test'] = "..." can be accessed through {{page.test}}.

Now my generator includes self.data['page'] = post and I access it in my partial.html as <h1>{{page.post.title}}</h1>.

Upvotes: 1

Related Questions