24

In order to keep some of my Jekyll sites simple, I'm always using the same layout. That is to say, I'm always writing something like. . .

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

. . . as the YAML Front Matter at the top of my pages.

What I'd rather write, however is only. . .

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

. . . and have Jekyll assume that it should use a certain layout, as if I had explicitly written "layout: default" (or whatever), as above.

I don't see a way to specify this behavior in _config.yml. Maybe I could write a Jekyll plugin that would allow this. . . any ideas?

Philip Durbin
  • 4,042
  • 2
  • 25
  • 36
  • 1
    If you end up writing (or finding) a plugin that does this, please post back here. I think that would be a nice improvement. Assuming there isn't some way I don't know about to do it already. (Putting "layout: default" in the _config.yml doesn't work, unfortunately.) – Alan W. Smith Dec 13 '11 at 15:19
  • 1
    @AlanW.Smith I agree it would be a nice addition; I just opened issue #453: Option for "layout: default" in _config.yml: https://github.com/mojombo/jekyll/issues/453 – Philip Durbin Dec 15 '11 at 05:52

4 Answers4

27

This can be done using Frontmatter defaults:

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

This setting is available since Jekyll Version 2.0.0.

hoijui
  • 3,615
  • 2
  • 33
  • 41
Martin
  • 10,738
  • 14
  • 59
  • 67
  • Does this allow to specify a title as well? I tried to put `title: test` in the values but nothing happens. – Pithikos Jan 05 '15 at 11:54
5

Shorter and with no monkey-patching:

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

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

Caveat: GH Pages won't run your plugins.

Hakan Ensari
  • 1,969
  • 1
  • 18
  • 32
0

By default, you can't do this. Jekyll needs the YAML to specify layout so it knows where to drop it in at.

Brett Hardin
  • 4,012
  • 2
  • 19
  • 22
  • You're right. That's why I posted that `implicit-layout.rb` plugin as a workaround: http://stackoverflow.com/questions/8490528/how-can-i-make-jekyll-use-a-layout-without-specifying-it/8514849#8514849 – Philip Durbin Mar 03 '13 at 12:16
0

Here's a Jekyll plugin you can drop in as _plugins/implicit-layout.rb, for example:

# 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

From hanging out on #jekyll on freenode, I'm given to understand this is a monkey patch.

As Alan W. Smith commented, being able to put "layout: default" in _config.yml would be a nice improvement to this plugin.

Ideally (from my perspective), this functionality could be incorporated in Jekyll itself so a plugin wouldn't be necessary.

Philip Durbin
  • 4,042
  • 2
  • 25
  • 36
  • [Evolved version](https://github.com/duckinator/jekyll-default-layout/blob/master/default-layout.rb) – Sawny Jun 01 '13 at 21:47