Home How to specify layout in nanoc?
Reply: 1

How to specify layout in nanoc?

kingmakerking
1#
kingmakerking Published in 2018-02-14 10:05:42Z

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?

ddfreyne
2#
ddfreyne Reply to 2018-02-14 22:04:10Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327317 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO