Class: Wrapt

Inherits:
Object
  • Object
show all
Defined in:
lib/wrapt/wrapt.rb,
lib/wrapt/layout.rb,
lib/wrapt/helpers.rb,
lib/wrapt/layout_context.rb

Overview

Wrapt is a specialised middleware that wraps content in a given layout

Wrapt injects an object into the environment which you can use to provide content to. When you’re done, return the wrapt object as the body for your request and the layout will be applied.

You can pass variables through the rack through the environment via the ‘request.variables’ key which will be a hash like object

def call(e)

wrapt = e['layout']
wrapt.content = "Here's some content"
[200, {"Content-Type" => "text/html"}, wrapt]

end

Produces:

<!-- wrapping layout -->
  Here's some content
<!-- footer wrapping layout -->

A layout directory may be specified that points to any layouts that are to be used.

A format may be specified for the layout if it is intended not to use html. Simply tell wrapt the format to use via the format= method.

If you don’t want a layout, simply don’t use the wrapt object.

Defined Under Namespace

Modules: Helpers Classes: Layout, LayoutContext

Constant Summary collapse

IGNORE_LAYOUT =
lambda{|e| false}

Instance Method Summary collapse

Constructor Details

#initialize(app) {|_self| ... } ⇒ Wrapt

Wrapt is initialized as middleware in a Rack stack

Yields:

  • (_self)

Yield Parameters:

  • _self (Wrapt)

    the object that the method was called on

See Also:



41
42
43
44
45
# File 'lib/wrapt/wrapt.rb', line 41

def initialize(app)
  @app = app
  @master = false
  yield self if block_given?
end

Instance Method Details

#call(env) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/wrapt/wrapt.rb', line 95

def call(env)
  env['request.variables'] ||= Hashie::Mash.new
  layout = env['layout']
  if !layout || (layout && !layout.master?)
    env['layout'] = Layout.new(self, env)
  end
  r = @app.call(env) # just return what the app returns… If it wants a layout, it will return it.
  env['layout'] = layout if layout
  r
end

#default_formatObject

Get the default format that has been defined for the instance of wrapt

The format is used by default in the template file name. The default naming convention for the template name is

<template_name>.<format>.<template_type>

Examples:

application.html.haml


150
151
152
# File 'lib/wrapt/wrapt.rb', line 150

def default_format
  @default_format ||= :html
end

#default_format=(format) ⇒ Object

Set the default format for this instance of wrapt

See Also:



157
158
159
# File 'lib/wrapt/wrapt.rb', line 157

def default_format=(format)
  @default_format = format
end

#default_templateString

The default template name wrapt will use when none is specified

Returns:

  • (String)

    default template name



130
131
132
# File 'lib/wrapt/wrapt.rb', line 130

def default_template
  @default_template ||= "application"
end

#default_template=(name) ⇒ Object

set the default template

See Also:



137
138
139
# File 'lib/wrapt/wrapt.rb', line 137

def default_template=(name)
  @default_template = name
end

#ignore_layout(&block) ⇒ Object

Wrapt allows you to ignore layouts from the client side.

This may be useful for esi, or ajax, where you want the content, but not the layout

The block is provided with the Rack environment from the request

See Also:



71
72
73
# File 'lib/wrapt/wrapt.rb', line 71

def ignore_layout(&block)
  @ignore_layout = block
end

#ignore_layout?(env) ⇒ Boolean

Checks to see if the layout should be ignored for this request.

GET “/?apply_layout=false” # Layout is ignored

Examples:

use Wrapt do |wrapt|
  wrapt.ignore_layout do |env|
    request = Rack::Request.new(env)
    params["apply_layout"] == false
  end
end
run MyApp

Returns:

  • (Boolean)

See Also:



90
91
92
93
# File 'lib/wrapt/wrapt.rb', line 90

def ignore_layout?(env)
  @ignore_layout ||= IGNORE_LAYOUT
  @ignore_layout.call(env)
end

#layout_dirsArray

Provides access to the directories where wrapt will inspect to find the layouts

Returns:

  • (Array)

    An array of directories that wrapt will look in for template files



117
118
119
120
121
122
123
124
125
# File 'lib/wrapt/wrapt.rb', line 117

def layout_dirs
  @layout_dirs ||= begin
    [
      File.join(Dir.pwd, "layouts"),
      File.join(Dir.pwd, "views/layouts"),
      File.join(Dir.pwd, "app/views/layouts")
    ]
  end
end

#layout_dirs=(dirs) ⇒ Object

Set the layout directories These are the directories that wrapt will inspect (in order) when it attempts to find the given layouts

Parameters:

  • dirs (Array)

    An array of directories where wrapt should look to find the layout templates



110
111
112
# File 'lib/wrapt/wrapt.rb', line 110

def layout_dirs=(dirs)
  @layout_dirs = Array.new(dirs).flatten
end

#master!Object

Declare this wrapt instance to be the master This will mean that all downstream wrapt instances will be ignored and this layouter will be used for the entire downstream graph



52
53
54
# File 'lib/wrapt/wrapt.rb', line 52

def master!
  @master = true
end

#master?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks to see if this layouter is a master

Returns:

  • (Boolean)


58
59
60
# File 'lib/wrapt/wrapt.rb', line 58

def master?
  !!@master
end

#template(name, opts = {}) ⇒ Tilt::Template|NilClass

Fetches the named template with any given options

Parameters:

  • The (String|Symbol)

    template name to fetch

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :format (String|Symbol)

    Provide the format for the template that will be used

Returns:

  • (Tilt::Template|NilClass)

    A template file to use to render the layout



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/wrapt/wrapt.rb', line 168

def template(name, opts={})
  format = opts.fetch(:format, default_format)
  template_name = template_name_and_format_glob(name,format, opts)

  return _template_cache[template_name] if _template_cache[template_name]

  file = nil
  layout_dirs.detect do |dir|
    file = Dir[File.join(dir, template_name)].first
  end

  if file.nil?
    nil
  else
    _template_cache[template_name] = Tilt.new(file)
  end
end