nestive 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.yardopts +8 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +78 -0
- data/MIT-LICENSE +20 -0
- data/README.md +199 -0
- data/Rakefile +27 -0
- data/app/helpers/nestive/layout_helper.rb +232 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/nestive.rb +4 -0
- data/lib/nestive/engine.rb +6 -0
- data/lib/nestive/railtie.rb +11 -0
- data/lib/nestive/version.rb +3 -0
- data/nestive.gemspec +23 -0
- data/test/nestive_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- data/uninstall.rb +1 -0
- metadata +100 -0
data/.gitignore
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
nestive (0.0.1.pre)
|
5
|
+
rails (~> 3.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
abstract (1.0.0)
|
11
|
+
actionmailer (3.0.7)
|
12
|
+
actionpack (= 3.0.7)
|
13
|
+
mail (~> 2.2.15)
|
14
|
+
actionpack (3.0.7)
|
15
|
+
activemodel (= 3.0.7)
|
16
|
+
activesupport (= 3.0.7)
|
17
|
+
builder (~> 2.1.2)
|
18
|
+
erubis (~> 2.6.6)
|
19
|
+
i18n (~> 0.5.0)
|
20
|
+
rack (~> 1.2.1)
|
21
|
+
rack-mount (~> 0.6.14)
|
22
|
+
rack-test (~> 0.5.7)
|
23
|
+
tzinfo (~> 0.3.23)
|
24
|
+
activemodel (3.0.7)
|
25
|
+
activesupport (= 3.0.7)
|
26
|
+
builder (~> 2.1.2)
|
27
|
+
i18n (~> 0.5.0)
|
28
|
+
activerecord (3.0.7)
|
29
|
+
activemodel (= 3.0.7)
|
30
|
+
activesupport (= 3.0.7)
|
31
|
+
arel (~> 2.0.2)
|
32
|
+
tzinfo (~> 0.3.23)
|
33
|
+
activeresource (3.0.7)
|
34
|
+
activemodel (= 3.0.7)
|
35
|
+
activesupport (= 3.0.7)
|
36
|
+
activesupport (3.0.7)
|
37
|
+
arel (2.0.10)
|
38
|
+
builder (2.1.2)
|
39
|
+
erubis (2.6.6)
|
40
|
+
abstract (>= 1.0.0)
|
41
|
+
i18n (0.5.0)
|
42
|
+
mail (2.2.19)
|
43
|
+
activesupport (>= 2.3.6)
|
44
|
+
i18n (>= 0.4.0)
|
45
|
+
mime-types (~> 1.16)
|
46
|
+
treetop (~> 1.4.8)
|
47
|
+
mime-types (1.16)
|
48
|
+
polyglot (0.3.1)
|
49
|
+
rack (1.2.3)
|
50
|
+
rack-mount (0.6.14)
|
51
|
+
rack (>= 1.0.0)
|
52
|
+
rack-test (0.5.7)
|
53
|
+
rack (>= 1.0)
|
54
|
+
rails (3.0.7)
|
55
|
+
actionmailer (= 3.0.7)
|
56
|
+
actionpack (= 3.0.7)
|
57
|
+
activerecord (= 3.0.7)
|
58
|
+
activeresource (= 3.0.7)
|
59
|
+
activesupport (= 3.0.7)
|
60
|
+
bundler (~> 1.0)
|
61
|
+
railties (= 3.0.7)
|
62
|
+
railties (3.0.7)
|
63
|
+
actionpack (= 3.0.7)
|
64
|
+
activesupport (= 3.0.7)
|
65
|
+
rake (>= 0.8.7)
|
66
|
+
thor (~> 0.14.4)
|
67
|
+
rake (0.9.0)
|
68
|
+
thor (0.14.6)
|
69
|
+
treetop (1.4.9)
|
70
|
+
polyglot (>= 0.3.1)
|
71
|
+
tzinfo (0.3.27)
|
72
|
+
|
73
|
+
PLATFORMS
|
74
|
+
ruby
|
75
|
+
|
76
|
+
DEPENDENCIES
|
77
|
+
nestive!
|
78
|
+
rails (~> 3.0)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Justin French
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,199 @@
|
|
1
|
+
# Nestive, A Nested Inheritable Layouts Plugin for Rails
|
2
|
+
|
3
|
+
**Note: This is ridiculously alpha proof-of-concept seeking feedback. Things will change.**
|
4
|
+
|
5
|
+
Nestive adds powerful layout and view helpers to your Rails app. It's similar to the nested layout technique [already documented in the Rails guides](http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts) and found in many other nested layout plugins (a technique using `content_for` and rendering the parent layout at the end of the child layout). There's a bunch of problems with this technique, including:
|
6
|
+
|
7
|
+
* you can only *append* content to the content buffer with `content_for` (you can't prepend to content, you can't replace it)
|
8
|
+
* when combined with this nested layout technique, `content_for` actually *prepends* new content to the buffer, because each parent layout is rendered *after* it's child
|
9
|
+
|
10
|
+
Nestive is *better* because it addresses these problems.
|
11
|
+
|
12
|
+
## Just five methods (so far) – `area`, `extend`, `append`, `prepend`, `replace`.
|
13
|
+
|
14
|
+
### Declaring an area of content in your parent layout with `area`:
|
15
|
+
|
16
|
+
The `area` helper is a lot like Rails' own `<%= yield :foo %>`, and is used in layouts to define and render a chunk of content in your layout:
|
17
|
+
|
18
|
+
<%= area :sidebar %>
|
19
|
+
|
20
|
+
Unlike `yield`, `area` will allow your parent layouts to add content to the area at the same time using either a String or a block:
|
21
|
+
|
22
|
+
<%= area :sidebar, "Some Content Here" %>
|
23
|
+
|
24
|
+
<%= area :sidebar do %>
|
25
|
+
Some Content Here
|
26
|
+
<% end %>
|
27
|
+
|
28
|
+
It's important to note that this isn't *default* content, it *is* the content (unless a child changes it).
|
29
|
+
|
30
|
+
### Extending a layout in a child layout (or view):
|
31
|
+
|
32
|
+
Any layout (or view) can declare that it wants to inherit from and extend a parent layout, in this case we're extending `app/views/layouts/application.html.erb`:
|
33
|
+
|
34
|
+
<%= extends :application do %>
|
35
|
+
...
|
36
|
+
<% end %>
|
37
|
+
|
38
|
+
You can nest many levels deep:
|
39
|
+
|
40
|
+
# app/views/posts/index.html.erb
|
41
|
+
<%= extends :blog do %>
|
42
|
+
...
|
43
|
+
<% end %>
|
44
|
+
|
45
|
+
# app/views/layouts/blog.html.erb
|
46
|
+
<%= extends :public do %>
|
47
|
+
...
|
48
|
+
<% end %>
|
49
|
+
|
50
|
+
# app/views/layouts/public.html.erb
|
51
|
+
<%= extends :application do %>
|
52
|
+
...
|
53
|
+
<% end %>
|
54
|
+
|
55
|
+
### Appending content to an area:
|
56
|
+
|
57
|
+
The implementation details are quite different, but the `append` helper works much like Rails' built-in `content_for`. It will work with either a String or block, adding the new content onto the end of any content previously provided by parent layouts:
|
58
|
+
|
59
|
+
<%= extends :application do %>
|
60
|
+
<%= append :sidebar, "More content." %>
|
61
|
+
<%= append :sidebar do %>
|
62
|
+
More content.
|
63
|
+
<% end %>
|
64
|
+
<% end %>
|
65
|
+
|
66
|
+
### Prepending content to an area:
|
67
|
+
|
68
|
+
Exactly what you think it is. The reverse of `append` (duh), adding the new content at the start of any content previously provided by parent layouts:
|
69
|
+
|
70
|
+
<%= extends :application do %>
|
71
|
+
<%= prepend :sidebar, "Content." %>
|
72
|
+
<%= prepend :sidebar do %>
|
73
|
+
Content.
|
74
|
+
<% end %>
|
75
|
+
<% end %>
|
76
|
+
|
77
|
+
### Replacing content
|
78
|
+
|
79
|
+
You can also replace any content provided by parent layouts:
|
80
|
+
|
81
|
+
<%= extends :application do %>
|
82
|
+
<%= replace :sidebar, "New content." %>
|
83
|
+
<%= replace :sidebar do %>
|
84
|
+
New content.
|
85
|
+
<% end %>
|
86
|
+
<% end %>
|
87
|
+
|
88
|
+
|
89
|
+
## The token blog example
|
90
|
+
|
91
|
+
Set-up a global layout defining some content areas. Note that there is no `<% yield %>` here.
|
92
|
+
|
93
|
+
# app/views/layouts/application.html.erb
|
94
|
+
<!DOCTYPE html>
|
95
|
+
<html>
|
96
|
+
<head>
|
97
|
+
<meta charset="utf-8">
|
98
|
+
<title><%= area :title %> JustinFrench.com</title>
|
99
|
+
<meta name="description" content="<%= area :description, "This is my website." %>">
|
100
|
+
<meta name="keywords" content="<%= area :keywords, "justin, french, ruby, design" %>">
|
101
|
+
</head>
|
102
|
+
<body>
|
103
|
+
<div id="wrapper">
|
104
|
+
<div id="content">
|
105
|
+
<%= area :content do %>
|
106
|
+
<p>Default content goes here.</p>
|
107
|
+
<% end %>
|
108
|
+
</div>
|
109
|
+
<div id="sidebar">
|
110
|
+
<%= area :sidebar do %>
|
111
|
+
<h2>About Me</h2>
|
112
|
+
<p>...</p>
|
113
|
+
<% end %>
|
114
|
+
</div>
|
115
|
+
</div>
|
116
|
+
</body>
|
117
|
+
</html>
|
118
|
+
|
119
|
+
Next, we set-up a `blog` layout that extends `application`, replacing, appending & prepending content to the areas we defined earlier.
|
120
|
+
|
121
|
+
# app/views/layouts/blog.html.erb
|
122
|
+
<%= extends :application do %>
|
123
|
+
<% replace :title, "My Blog – " %>
|
124
|
+
<% replace :description, "Justin French blogs here on Ruby, Rails, Design, Formtastic, etc" %>
|
125
|
+
<% prepend :keywords, "blog, weblog, design links, ruby links, formtastic release notes, " %>
|
126
|
+
<% end %>
|
127
|
+
|
128
|
+
Now our blog index view can extend `blog` and fill in the areas with content specific to the index action.
|
129
|
+
|
130
|
+
# app/views/posts/index.html.erb
|
131
|
+
<%= extends :blog do %>
|
132
|
+
<% replace :content do %>
|
133
|
+
<h1>My Blog</h1>
|
134
|
+
<% render @articles %>
|
135
|
+
<% end %>
|
136
|
+
|
137
|
+
<% append :content do %>
|
138
|
+
<h2>Blog Roll</h2>
|
139
|
+
<% render @links %>
|
140
|
+
<% end %>
|
141
|
+
<% end %>
|
142
|
+
|
143
|
+
We also need to instruct the `PostsController` not to wrap the view in a layout of it's own (default Rails behavior), which can be done on an individual action:
|
144
|
+
|
145
|
+
# app/controllers/posts_controller.rb
|
146
|
+
class PostsController < ApplicationController
|
147
|
+
def index
|
148
|
+
render :layout => nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
Or for an entire controller:
|
153
|
+
|
154
|
+
# app/controllers/posts_controller.rb
|
155
|
+
class PostsController < ApplicationController
|
156
|
+
layout nil
|
157
|
+
end
|
158
|
+
|
159
|
+
Or for every controller:
|
160
|
+
|
161
|
+
# app/controllers/application_controller.rb
|
162
|
+
class ApplicationController < ActionController::Base
|
163
|
+
layout nil
|
164
|
+
end
|
165
|
+
|
166
|
+
We'll find a way to make this easier or a bit more obvious in a future version.
|
167
|
+
|
168
|
+
|
169
|
+
## Installation
|
170
|
+
|
171
|
+
* add `gem 'nestive', '~> 0.1'` to your gemfile
|
172
|
+
* run `bundle`
|
173
|
+
* add `layout nil` to ApplicationController or the specific controllers you want to use Nestive on (see above)
|
174
|
+
|
175
|
+
|
176
|
+
## TODO
|
177
|
+
|
178
|
+
* Figure out how to test it
|
179
|
+
* Actually use it in an app
|
180
|
+
* You know, everything!
|
181
|
+
|
182
|
+
|
183
|
+
## Compatibility
|
184
|
+
|
185
|
+
Only testing it with Rails 3.1 RCs right now, but it should work with Rails 2 & 3. The dependency is set to ~> 3.0 right now, will change to 2.x when someone can test it works.
|
186
|
+
|
187
|
+
*Nestive doesn't monkey patch or fiddle with any default behaviors in Rails.* Use it when you want to, don't when you don't.
|
188
|
+
|
189
|
+
## You can help with...
|
190
|
+
|
191
|
+
* feedback
|
192
|
+
* reporting issues
|
193
|
+
* fixing issues with pull requests
|
194
|
+
* performance testing
|
195
|
+
|
196
|
+
## Twitter
|
197
|
+
|
198
|
+
* [@nestivegem](http://twitter.com/nestivegem)
|
199
|
+
* [@justinfrench](http://twitter.com/justinfrench)
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
require 'bundler'
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
|
9
|
+
desc 'Default: run unit tests.'
|
10
|
+
task :default => :test
|
11
|
+
|
12
|
+
desc 'Test the nestive plugin.'
|
13
|
+
Rake::TestTask.new(:test) do |t|
|
14
|
+
t.libs << 'lib'
|
15
|
+
t.libs << 'test'
|
16
|
+
t.pattern = 'test/**/*_test.rb'
|
17
|
+
t.verbose = true
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Generate documentation for the nestive plugin.'
|
21
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
22
|
+
rdoc.rdoc_dir = 'rdoc'
|
23
|
+
rdoc.title = 'Nestive'
|
24
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
25
|
+
rdoc.rdoc_files.include('README')
|
26
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
27
|
+
end
|
@@ -0,0 +1,232 @@
|
|
1
|
+
module Nestive
|
2
|
+
|
3
|
+
# The Nestive LayoutHelper provides a handful of helper methods for use in your layouts and views.
|
4
|
+
#
|
5
|
+
# See the documentation for each individual method for detailed information, but at a high level,
|
6
|
+
# your parent layouts define `area`s of content. You can define an area and optionally add content
|
7
|
+
# to it at the same time using either a String, or a block:
|
8
|
+
#
|
9
|
+
# # app/views/layouts/global.html.erb
|
10
|
+
# <html>
|
11
|
+
# <head>
|
12
|
+
# <title><%= area :title, "MySite.com" %></title>
|
13
|
+
# </head>
|
14
|
+
# <body>
|
15
|
+
# <div id="content">
|
16
|
+
# <%= area :content %>
|
17
|
+
# </div>
|
18
|
+
# <div id="sidebar">
|
19
|
+
# <%= area :sidebar do %>
|
20
|
+
# <h2>About MySite.com</h2>
|
21
|
+
# <p>...</p>
|
22
|
+
# <% end %>
|
23
|
+
# </div>
|
24
|
+
# </body>
|
25
|
+
# </html>
|
26
|
+
#
|
27
|
+
# Your child layouts (or views) inherit and modify the parent by wrapping in an `extend` block
|
28
|
+
# helper. You can then either `append`, `prepend` or `replace` the content that has previously
|
29
|
+
# been assigned to each area by parent layouts.
|
30
|
+
#
|
31
|
+
# The `append`, `prepend` or `replace` helpers are *similar* to Rails' own `content_for`, which
|
32
|
+
# accepts content for the named area with either a String or with a block). They're different to
|
33
|
+
# `content_for` because they're only used modify the content assigned to the area, not retrieve it:
|
34
|
+
#
|
35
|
+
# # app/views/layouts/admin.html.erb
|
36
|
+
# <%= extends :global do %>
|
37
|
+
# <% prepend :title, "Admin :: " %>
|
38
|
+
# <% replace :sidebar do %>
|
39
|
+
# <h2>Quick Links</h2>
|
40
|
+
# <ul>
|
41
|
+
# <li>...</li>
|
42
|
+
# </ul>
|
43
|
+
# <% end %>
|
44
|
+
# <% end %>
|
45
|
+
#
|
46
|
+
# # app/views/admin/posts/index.html.erb
|
47
|
+
# <%= extends :admin do %>
|
48
|
+
# <% prepend :title, "Posts ::" %>
|
49
|
+
# <% replace :content do %>
|
50
|
+
# Normal view stuff goes here.
|
51
|
+
# <% end %>
|
52
|
+
# <% end %>
|
53
|
+
module LayoutHelper
|
54
|
+
|
55
|
+
# Declares that the current layour (or view) is inheriting from and extending another layout.
|
56
|
+
#
|
57
|
+
# @param [Symbol] name
|
58
|
+
# The base name of the file in `layouts/` that you wish to extend (eg `:application` for `layouts/application.html.erb`)
|
59
|
+
#
|
60
|
+
# @example Extending the `application` layout to create an `admin` layout
|
61
|
+
#
|
62
|
+
# # app/views/layouts/admin.html.erb
|
63
|
+
# <%= extends :application do %>
|
64
|
+
# ...
|
65
|
+
# <% end %>
|
66
|
+
#
|
67
|
+
# @example Extending the `admin` layout in a view (you'll need to render the view with `:layout => nil`)
|
68
|
+
#
|
69
|
+
# # app/controllers/admin/posts_controller.rb
|
70
|
+
# class Admin::PostsController < ApplicationController
|
71
|
+
# # You can disable Rails' layout rendering for all actions
|
72
|
+
# layout nil
|
73
|
+
#
|
74
|
+
# # Or disable Rails' layout rendering per-controller
|
75
|
+
# def index
|
76
|
+
# render :layout => nil
|
77
|
+
# end
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# # app/views/admin/posts/index.html.erb
|
81
|
+
# <%= extends :admin do %>
|
82
|
+
# ...
|
83
|
+
# <% end %>
|
84
|
+
def extends(name, &block)
|
85
|
+
capture(&block)
|
86
|
+
render(:file => "layouts/#{name}")
|
87
|
+
end
|
88
|
+
|
89
|
+
# Defines an area of content in your layout that can be modified or replaced by child layouts
|
90
|
+
# that extend it. You can optionally add content to an area using either a String, or a block.
|
91
|
+
#
|
92
|
+
# Areas are declared in a parent layout and modified by a child layout, but since Nestive
|
93
|
+
# allows for multiple levels of inheritance, a child layout can also declare an area for it's
|
94
|
+
# children to modify.
|
95
|
+
#
|
96
|
+
# @example Define an area without adding content to it:
|
97
|
+
# <%= area :sidebar %>
|
98
|
+
#
|
99
|
+
# @example Define an area and add a String of content to it:
|
100
|
+
# <%= area :sidebar, "Some content." %>
|
101
|
+
#
|
102
|
+
# @example Define an area and add content to it with a block:
|
103
|
+
# <%= area :sidebar do %>
|
104
|
+
# Some content.
|
105
|
+
# <% end %>
|
106
|
+
#
|
107
|
+
# @example Define an area in a child layout:
|
108
|
+
# <%= extends :global do %>
|
109
|
+
# <%= area :sidebar do %>
|
110
|
+
# Some content.
|
111
|
+
# <% end %>
|
112
|
+
# <% end %>
|
113
|
+
#
|
114
|
+
# @param [Symbol] name
|
115
|
+
# A unique name to identify this area of content.
|
116
|
+
#
|
117
|
+
# @param [String] content
|
118
|
+
# An optional String of content to add to the area as you declare it.
|
119
|
+
def area(name, content=nil, &block)
|
120
|
+
content = capture(&block) if block_given?
|
121
|
+
append(name, content)
|
122
|
+
render_area(name)
|
123
|
+
end
|
124
|
+
|
125
|
+
def block(name, content=nil, &block)
|
126
|
+
ActiveSupport::Deprecation.warn("block() is deprecated and will be removed very soon, please use area() instead")
|
127
|
+
area(name, content, &block)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Appends content to an area previously defined or modified in parent layout(s). You can provide
|
131
|
+
# the content using either a String, or a block.
|
132
|
+
#
|
133
|
+
# @example Appending content with a String
|
134
|
+
# <% append :sidebar, "Some content." %>
|
135
|
+
#
|
136
|
+
# @example Appending content with a block:
|
137
|
+
# <% append :sidebar do %>
|
138
|
+
# Some content.
|
139
|
+
# <% end %>
|
140
|
+
#
|
141
|
+
# @param [Symbol] name
|
142
|
+
# A name to identify the area of content you wish to append to
|
143
|
+
#
|
144
|
+
# @param [String] content
|
145
|
+
# Optionally provide a String of content, instead of a block. A block will take precedence.
|
146
|
+
def append(name, content=nil, &block)
|
147
|
+
content = capture(&block) if block_given?
|
148
|
+
add_instruction_to_area(name, :push, content)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Prepends content to an area previously declared or modified in parent layout(s). You can
|
152
|
+
# provide the content using either a String, or a block.
|
153
|
+
#
|
154
|
+
# @example Prepending content with a String
|
155
|
+
# <% prepend :sidebar, "Some content." %>
|
156
|
+
#
|
157
|
+
# @example Prepending content with a block:
|
158
|
+
# <% prepend :sidebar do %>
|
159
|
+
# Some content.
|
160
|
+
# <% end %>
|
161
|
+
#
|
162
|
+
# @param [Symbol] name
|
163
|
+
# A name to identify the area of content you wish to prepend to
|
164
|
+
#
|
165
|
+
# @param [String] content
|
166
|
+
# Optionally provide a String of content, instead of a block. A block will take precedence.
|
167
|
+
def prepend(name, content=nil, &block)
|
168
|
+
content = capture(&block) if block_given?
|
169
|
+
add_instruction_to_area(name, :unshift, content)
|
170
|
+
end
|
171
|
+
|
172
|
+
# Replaces the content of an area previously declared or modified in parent layout(s). You can
|
173
|
+
# provide the content using either a String, or a block.
|
174
|
+
#
|
175
|
+
# @example Prepending content with a String
|
176
|
+
# <% replace :sidebar, "New content." %>
|
177
|
+
#
|
178
|
+
# @example Prepending content with a block:
|
179
|
+
# <% replace :sidebar do %>
|
180
|
+
# New content.
|
181
|
+
# <% end %>
|
182
|
+
#
|
183
|
+
# @param [Symbol] name
|
184
|
+
# A name to identify the area of content you wish to replace
|
185
|
+
#
|
186
|
+
# @param [String] content
|
187
|
+
# Optionally provide a String of content, instead of a block. A block will take precedence.
|
188
|
+
def replace(name, content=nil, &block)
|
189
|
+
content = capture(&block) if block_given?
|
190
|
+
add_instruction_to_area(name, :replace, [content])
|
191
|
+
end
|
192
|
+
|
193
|
+
private
|
194
|
+
|
195
|
+
# We record the instructions (declaring, appending, prepending and replacing) for an area of
|
196
|
+
# content into an array that we can later retrieve and replay. Instructions are stored in an
|
197
|
+
# instance variable Hash `@_area_for`, with each key representing an area name, and each value
|
198
|
+
# an Array of instructions. Each instruction is a two element array containing a instruction
|
199
|
+
# method (eg `:push`, `:unshift`, `:replace`) and a value (content String).
|
200
|
+
#
|
201
|
+
# @_area_for[:sidebar] # => [ [:push,"World"], [:unshift,"Hello"] ]
|
202
|
+
#
|
203
|
+
# Due to the way we extend layouts (render the parent layout after the child), the instructions
|
204
|
+
# are captured in reverse order. `render_area` reversed them and plays them back at rendering
|
205
|
+
# time.
|
206
|
+
#
|
207
|
+
# @example
|
208
|
+
# add_instruction_to_area(:sidebar, :push, "More content.")
|
209
|
+
def add_instruction_to_area(name, instruction, value)
|
210
|
+
@_area_for ||= {}
|
211
|
+
@_area_for[name] ||= []
|
212
|
+
@_area_for[name] << [instruction, value]
|
213
|
+
end
|
214
|
+
|
215
|
+
# Take the instructions we've gathered for the area and replay them one after the other on
|
216
|
+
# an empty array. These instructions will push, unshift or replace items into our output array,
|
217
|
+
# which we then join and mark as html_safe.
|
218
|
+
#
|
219
|
+
# These instructions are reversed and replayed when we render the block (rather than as they
|
220
|
+
# happen) due to the way they are gathered by the layout extension process (in reverse).
|
221
|
+
#
|
222
|
+
# @todo is `html_safe` "safe" here?
|
223
|
+
def render_area(name)
|
224
|
+
output = []
|
225
|
+
(@_area_for[name] || []).reverse.each do |i|
|
226
|
+
output.send(i.first, i.last)
|
227
|
+
end
|
228
|
+
output.join.html_safe
|
229
|
+
end
|
230
|
+
|
231
|
+
end
|
232
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Include hook code here
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/nestive.rb
ADDED
data/nestive.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "nestive/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "nestive"
|
7
|
+
s.version = Nestive::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Justin French"]
|
10
|
+
s.email = ["justin@indent.com.au"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A Rails plugin/gem for awesome nested templates and layouts}
|
13
|
+
s.description = %q{A Rails plugin/gem for awesome nested templates and layouts}
|
14
|
+
|
15
|
+
s.rubyforge_project = "nestive"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_dependency(%q<rails>, ["~> 3.0"])
|
23
|
+
end
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nestive
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Justin French
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-30 00:00:00 +10:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rails
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
version: "3.0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: A Rails plugin/gem for awesome nested templates and layouts
|
37
|
+
email:
|
38
|
+
- justin@indent.com.au
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- .yardopts
|
48
|
+
- Gemfile
|
49
|
+
- Gemfile.lock
|
50
|
+
- MIT-LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- app/helpers/nestive/layout_helper.rb
|
54
|
+
- init.rb
|
55
|
+
- install.rb
|
56
|
+
- lib/nestive.rb
|
57
|
+
- lib/nestive/engine.rb
|
58
|
+
- lib/nestive/railtie.rb
|
59
|
+
- lib/nestive/version.rb
|
60
|
+
- nestive.gemspec
|
61
|
+
- test/nestive_test.rb
|
62
|
+
- test/test_helper.rb
|
63
|
+
- uninstall.rb
|
64
|
+
has_rdoc: true
|
65
|
+
homepage: ""
|
66
|
+
licenses: []
|
67
|
+
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
hash: 3
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
requirements: []
|
92
|
+
|
93
|
+
rubyforge_project: nestive
|
94
|
+
rubygems_version: 1.3.7
|
95
|
+
signing_key:
|
96
|
+
specification_version: 3
|
97
|
+
summary: A Rails plugin/gem for awesome nested templates and layouts
|
98
|
+
test_files:
|
99
|
+
- test/nestive_test.rb
|
100
|
+
- test/test_helper.rb
|