Class: Wrapt::Layout

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wrapt/layout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapt, env) ⇒ Layout

Returns a new instance of Layout.



7
8
9
10
11
12
13
# File 'lib/wrapt/layout.rb', line 7

def initialize(wrapt, env)
  @env = env
  @wrapt = wrapt
  @request = Rack::Request.new(@env)
  @content_for = Hashie::Mash.new
  @ignore_layout = nil
end

Instance Attribute Details

#envObject

Returns the value of attribute env.



4
5
6
# File 'lib/wrapt/layout.rb', line 4

def env
  @env
end

#formatObject

Gets the format that this layouter has been set to. Defaults to the default_format of the wrapt instance

May be set with Wrapt::Layout#format=



27
28
29
# File 'lib/wrapt/layout.rb', line 27

def format
  @format
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/wrapt/layout.rb', line 5

def request
  @request
end

#template_nameObject

Gets the template name that this layouter has. Defaults to the wrapts instances default_template

May be set with Wrapt::Layout#default_template=



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

def template_name
  @template_name
end

#wraptObject

Returns the value of attribute wrapt.



4
5
6
# File 'lib/wrapt/layout.rb', line 4

def wrapt
  @wrapt
end

Instance Method Details

#content=(content) ⇒ Object

Set the main content for the layout



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

def content=(content)
  set_content_for(:content, content)
end

#content_forObject

Provides access to the content_for hash. Content may be accessed for concatination etc

When using content_for you can provide different contents for you layouts. The default content label is :content

Examples:

# In your application
layout = env['layout']
layout.set_content_for(:foo, "Foo Content")
layout.content = "Normal Content"

# In the layout
<%= yield %>          <-- inserts the content labeled :content
<%= yield :content %> <-- insert the content labeled :content
<%= yield :foo %>     <-- insert the content labled :foo


104
105
106
# File 'lib/wrapt/layout.rb', line 104

def content_for
  @content_for
end

#dupObject



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

def dup
  dupped = super
  dupped.instance_variable_set('@content_for', Hashie::Mash.new)
  dupped
end

#each {|result| ... } ⇒ Object

The interface for rack.

Yields:

  • (result)


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wrapt/layout.rb', line 122

def each
  ignore = if @ignore_layout
    @ignore_layout.call(env)
  else
    @wrapt.ignore_layout?(env)
  end

  result = render_layout(ignore)
  yield result
  result
end

#ignore_layout(&block) ⇒ Object

allows you to set the ignore_layout block for just this request (and anythin downstream)



33
34
35
# File 'lib/wrapt/layout.rb', line 33

def ignore_layout(&block)
  @ignore_layout = block
end

#master?Boolean

Is the wrapt instance that created this layouter set as a master?

Returns:

  • (Boolean)

See Also:



17
18
19
# File 'lib/wrapt/layout.rb', line 17

def master?
  @wrapt.master?
end

#render_layout(ignore_layout = false) ⇒ Object

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.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/wrapt/layout.rb', line 135

def render_layout(ignore_layout = false)
  return content_for[:content] if ignore_layout

  opts = {}
  opts[:format]   ||= format
  template = template_name

  template = @wrapt.template(template, opts)

  output = if template
    template.render(LayoutContext.new) do |*args|
      label = args.first || :content
      content_for[label]
    end
  else
    content_for[:content]
  end
  output
end

#set_content_for(label = :content, content = nil) ⇒ Object

Set the content for the layout for the given label

Parameters:

  • label (Symbol) (defaults to: :content)

    The label to identify the content to insert

  • content (String) (defaults to: nil)

    The content to set for this label

See Also:



78
79
80
81
82
83
84
# File 'lib/wrapt/layout.rb', line 78

def set_content_for(label = :content, content = nil)
  if block_given?
    content = block.call
  end

  content_for[label] = content
end

#to_sObject

An easy method to get the wrapped results



116
117
118
# File 'lib/wrapt/layout.rb', line 116

def to_s
  map.join
end

#wrap(content, opts = {}) ⇒ Object

Wraps the given content into the layout using the same base as the layouter currently has

Parameters:

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

    a customizable set of options

Options Hash (opts):

  • :layout (String|Symbol)

    the name of a layout template to use

  • :format (String|Symbol)

    the format of the layout template to use



55
56
57
58
59
60
61
62
63
# File 'lib/wrapt/layout.rb', line 55

def wrap(content, opts={})
  layout = self.dup

  layout.format         = opts[:format] if opts[:format]
  layout.template_name  = opts[:layout] if opts[:layout]

  layout.content = content
  layout.render_layout(false)
end