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:



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

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

Instance Method Details

#call(env) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/wrapt/wrapt.rb', line 110

def call(env)
  env['request.variables'] ||= Hashie::Mash.new
  layout = env['layout']
  if !layout || (!self.defered? && !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


165
166
167
# File 'lib/wrapt/wrapt.rb', line 165

def default_format
  @default_format ||= :html
end

#default_format=(format) ⇒ Object

Set the default format for this instance of wrapt

See Also:



172
173
174
# File 'lib/wrapt/wrapt.rb', line 172

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



145
146
147
# File 'lib/wrapt/wrapt.rb', line 145

def default_template
  @default_template ||= "application"
end

#default_template=(name) ⇒ Object

set the default template

See Also:



152
153
154
# File 'lib/wrapt/wrapt.rb', line 152

def default_template=(name)
  @default_template = name
end

#defer!Object

Defers this layout. Meaning that if there is another layout already in the environment That should be used and this one should do nothing.



67
68
69
# File 'lib/wrapt/wrapt.rb', line 67

def defer!
  @defer = true
end

#defered?Boolean

Checks to see if this mdidleware should be defered

Returns:

  • (Boolean)


73
74
75
# File 'lib/wrapt/wrapt.rb', line 73

def defered?
  !!@defer
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:



86
87
88
# File 'lib/wrapt/wrapt.rb', line 86

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:



105
106
107
108
# File 'lib/wrapt/wrapt.rb', line 105

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



132
133
134
135
136
137
138
139
140
# File 'lib/wrapt/wrapt.rb', line 132

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



125
126
127
# File 'lib/wrapt/wrapt.rb', line 125

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



54
55
56
# File 'lib/wrapt/wrapt.rb', line 54

def master!
  @master = true
end

#master?Boolean

Checks to see if this layouter is a master

Returns:

  • (Boolean)


60
61
62
# File 'lib/wrapt/wrapt.rb', line 60

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/wrapt/wrapt.rb', line 183

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