24

为了让我的一些 Jekyll 网站保持简单,我总是使用相同的布局。也就是说,我总是在写类似的东西。. .

---
layout: default
title: Here's my Title
---

. . . 作为我页面顶部的YAML Front Matter 。

然而,我宁愿写的只是。. .

---
title: Here's my Title
---

. . . 并让 Jekyll 假设它应该使用某种布局,就好像我已经明确地写了“ layout: default”(或其他),如上所述。

我看不到在_config.yml. 也许我可以编写一个Jekyll 插件来实现这一点。. . 有任何想法吗?

4

4 回答 4

27

这可以使用Frontmatter 默认值来完成:

defaults:
  -
    scope:
      path: "" # empty string for all files
    values:
      layout: "default"

此设置从 Jekyll版本 2.0.0开始可用。

于 2014-05-21T08:21:07.527 回答
5

更短且没有猴子补丁:

# _plugins/implicit_layout.rb
module ImplicitLayout
  def read_yaml(*args)
    super
    self.data['layout'] ||= 'post'
  end
end

Jekyll::Post.send(:include, ImplicitLayout)

警告:GH Pages 不会运行您的插件。

于 2013-07-19T07:04:51.610 回答
0

这是一个 Jekyll 插件,您可以将其放入_plugins/implicit-layout.rb,例如:

# By specifying an implicit layout here, you do not need to
# write, for example "layout: default" at the top of each of
# your posts and pages (i.e. in the "YAML Front Matter")
#
# Please note that you should only use this plugin if you
# plan to use the same layout for all your posts and pages.
# To use the plugin, just drop this file in _plugins, calling it
# _plugins/implicit-layout.rb, for example
IMPLICIT_LAYOUT = 'default'

module Jekyll
  module Convertible

    def read_yaml(base, name)
      self.content = File.read(File.join(base, name))

      if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
        self.content = $POSTMATCH

        begin
          self.data = YAML.load($1)
          self.data["layout"] = IMPLICIT_LAYOUT
        rescue => e
          puts "YAML Exception reading #{name}: #{e.message}"
        end
      end

      self.data ||= {}
    end

  end
end

通过在 freenode 上的 #jekyll 上闲逛,我了解到这是一个猴子补丁。

正如 Alan W. Smith 评论的那样,能够将 " layout: default" 放入_config.yml将是对这个插件的一个很好的改进。

理想情况下(从我的角度来看),此功能可以合并到 Jekyll 本身中,因此不需要插件。

于 2011-12-15T03:42:58.760 回答
0

默认情况下,您不能这样做。Jekyll 需要 YAML 来指定布局,以便知道将其放入何处。

于 2013-03-02T20:34:01.847 回答