Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 630
Philip Durbin Avatar asked Dec 13 '11 14:12

Philip Durbin


People also ask

What are Jekyll templates?

Jekyll has an extensive theme system that allows you to leverage community-maintained templates and styles to customize your site's presentation. Jekyll themes specify plugins and package up assets, layouts, includes, and stylesheets in a way that can be overridden by your site's content.

What is front matter in Jekyll?

Front matter tells Jekyll to parse a file. You add predefined variables, which are YAML sets, to the front matter. Then, you can use Liquid tags in your files to access the front matter. Front matter is indicated with two triple-dashed lines.


2 Answers

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.

like image 59
Martin Avatar answered Sep 23 '22 15:09

Martin


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.

like image 37
Hakan Ensari Avatar answered Sep 22 '22 15:09

Hakan Ensari