Search code examples
rubyhamlmiddleman

Use nested layouts in Middleman


I'm basically work with Middleman 2 though if this can only be done in Middleman 3 I can switch to it

I have layout.haml having all the boilerplate and simultaneously being the index's layout.

Now I want the inner.haml layout that will work for the rest of the pages, will inherit from layout.haml (in terms that I won't repeat the boilerplate parts), will incude some extra common styles / scripts, some common markup and will then re-place the yield blocks.

Currently I don't realize at all from where should I start. I understand how I can set inner.haml as a default layout and layout.haml as a layout for the "/" route, but how would the system know that the inner.haml is actually nested within layout.haml?

Sample setup

layout.haml

!!!5
%html
  %head
    %script(src="HTML5 shiv")
    %title
      My Site
      \|
      = yield_content :title
    = stylesheet_link_tag "site.css"
    = yield_content :page_styles
  %body
    %div(role="main")
      = yield_content :content
    %script(src="jquery")
    = yield_content :page_scripts

index.html.haml

- content_for :title do
  Index
- content_for :page_styles do
  = stylesheet_link_tag "index.css"
- content_for :page_scripts do
  %script(src="index.js")
- content_for :content do
  Cool banner here

inner.haml

-# somehow inherits from / extends layout.haml
- content_for :page_styles do
  -# somehow I'm putting some common content and then reinclude the block from the specific page
  = stylesheet_link_tag "inner.css"
  = yield_content :page_styles
-# same thing for page_scripts
- content_fir :content do
  -# again I define some common HTML, then include page's content

Solution

  • In 3.0 you'd have index use the layout inner which would wrap_layout layout.

    In 2.0, you'll need a combination of partials and content_for blocks.