kingmakerking
kingmakerking

Reputation: 2089

How to specify layout in nanoc?

In Jekyll, we write layout structure in a HTML file and put it in _layout folder. Say, the _layout folder has file named welcome.html, for a webpage to use it, we simply specify as follows:

---
title: Awesome webpage
layout: welcome
---

How to do the same when using nanoc? I thought it works the same way, but unfortunately, it seems like not picking up the welcome template. It is just picking up the default.html template.

Is it because of the file called rule which has the following lines?

compile '/' do
  filter :erb
  layout 'default'
end

I want only one specific file to pick up the welcome layout. How to do it?

Upvotes: 1

Views: 187

Answers (1)

Ian Young
Ian Young

Reputation: 121

In the Rules file, call #layout like this:

compile '/' do
  filter :erb
  layout @item[:layout]
end

This will call #layout with whatever is in the layout attribute of the item.

To fall back to a default when no layout attribute is specified, use #fetch:

compile '/' do
  filter :erb
  layout @item.fetch(:layout, '/default.*')
end

This will use whatever layout is specified in the layout attribute, falling back to the layout that matches /default.*.

For your specific case, you probably want the layout attribute to be something like /welcome.html, like this:

---
title: Awesome webpage
layout: /welcome.html
---

... but you can also manipulate the string you get back from #fetch if you prefer, so that putting just welcome in the item metadata will work. This would be less work overall if you were importing a lot of Jekyll pages, for example.

Upvotes: 3

Related Questions