Class: Wrapt::Layout
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
-
#format ⇒ Object
Gets the format that this layouter has been set to.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#template_name ⇒ Object
Gets the template name that this layouter has.
-
#wrapt ⇒ Object
Returns the value of attribute wrapt.
Instance Method Summary collapse
-
#content=(content) ⇒ Object
Set the main content for the layout.
-
#content_for ⇒ Object
Provides access to the content_for hash.
- #dup ⇒ Object
-
#each {|result| ... } ⇒ Object
The interface for rack.
-
#ignore_layout(&block) ⇒ Object
allows you to set the ignore_layout block for just this request (and anythin downstream).
-
#initialize(wrapt, env) ⇒ Layout
constructor
A new instance of Layout.
-
#master? ⇒ Boolean
Is the wrapt instance that created this layouter set as a master?.
- #render_layout(ignore_layout = false) ⇒ Object private
-
#set_content_for(label = :content, content = nil) ⇒ Object
Set the content for the layout for the given label.
-
#to_s ⇒ Object
An easy method to get the wrapped results.
-
#wrap(content, opts = {}) ⇒ Object
Wraps the given content into the layout using the same base as the layouter currently has.
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
#env ⇒ Object
Returns the value of attribute env.
4 5 6 |
# File 'lib/wrapt/layout.rb', line 4 def env @env end |
#format ⇒ Object
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 |
#request ⇒ Object (readonly)
Returns the value of attribute request.
5 6 7 |
# File 'lib/wrapt/layout.rb', line 5 def request @request end |
#template_name ⇒ Object
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 |
#wrapt ⇒ Object
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_for ⇒ Object
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
104 105 106 |
# File 'lib/wrapt/layout.rb', line 104 def content_for @content_for end |
#dup ⇒ Object
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.
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?
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
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_s ⇒ Object
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
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 |