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.
-
#template_name?(name, opts = {}) ⇒ Boolean
Check to see if there is a template of a given name and options for this layouter.
-
#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=
37 38 39 |
# File 'lib/wrapt/layout.rb', line 37 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=
53 54 55 |
# File 'lib/wrapt/layout.rb', line 53 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
120 121 122 |
# File 'lib/wrapt/layout.rb', line 120 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
114 115 116 |
# File 'lib/wrapt/layout.rb', line 114 def content_for @content_for end |
#dup ⇒ Object
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.
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?
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
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.
27 28 29 |
# File 'lib/wrapt/layout.rb', line 27 def template_name?(name, opts = {}) !!@wrapt.template(name, opts) end |
#to_s ⇒ Object
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
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 |