How can I make Jekyll use a layout without specifying it?
Asked Answered
S

4

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?

Susie answered 13/12, 2011 at 14:6 Comment(2)
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.)Gredel
@AlanW.Smith I agree it would be a nice addition; I just opened issue #453: Option for "layout: default" in _config.yml: github.com/mojombo/jekyll/issues/453Susie
T
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.

Truncation answered 21/5, 2014 at 8:21 Comment(1)
Does this allow to specify a title as well? I tried to put title: test in the values but nothing happens.Saarinen
E
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.

Eager answered 19/7, 2013 at 7:4 Comment(1)
What you did is still considered monkey patching it's just traceable.Legibility
S
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.

Susie answered 15/12, 2011 at 3:42 Comment(1)
Evolved versionDiagnose
O
0

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

Orgulous answered 2/3, 2013 at 20:34 Comment(1)
You're right. That's why I posted that implicit-layout.rb plugin as a workaround: #8491028Susie

© 2022 - 2024 — McMap. All rights reserved.