如何使用不同的布局渲染所有Jekyll页面?

时间:2016-03-25 23:01:51

标签: ruby jekyll

我正在尝试创建一个Jekyll插件,该插件应该遍历所有帖子并使用不同的布局呈现它们。无法弄清楚如何做到这一点。这就是我到目前为止所做的:

module Jekyll
  class MyGenerator < Generator
    priority :low
    def generate(site)
      site.posts.docs.each do |doc|
        page = Page.new(site, site.source, File.dirname(doc.relative_path), doc.basename)
        page.do_layout(
          site.site_payload,
          'post' => Layout.new(site, site.source, '_layouts/my.html')
        )
        page.write(?)
        site.pages << page
      end
    end
  end
end

此代码不起作用。

1 个答案:

答案 0 :(得分:2)

在下面的代码中,我使用null布局第二次渲染我的所有页面。生成的文件都有后缀&#34; _BARE&#34;

module Jekyll
  class BareHtml < Page
    def initialize(site, base, dest_dir, src_dir, page)

      @site = site
      @base = base
      @dir = dest_dir
      @dest_dir = dest_dir
      @dest_name = page.basename
      file_name = "#{page.basename}_BARE.html"
      self.process(file_name)
      self.read_yaml(base, page.path)
      self.data['layout'] = nil               ###      <-- set the layout name here
    end
  end

  class BareHtmlGenerator < Generator
    safe true
    priority :low

    def generate(site)

      # Converter for .md > .html
      converter = site.find_converter_instance(Jekyll::Converters::Markdown)

      dest = site.dest
      src = site.source

      # Create destination path
      FileUtils.mkpath(dest) unless File.exists?(dest)

      site_pages = site.pages.dup

      site_pages.each do |page|
        bare = BareHtml.new(site, site.source, dest, src, page)
        bare.content = converter.convert(bare.content)
        bare.render(site.layouts, site.site_payload)
        bare.write(site.dest)
        site.pages << bare
      end

    end

  end

end