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=



37
38
39
# File 'lib/wrapt/layout.rb', line 37

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=



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

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



120
121
122
# File 'lib/wrapt/layout.rb', line 120

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


114
115
116
# File 'lib/wrapt/layout.rb', line 114

def content_for
  @content_for
end

#dupObject



75
76
77
78
79
# File 'lib/wrapt/layout.rb', line 75

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

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

The interface for rack.

Yields:

  • (result)


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

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)



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

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:



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

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.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/wrapt/layout.rb', line 145

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(env)) 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:



88
89
90
91
92
93
94
# File 'lib/wrapt/layout.rb', line 88

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

  content_for[label] = content
end

#template_name?(name, opts = {}) ⇒ Boolean

Check to see if there is a template of a given name and options for this layouter.

Returns:

  • (Boolean)

See Also:



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

def template_name?(name, opts = {})
  !!@wrapt.template(name, opts)
end

#to_sObject

An easy method to get the wrapped results



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

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



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

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