如何用不同的布局呈现所有Jekyll页面? [英] How to render all Jekyll pages with a different layout?

查看:66
本文介绍了如何用不同的布局呈现所有Jekyll页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Jekyll插件,该插件应遍历所有帖子并以不同的布局呈现它们.无法弄清楚该怎么做.到目前为止,这就是我的意思:

I'm trying to create a Jekyll plugin, which should go through all posts and render them with a different layout. Can't figure out how to do that. That's what I have so far:

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

此代码无效.

推荐答案

在下面的代码中,我第二次使用null布局呈现所有页面.结果文件的后缀均为"_BARE"

In my code below, I'm rendering all my pages a second time with the null layout. The resulting files all have the suffix "_BARE"

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

这篇关于如何用不同的布局呈现所有Jekyll页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆