module Sinatra::FuzzyLayout

Sinatra::FuzzyLayout

Sinatra::FuzzyLayout is an extension to enable or disable layouts for views with much more control. By default, regular Sinatra provides two options:

  1. You can specify whether the layout should be enabled/disabled for all views.

  2. You can specify whether the layout should be enabled/disabled for an individual route/view.

This extension provides two DSL methods `enable_layout_for` and `disable_layout_for` which accept multiple parameters using which you can have greater control over the layout-ing.

Usage

In your application, you can use the `enable_layout_for` or `disable_layout_for` DSL methods to specify which route/view should have the layout and which routes/views should not.

enable_layout_for :index, :home, /user/

All views that have the name “index”, “home” or any view whose name matches the regex /user/ will have it's layout disabled.

disable_layout_for /reports/

The layout will be disabled for all views that match the regex /reports/ like “user_reports.slim” or “usage_reports.erb”

Classic Application

To use the extension in a classic application, all you have to do is require it.

require "sinatra"
require "sinatra/fuzzy_layout"

enable_layout_for :index, :home, /user/
disable_layout_for /reports/

# rest of the application goes here

Modular Application

To use this in a modular application, you need to `register` the extension like so:

require "sinatra/base"
require "sinatra/fuzzy_layout"

class App < Sinatra::Base
  register Sinatra::FuzzyLayout

  enable_layout_for :one, :two, :three

  # rest of the application goes here
end

Public Class Methods

registered(app) click to toggle source
# File lib/sinatra/fuzzy_layout.rb, line 119
def self.registered(app)
  app.set :enable_list, []
  app.set :disable_list, []
  app.helpers TemplatesHelpers
end

Public Instance Methods

disable_layout_for(*templates) click to toggle source
# File lib/sinatra/fuzzy_layout.rb, line 115
def disable_layout_for(*templates)
  settings.disable_list.push(*templates)
end
enable_layout_for(*templates) click to toggle source
# File lib/sinatra/fuzzy_layout.rb, line 111
def enable_layout_for(*templates)
  settings.enable_list.push(*templates)
end