Skip to content

Multiple layouts

There are some ways to solve the problem of pages needing multiple layouts. The enveloping guide can introduce you the #envelope method.

Inheritance

Your pages can inherit from layout classes.

class MainLayout
  include Blueprint::HTML

  abstract def blueprint

  private def envelope(&)
    html do
      body do
        div class: "main-layout" do
          yield
        end
      end
    end
  end
end
class AuthLayout
  include Blueprint::HTML

  abstract def blueprint

  private def envelope(&)
    html do
      body do
        div class: "auth-layout" do
          yield
        end
      end
    end
  end
end
class LoginPage < AuthLayout
  private def blueprint
    h1 { "Sign In" }
  end
end
class LoginPage < MainLayout
  private def blueprint
    h1 { "Welcome" }
  end
end

Generics

Your base page can have a generic type.

class BasePage(T)
  include Blueprint::HTML

  abstract def blueprint

  private def envelope(&)
    render(T.new) do
      yield
    end
  end
end
class LoginPage < BasePage(AuthLayout)
  private def blueprint
    h1 { "Sign In" }
  end
end
class LoginPage < BasePage(MainLayout)
  private def blueprint
    h1 { "Welcome" }
  end
end
class MainLayout
  include Blueprint::HTML

  private def blueprint(&)
    html do
      body do
        div class: "main-layout" do
          yield
        end
      end
    end
  end
end
class AuthLayout
  include Blueprint::HTML

  private def blueprint(&)
    html do
      body do
        div class: "auth-layout" do
          yield
        end
      end
    end
  end
end