Module: NestiveRendering::LayoutHelper
- Defined in:
- lib/nestive_rendering/layout_helper.rb
Overview
The Nestive Rendering LayoutHelper provides a handful of helper methods for use in your layouts and views.
See the documentation for each individual method for detailed information, but at a high level, your parent layouts define ‘area`s of content. You can define an area and optionally add content to it at the same time using either a String, or a block:
# app/views/layouts/global.html.erb
<html>
<head>
<title><%= area :title, "MySite.com" %></title>
</head>
<body>
<div id="content">
<%= area :content %>
</div>
<div id="sidebar">
<%= area :sidebar do %>
<h2>About MySite.com</h2>
<p>...</p>
<% end %>
</div>
</body>
</html>
Your child layouts (or views) inherit and modify the parent by wrapping in an ‘extends` block helper. You can then either `append`, `prepend` or `replace` the content that has previously been assigned to each area by parent layouts.
The ‘append`, `prepend` or `replace` helpers are similar to Rails’ own ‘content_for`, which accepts content for the named area with either a String or with a block). They’re different to ‘content_for` because they’re only used modify the content assigned to the area, not retrieve it:
# app/views/layouts/admin.html.erb
<%= extends :global do %>
<% prepend :title, "Admin :: " %>
<% replace :sidebar do %>
<h2>Quick Links</h2>
<ul>
<li>...</li>
</ul>
<% end %>
<% end %>
# app/views/admin/posts/index.html.erb
<%= extends :admin do %>
<% prepend :title, "Posts ::" %>
<% replace :content do %>
Normal view stuff goes here.
<% end %>
<% end %>
Instance Method Summary collapse
-
#append(name, content = nil, &block) ⇒ Object
Appends content to an area previously defined or modified in parent layout(s).
-
#area(name, content = nil, &block) ⇒ Object
Defines an area of content in your layout that can be modified or replaced by child layouts that extend it.
-
#extends(layout, options = {}, &block) ⇒ Object
Declares that the current layour (or view) is inheriting from and extending another layout.
-
#extends_partial(partial, options = {}, &block) ⇒ Object
Works exactly the same as extends but is targeted at extending partials not layouts.
-
#prepend(name, content = nil, &block) ⇒ Object
Prepends content to an area previously declared or modified in parent layout(s).
-
#purge(*names) ⇒ Object
Purge the content of an area previously declared or modified in parent layout(s).
-
#replace(name, content = nil, &block) ⇒ Object
Replaces the content of an area previously declared or modified in parent layout(s).
Instance Method Details
#append(name, content = nil, &block) ⇒ Object
Appends content to an area previously defined or modified in parent layout(s). You can provide the content using either a String, or a block.
166 167 168 169 |
# File 'lib/nestive_rendering/layout_helper.rb', line 166 def append(name, content=nil, &block) content = capture(&block) if block_given? add_instruction_to_area name, :push, content end |
#area(name, content = nil, &block) ⇒ Object
Defines an area of content in your layout that can be modified or replaced by child layouts that extend it. You can optionally add content to an area using either a String, or a block.
Areas are declared in a parent layout and modified by a child layout, but since Nestive Rendering allows for multiple levels of inheritance, a child layout can also declare an area for it’s children to modify.
144 145 146 147 148 |
# File 'lib/nestive_rendering/layout_helper.rb', line 144 def area(name, content=nil, &block) content = capture(&block) if block_given? append name, content render_area name end |
#extends(layout, options = {}, &block) ⇒ Object
Declares that the current layour (or view) is inheriting from and extending another layout.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/nestive_rendering/layout_helper.rb', line 86 def extends(layout, = {}, &block) # Make sure it's a string layout = layout.to_s # If there's no directory component, presume a plain layout name layout = "layouts/#{layout}" unless layout.include?('/') # Capture the content to be placed inside the extended layout @view_flow.get(:layout).replace capture(&block) render .merge(file: layout) end |
#extends_partial(partial, options = {}, &block) ⇒ Object
Works exactly the same as extends but is targeted at extending partials not layouts.
107 108 109 110 111 112 |
# File 'lib/nestive_rendering/layout_helper.rb', line 107 def extends_partial(partial, = {}, &block) # Make sure it's a string partial = partial.to_s block.call if block_given? render .merge(partial: partial) end |
#prepend(name, content = nil, &block) ⇒ Object
Prepends content to an area previously declared or modified in parent layout(s). You can provide the content using either a String, or a block.
187 188 189 190 |
# File 'lib/nestive_rendering/layout_helper.rb', line 187 def prepend(name, content=nil, &block) content = capture(&block) if block_given? add_instruction_to_area name, :unshift, content end |
#purge(*names) ⇒ Object
Purge the content of an area previously declared or modified in parent layout(s).
220 221 222 |
# File 'lib/nestive_rendering/layout_helper.rb', line 220 def purge(*names) names.each{ |name| replace(name, nil)} end |
#replace(name, content = nil, &block) ⇒ Object
Replaces the content of an area previously declared or modified in parent layout(s). You can provide the content using either a String, or a block.
208 209 210 211 |
# File 'lib/nestive_rendering/layout_helper.rb', line 208 def replace(name, content=nil, &block) content = capture(&block) if block_given? add_instruction_to_area name, :replace, [content] end |