nice_partials 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +89 -41
- data/lib/nice_partials/partial.rb +4 -0
- data/lib/nice_partials/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0a121209bad4d81ba1702d9b34f908e90da58b27f224d78714129654a2b4a94
|
4
|
+
data.tar.gz: ee098d947c35bcf4cdb29d5aeb123856d6df88970686c310290c056d30502fc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f22e3ff0b5e24645c4f91025a02b08c2d3b018877caec5203d501483242f654364038d8f3fd7913cda2b5bceb6dc180d4d8846bd242999799146c3a68a3e37f
|
7
|
+
data.tar.gz: 026a3b89e7471b8e45765abbe78ec9a86b62311a9f145bd740d046be80cb1236ee1363a7e9c662008394124c4b31db1a0c39d3ecfa0c9fdf6674315b4cf9bcbc
|
data/README.md
CHANGED
@@ -1,82 +1,130 @@
|
|
1
1
|
# nice_partials [![[version]](https://badge.fury.io/rb/nice_partials.svg)](https://badge.fury.io/rb/nice_partials) [![[travis]](https://travis-ci.org/andrewculver/nice_partials.svg)](https://travis-ci.org/andrewculver/nice_partials)
|
2
2
|
|
3
|
-
Nice Partials provides a light layer of magic on top of traditional Rails view partials to try and make them an even better fit for extracting and reusing components in your views.
|
3
|
+
Nice Partials provides a light layer of magic on top of traditional Rails view partials to try and make them an even better fit for extracting and reusing components in your views.
|
4
4
|
|
5
|
+
It allows your partials to define named content areas like this:
|
5
6
|
|
6
|
-
|
7
|
+
`app/views/partials/_card.html.erb`:
|
8
|
+
```html+erb
|
9
|
+
<div class="card">
|
10
|
+
<%= p.yield :image %>
|
11
|
+
<div class="card-body">
|
12
|
+
<h5 class="card-title"><%= title %></h5>
|
13
|
+
<p class="card-text">
|
14
|
+
<%= p.yield :body %>
|
15
|
+
</p>
|
16
|
+
</div>
|
17
|
+
</div>
|
18
|
+
```
|
7
19
|
|
8
|
-
|
9
|
-
- Less context switching. Your components are all just rendering in the standard view context.
|
10
|
-
- You don't have to upgrade your existing partials. You can still nest them in a Nice Partials content area.
|
11
|
-
- Less ceremony. You _can_ spin up a custom class to back your partial if you want to, but you don't have to by default, and we don't suggest it.
|
12
|
-
- Instead, skip the component class entirely! You can define appropriately scoped helpers right inline with your partial.
|
13
|
-
- It's still testable. There's no reason why these can't be as testable as ViewComponents.
|
20
|
+
These partials can still be utilized with a standard `render` call, but you can specify how to populate the content areas like so:
|
14
21
|
|
22
|
+
```html+erb
|
23
|
+
<%= render 'partials/card', title: 'Some Title' do |p| %>
|
24
|
+
<% p.content_for :body do %>
|
25
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
|
26
|
+
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
|
27
|
+
<strong>quis nostrud exercitation ullamco laboris</strong> nisi ut aliquip
|
28
|
+
ex ea commodo consequat.
|
29
|
+
<% end %>
|
15
30
|
|
16
|
-
|
31
|
+
<% p.content_for :image do %>
|
32
|
+
<%= image_tag image_path('example.jpg'), alt: 'An example image' %>
|
33
|
+
<% end %>
|
34
|
+
<% end %>
|
35
|
+
```
|
17
36
|
|
18
|
-
|
37
|
+
Nice Partials is a lightweight and hopefully more Rails-native alternative to [ViewComponent](http://viewcomponent.org). It aims to provide many of the same benefits as ViewComponent while requiring less ceremony. This specific approach originated with [Bullet Train](https://bullettrain.co)'s "Field Partials" and was later reimagined and completely reimplemented by Dom Christie.
|
19
38
|
|
20
|
-
```ruby
|
21
|
-
gem "nice_partials"
|
22
|
-
```
|
23
39
|
|
40
|
+
## Benefits of Nice Partials
|
24
41
|
|
25
|
-
|
42
|
+
Compared to something more heavy-handed, Nice Partials:
|
26
43
|
|
27
|
-
|
44
|
+
- is just regular Rails view partials like you're used to.
|
45
|
+
- reduces the friction when extracting components.
|
46
|
+
- only ends up in the specific partials you need its functionality in.
|
47
|
+
- reduces context switching.
|
48
|
+
- allows isolated helper logic alongside your partial view code.
|
49
|
+
- doesn't require any upgrades to existing partials for interoperability.
|
50
|
+
- are still testable!
|
28
51
|
|
29
|
-
We'll define an example Nice Partial in `app/views/partials/card`. We start by invoking Nice Partials like so:
|
30
52
|
|
31
|
-
|
32
|
-
<% yield p = np %>
|
33
|
-
```
|
53
|
+
## How does it work?
|
34
54
|
|
35
|
-
|
55
|
+
Nice Partials slightly extends the concept of [`content_for` blocks and `yield`](https://guides.rubyonrails.org/layouts_and_rendering.html#using-the-content-for-method) so they can be properly used to define and utilize "content areas" or "slots" in simple ERB partials.
|
36
56
|
|
37
|
-
|
57
|
+
### Can't I do the same thing without Nice Partials?
|
38
58
|
|
39
|
-
|
59
|
+
You can almost accomplish the same thing without Nice Partials, but in practice you end up having to flush the content buffers after using them, leading to something like this:
|
60
|
+
|
61
|
+
```html+erb
|
40
62
|
<div class="card">
|
41
|
-
<%=
|
63
|
+
<%= yield :image %>
|
64
|
+
<% content_for :image, flush: true do '' end %>
|
42
65
|
<div class="card-body">
|
43
66
|
<h5 class="card-title"><%= title %></h5>
|
44
67
|
<p class="card-text">
|
45
|
-
<%=
|
68
|
+
<%= yield :body %>
|
69
|
+
<% content_for :body, flush: true do '' end %>
|
46
70
|
</p>
|
47
71
|
</div>
|
48
72
|
</div>
|
49
73
|
```
|
50
74
|
|
51
|
-
|
75
|
+
Earlier iterations of Nice Partials aimed to simply clean up this syntax with helper methods like `flush_content_for`, but because the named content buffers were in a global namespace, it was also possible to accidentally create situations where two partials with a `:body` content area would end up interfering with each other, depending on the order they're nested and rendered.
|
52
76
|
|
53
|
-
|
77
|
+
Nice Partials resolves the last-mile issues with standard view partials and content buffers by introducing a small, generic object that helps transparently namespace your named content buffers. This same object can also be used to define helper methods specific to your partial that are isolated from the global helper namespace.
|
54
78
|
|
55
|
-
To use a Nice Partial, just render it like you would any other partial, but also pass a block that accepts a parameter:
|
56
79
|
|
57
|
-
|
58
|
-
<%= render 'partials/card', title: 'Some Title' do |p| %>
|
59
|
-
<% p.content_for :body %>
|
60
|
-
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
61
|
-
Ut enim ad minim veniam, <strong>quis nostrud exercitation ullamco laboris</strong> nisi ut aliquip ex ea commodo consequat.
|
62
|
-
<% end %>
|
80
|
+
## Setup
|
63
81
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
82
|
+
Add to your `Gemfile`:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
gem "nice_partials"
|
68
86
|
```
|
69
87
|
|
70
|
-
|
88
|
+
## Usage
|
89
|
+
|
90
|
+
### When to use Nice Partials
|
71
91
|
|
72
|
-
|
92
|
+
You only need to use Nice Partials when:
|
73
93
|
|
94
|
+
- you want to define multiple named content areas in your partial. If you don't have multiple named content areas in your partial, you could just pass your content into the partial using the standard block and `yield` approach.
|
95
|
+
|
96
|
+
- you want to specifically isolate your helper methods for a specific partial.
|
97
|
+
|
98
|
+
### Use Nice Partials in a partial
|
99
|
+
|
100
|
+
To invoke nice partials, start your partial file with the following:
|
101
|
+
|
102
|
+
```html+erb
|
103
|
+
<% yield p = np %>
|
74
104
|
```
|
105
|
+
|
106
|
+
Here's what is happening here:
|
107
|
+
|
108
|
+
- `yield` executes the block we receive when someone uses our partial.
|
109
|
+
- `np` fetches an instance of the generic class that helps isolate our content buffers and helper methods.
|
110
|
+
- `p = np` ensures we have a reference to that object in this partial.
|
111
|
+
- `yield p = np` ensures the developer using this partial also has a reference to that object, so they can define what goes in the various content buffers.
|
112
|
+
|
113
|
+
(This is, [as far as we know](https://github.com/bullet-train-co/nice_partials/issues/1), the minimum viable invocation.)
|
114
|
+
|
115
|
+
Once you've done this at the top of your partial file, you can then use `<%= p.yield :some_section %>` to render whatever content areas you want to be passed into your partial.
|
116
|
+
|
117
|
+
|
118
|
+
### Extra Credit: Defining and Using Well Isolated Helper Methods
|
119
|
+
|
120
|
+
To minimize the amount of pollution in the global helper namespace, you can use the shared context object to define helper methods specifically for your partials _within your partial_ like so:
|
121
|
+
|
122
|
+
```html+erb
|
75
123
|
<% p.helpers do
|
76
124
|
|
77
125
|
# references should be a link if the user can drill down, otherwise just a text label.
|
78
|
-
# (this method has access to the scope of the entire view context and all the other helpers that come with it.)
|
79
126
|
def reference_to(user)
|
127
|
+
# look! this method has access to the scope of the entire view context and all the other helpers that come with it!
|
80
128
|
if can? :show, user
|
81
129
|
link_to user.name, user
|
82
130
|
else
|
@@ -89,7 +137,7 @@ end %>
|
|
89
137
|
|
90
138
|
Then later in the partial you can use the helper method like so:
|
91
139
|
|
92
|
-
```
|
140
|
+
```html+erb
|
93
141
|
<td><%= p.reference_to(user) %></td>
|
94
142
|
```
|
95
143
|
|
@@ -17,5 +17,9 @@ module NicePartials
|
|
17
17
|
def content_for(name, content = nil, options = {}, &block)
|
18
18
|
@view_context.content_for("#{name}_#{@key}".to_sym, content, options, &block)
|
19
19
|
end
|
20
|
+
|
21
|
+
def content_for?(name)
|
22
|
+
@view_context.content_for?("#{name}_#{@key}".to_sym)
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|