blocks 3.0.0.rc4 → 3.0.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -2
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/CHANGELOG.rdoc +7 -0
- data/Gemfile +10 -3
- data/README.md +25 -9
- data/_config.yml +20 -0
- data/bin/deploy_docs +2 -0
- data/docs/.gitignore +4 -0
- data/docs/404.html +23 -0
- data/docs/_includes/acknowledgements.md +13 -0
- data/docs/_includes/caller-id.md +3 -0
- data/docs/_includes/configuration.md +3 -0
- data/docs/_includes/custom-builders.md +3 -0
- data/docs/_includes/defining.md +615 -0
- data/docs/_includes/demos/hooks-and-wrappers-output.html +109 -0
- data/docs/_includes/hooks.md +1156 -0
- data/docs/_includes/installation.md +25 -0
- data/docs/_includes/introduction.md +18 -0
- data/docs/_includes/option-merging.md +5 -0
- data/docs/_includes/rendering.md +622 -0
- data/docs/_includes/reserved-keywords.md +59 -0
- data/docs/_includes/skipping.md +403 -0
- data/docs/_includes/slate/assets.html +34 -0
- data/docs/_includes/slate/language-tabs.html +11 -0
- data/docs/_includes/templating.md +48 -0
- data/docs/_includes/templating/bootstrap_4_cards.md +753 -0
- data/docs/_includes/use-cases.md +23 -0
- data/docs/_includes/wip.md +34 -0
- data/docs/_includes/wrappers.md +629 -0
- data/docs/_layouts/slate.html +75 -0
- data/docs/_plugins/gem_version.rb +11 -0
- data/docs/_plugins/highlight_with_div.rb +25 -0
- data/docs/_sass/_default_styling.scss +627 -0
- data/docs/_sass/_icon-font.scss +26 -0
- data/docs/_sass/_normalize.scss +427 -0
- data/docs/_sass/_styling_overrides.scss +31 -0
- data/docs/_sass/_syntax.scss +78 -0
- data/docs/_sass/_variable_overrides.scss +10 -0
- data/docs/_sass/_variables.scss +105 -0
- data/docs/assets/javascripts/script.js +18 -0
- data/docs/assets/stylesheets/formChanges.less +106 -0
- data/docs/assets/stylesheets/style.css +46 -0
- data/docs/fonts/slate.eot +0 -0
- data/docs/fonts/slate.svg +14 -0
- data/docs/fonts/slate.ttf +0 -0
- data/docs/fonts/slate.woff +0 -0
- data/docs/fonts/slate.woff2 +0 -0
- data/docs/hooks.html +149 -0
- data/docs/hooks_and_wrappers_demo.html +313 -0
- data/docs/images/favicon.ico +0 -0
- data/docs/images/logo.png +0 -0
- data/docs/images/navbar.png +0 -0
- data/docs/images/placeholder.jpg +0 -0
- data/docs/images/render_strategies.png +0 -0
- data/docs/images/templating.png +0 -0
- data/docs/index.md +32 -0
- data/docs/javascripts/all.js +4 -0
- data/docs/javascripts/all_nosearch.js +3 -0
- data/docs/javascripts/app/lang.js +166 -0
- data/docs/javascripts/app/search.js +75 -0
- data/docs/javascripts/app/toc.js +57 -0
- data/docs/javascripts/demos/hooks_and_wrappers.js +9 -0
- data/docs/javascripts/lib/energize.js +169 -0
- data/docs/javascripts/lib/imagesloaded.min.js +7 -0
- data/docs/javascripts/lib/jquery.highlight.js +108 -0
- data/docs/javascripts/lib/jquery.js +9831 -0
- data/docs/javascripts/lib/jquery.tocify.js +1042 -0
- data/docs/javascripts/lib/jquery_ui.js +566 -0
- data/docs/javascripts/lib/lunr.js +1910 -0
- data/docs/stylesheets/demos/hooks_and_wrappers.scss +32 -0
- data/docs/stylesheets/print.scss +150 -0
- data/docs/stylesheets/screen.scss +10 -0
- data/lib/blocks/action_view_extensions/view_extensions.rb +1 -0
- data/lib/blocks/renderers/renderer.rb +1 -0
- data/lib/blocks/renderers/runtime_context.rb +30 -17
- data/lib/blocks/utilities/hash_with_render_strategy.rb +3 -0
- data/lib/blocks/version.rb +1 -1
- metadata +70 -2
@@ -0,0 +1,25 @@
|
|
1
|
+
# Installation & Prerequisites
|
2
|
+
|
3
|
+
Blocks requires Rails 3.0 or greater.
|
4
|
+
|
5
|
+
It has been tested with Ruby 1.9.3 through 2.4.1, and Rails 3.0 - 5.1 (as well as Edge Rails)
|
6
|
+
|
7
|
+
```
|
8
|
+
gem 'blocks'
|
9
|
+
```
|
10
|
+
|
11
|
+
<aside class="notice">
|
12
|
+
Add this to your Gemfile:
|
13
|
+
</aside>
|
14
|
+
|
15
|
+
```shell
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
<aside class="notice">
|
20
|
+
Then run this from your project directory command line:
|
21
|
+
</aside>
|
22
|
+
|
23
|
+
<aside class="success">
|
24
|
+
In most cases, this will be all you need to do. The "blocks" helper method is now available within your Rails views
|
25
|
+
</aside>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Introduction
|
2
|
+
|
3
|
+
**VERSION {% gem_version %}**
|
4
|
+
|
5
|
+
The [blocks gem](http://github.com/hunterae/blocks) is many things.
|
6
|
+
|
7
|
+
It acts as:
|
8
|
+
|
9
|
+
* a container for reusable blocks of code and options
|
10
|
+
* a common interface for rendering code, whether the code was defined previously in Ruby blocks, Rails partials, or proxies to other blocks of code
|
11
|
+
* a series of hooks and wrappers that can be utilized to render code before, after, and around other blocks of code, as well as before each, after each, and around each item in a collection
|
12
|
+
* a templating utility for easily building reusable and highly customizable UI components
|
13
|
+
* a means for DRYing up oft-repeated code in your layouts and views
|
14
|
+
* a simple mechanism for changing or skipping the rendering behavior for particular blocks of code
|
15
|
+
|
16
|
+
Essentially, this all boils down to the following: Blocks makes it easy to define blocks of code that can be rendered either verbatim or with replacements and modifications at some later point in time.
|
17
|
+
|
18
|
+
[![Build Status](https://travis-ci.org/hunterae/blocks.svg?branch=3-0-stable)](https://travis-ci.org/hunterae/blocks)
|
@@ -0,0 +1,622 @@
|
|
1
|
+
# Rendering Blocks
|
2
|
+
|
3
|
+
```erb
|
4
|
+
<%= blocks.render :my_block %>
|
5
|
+
<!-- OR -->
|
6
|
+
<%= blocks.render "my_block" %>
|
7
|
+
```
|
8
|
+
|
9
|
+
```haml
|
10
|
+
= blocks.render :my_block
|
11
|
+
#- OR
|
12
|
+
= blocks.render "my_block"
|
13
|
+
```
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
# where builder is an instance
|
17
|
+
# of Blocks::Builder
|
18
|
+
builder.render :my_block
|
19
|
+
# OR
|
20
|
+
builder.render "my_block"
|
21
|
+
```
|
22
|
+
|
23
|
+
There is a single method to render a block that has been defined, regardless of that block's rendering strategy (whether it's a Ruby block, Rails partial, or a proxy to another block).
|
24
|
+
|
25
|
+
The name of the block being rendered can be a symbol or a string. The underlying system treats symbols and strings the same. Therefore, any block that is defined with a String name can be rendered with its corresponding symbol name and vice-versa.
|
26
|
+
|
27
|
+
<img src="{{'/render_strategies.png' | prepend: site.images_dir | prepend: '/'}}" />
|
28
|
+
|
29
|
+
## With no Corresponding Definition
|
30
|
+
|
31
|
+
If a block is rendered without a definition, it doesn't output anything (unless there are hooks or wrappers for the specified block), but it doesn't fail either.
|
32
|
+
|
33
|
+
## With a Default Definition
|
34
|
+
|
35
|
+
Blocks provides the ability to specify a default definition for the block should no corresponding definition be found.
|
36
|
+
|
37
|
+
<aside class="warning">
|
38
|
+
The default definition is not stored. It is used for the render call to which it is applied and then thrown away.
|
39
|
+
</aside>
|
40
|
+
|
41
|
+
### With a Ruby Block
|
42
|
+
|
43
|
+
```erb
|
44
|
+
<%= blocks.render :my_block do %>
|
45
|
+
content
|
46
|
+
<% end %>
|
47
|
+
|
48
|
+
<!-- OR -->
|
49
|
+
|
50
|
+
<% my_block = Proc.new { "content" } %>
|
51
|
+
<%= blocks.render :my_block,
|
52
|
+
&my_block %>
|
53
|
+
|
54
|
+
<!-- OR -->
|
55
|
+
|
56
|
+
<% my_block = Proc.new { "content" } %>
|
57
|
+
<%= blocks.render :my_block,
|
58
|
+
defaults: { block: my_block } %>
|
59
|
+
```
|
60
|
+
|
61
|
+
```haml
|
62
|
+
= blocks.render :my_block do
|
63
|
+
content
|
64
|
+
|
65
|
+
-# OR
|
66
|
+
|
67
|
+
- my_block = Proc.new { "content" }
|
68
|
+
= blocks.render :my_block, &my_block
|
69
|
+
|
70
|
+
-# OR
|
71
|
+
|
72
|
+
- my_block = Proc.new { "content" }
|
73
|
+
= blocks.render :my_block,
|
74
|
+
defaults: { block: my_block }
|
75
|
+
```
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# where builder is an instance
|
79
|
+
# of Blocks::Builder
|
80
|
+
builder.render :my_block do
|
81
|
+
"content"
|
82
|
+
end
|
83
|
+
|
84
|
+
# OR
|
85
|
+
|
86
|
+
my_block = Proc.new { "content" }
|
87
|
+
builder.render :my_block, &my_block
|
88
|
+
|
89
|
+
# OR
|
90
|
+
|
91
|
+
my_block = Proc.new { "content" }
|
92
|
+
builder.render :my_block,
|
93
|
+
defaults: { block: my_block }
|
94
|
+
```
|
95
|
+
|
96
|
+
> After running the above code, the output will be:
|
97
|
+
|
98
|
+
```
|
99
|
+
content
|
100
|
+
```
|
101
|
+
|
102
|
+
The default definition can be specified as a Ruby block:
|
103
|
+
|
104
|
+
<aside class="notice">
|
105
|
+
Just as a block can be defined with a Ruby block that takes an optional "options" parameter, default definitions can also take an options parameter.
|
106
|
+
</aside>
|
107
|
+
|
108
|
+
### With a Rails Partial
|
109
|
+
|
110
|
+
```erb
|
111
|
+
<%= blocks.render :my_block,
|
112
|
+
defaults: { partial: "my_partial" } %>
|
113
|
+
```
|
114
|
+
|
115
|
+
```haml
|
116
|
+
= blocks.render :my_block,
|
117
|
+
defaults: { partial: "my_partial" }
|
118
|
+
```
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# where builder is an instance
|
122
|
+
# of Blocks::Builder
|
123
|
+
builder.render :my_block,
|
124
|
+
defaults: { partial: "my_partial" }
|
125
|
+
```
|
126
|
+
|
127
|
+
> After running the above code, the output will be whatever the result is of rendering the partial "my_partial"
|
128
|
+
|
129
|
+
The default definition can be specified as a Rails partial:
|
130
|
+
|
131
|
+
### With a Proxy to Another Block
|
132
|
+
|
133
|
+
```erb
|
134
|
+
<%= blocks.render :my_block,
|
135
|
+
defaults: { with: :proxy_block } %>
|
136
|
+
```
|
137
|
+
|
138
|
+
```haml
|
139
|
+
= blocks.render :my_block,
|
140
|
+
defaults: { with: :proxy_block }
|
141
|
+
```
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
# where builder is an instance
|
145
|
+
# of Blocks::Builder
|
146
|
+
builder.render :my_block,
|
147
|
+
defaults: { with: :proxy_block }
|
148
|
+
```
|
149
|
+
|
150
|
+
> After running the above code, the output will be whatever the result is of rendering the Proxy block or method called "some_other_block"
|
151
|
+
|
152
|
+
The default definition can be specified as a proxy to another block:
|
153
|
+
|
154
|
+
## With Options
|
155
|
+
|
156
|
+
```erb
|
157
|
+
<%= blocks.render :my_block,
|
158
|
+
defaults: {
|
159
|
+
a: "defaults",
|
160
|
+
b: "defaults"
|
161
|
+
},
|
162
|
+
a: "runtime",
|
163
|
+
c: "runtime" do |options| %>
|
164
|
+
<%= options.to_json %>
|
165
|
+
<% end %>
|
166
|
+
```
|
167
|
+
|
168
|
+
```haml
|
169
|
+
= blocks.render :my_block,
|
170
|
+
defaults: { a: "defaults",
|
171
|
+
b: "defaults" },
|
172
|
+
a: "runtime",
|
173
|
+
c: "runtime" do |options|
|
174
|
+
= options.to_json
|
175
|
+
```
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
# where builder is an instance
|
179
|
+
# of Blocks::Builder
|
180
|
+
builder.render :my_block,
|
181
|
+
defaults: {
|
182
|
+
a: "defaults",
|
183
|
+
b: "defaults"
|
184
|
+
},
|
185
|
+
a: "runtime",
|
186
|
+
c: "runtime" do |options|
|
187
|
+
options.to_json
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
191
|
+
> The output would be:
|
192
|
+
|
193
|
+
```json
|
194
|
+
{
|
195
|
+
"a":"runtime",
|
196
|
+
"c":"runtime",
|
197
|
+
"b":"defaults"
|
198
|
+
}
|
199
|
+
```
|
200
|
+
|
201
|
+
Just as options can be set for a block when the block is defined, they can also be applied at render time.
|
202
|
+
|
203
|
+
Options provided to the render call can be either runtime options or default options (unlike defining blocks, there is no concept for render standard options).
|
204
|
+
|
205
|
+
Default options are specified within a nested hash under the key "defaults".
|
206
|
+
|
207
|
+
All other options are considered to be runtime options. Runtime options provided to the render call will take precedence over all other options included runtime options set on the block definition.
|
208
|
+
|
209
|
+
### Indifferent Access
|
210
|
+
|
211
|
+
```erb
|
212
|
+
<% blocks.define :my_block,
|
213
|
+
"a" => "Block String",
|
214
|
+
b: "Block Symbol" %>
|
215
|
+
<%= blocks.render :my_block,
|
216
|
+
a: "Runtime Symbol" do |options| %>
|
217
|
+
<%= options.to_json %>
|
218
|
+
<% end %>
|
219
|
+
```
|
220
|
+
|
221
|
+
```haml
|
222
|
+
- blocks.define :my_block,
|
223
|
+
"a" => "Block String",
|
224
|
+
b: "Block Symbol"
|
225
|
+
= blocks.render :my_block,
|
226
|
+
a: "Runtime Symbol" do |options|
|
227
|
+
= options.to_json
|
228
|
+
```
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
# where builder is an instance of
|
232
|
+
# Blocks::Builder
|
233
|
+
builder.define :my_block,
|
234
|
+
"a" => "Block String",
|
235
|
+
b: "Block Symbol"
|
236
|
+
builder.render :my_block,
|
237
|
+
a: "Runtime Symbol" do |options|
|
238
|
+
options.to_json
|
239
|
+
end
|
240
|
+
```
|
241
|
+
|
242
|
+
> The output would be:
|
243
|
+
|
244
|
+
```json
|
245
|
+
{
|
246
|
+
"a":"Runtime Symbol",
|
247
|
+
"b":"Block Symbol"
|
248
|
+
}
|
249
|
+
```
|
250
|
+
|
251
|
+
> Note that the render options took precedence over the block options. This is because render options are treated as runtime options (unless they are wrapper inside of the defaults hash) which take the highest level of precedence when merging options.
|
252
|
+
|
253
|
+
Like the name of the block itself, the options hash does not care whether a symbol or a string is provided as a hash key; they are treated the same.
|
254
|
+
|
255
|
+
### Deep Merging of Options
|
256
|
+
|
257
|
+
```erb
|
258
|
+
<% blocks.define :my_block,
|
259
|
+
a: 1,
|
260
|
+
shared_key: {
|
261
|
+
a: "a1",
|
262
|
+
c: "c1"
|
263
|
+
} %>
|
264
|
+
<%= blocks.render :my_block,
|
265
|
+
b: 1,
|
266
|
+
shared_key: {
|
267
|
+
a: "a2",
|
268
|
+
b: "b1"
|
269
|
+
} do |options| %>
|
270
|
+
<%= options.to_json %>
|
271
|
+
<% end %>
|
272
|
+
```
|
273
|
+
|
274
|
+
```haml
|
275
|
+
- blocks.define :my_block,
|
276
|
+
a: 1,
|
277
|
+
shared_key: { a: "a1",
|
278
|
+
c: "c1"}
|
279
|
+
= blocks.render :my_block,
|
280
|
+
b: 1,
|
281
|
+
shared_key: { a: "a2",
|
282
|
+
b: "b1" } do |options|
|
283
|
+
= options.to_json
|
284
|
+
```
|
285
|
+
|
286
|
+
```ruby
|
287
|
+
# where builder is an instance of
|
288
|
+
# Blocks::Builder
|
289
|
+
builder.define :my_block,
|
290
|
+
a: 1,
|
291
|
+
shared_key: {
|
292
|
+
a: "a1",
|
293
|
+
c: "c1"
|
294
|
+
}
|
295
|
+
builder.render :my_block,
|
296
|
+
b: 1,
|
297
|
+
shared_key: {
|
298
|
+
a: "a2",
|
299
|
+
b: "b1"
|
300
|
+
} do |options|
|
301
|
+
options.to_json
|
302
|
+
end
|
303
|
+
```
|
304
|
+
|
305
|
+
> The output would be:
|
306
|
+
|
307
|
+
```json
|
308
|
+
{
|
309
|
+
"b":1,
|
310
|
+
"shared_key": {
|
311
|
+
"a":"a2",
|
312
|
+
"c":"c1",
|
313
|
+
"b":"b1"
|
314
|
+
},
|
315
|
+
"a":1
|
316
|
+
}
|
317
|
+
```
|
318
|
+
|
319
|
+
When the block definition and the render options share a duplicate key with hashes as their values, they are deep merged, giving precedence for duplicate nested keys to the render options.
|
320
|
+
|
321
|
+
## With Parameters
|
322
|
+
|
323
|
+
TODO
|
324
|
+
|
325
|
+
## With a Collection
|
326
|
+
|
327
|
+
```erb
|
328
|
+
<ul>
|
329
|
+
<%= blocks.render :my_block,
|
330
|
+
collection: [1, 2, 3, 4] do |item| %>
|
331
|
+
<li>Item: <%= item %></li>
|
332
|
+
<% end %>
|
333
|
+
</ul>
|
334
|
+
|
335
|
+
<!-- OR -->
|
336
|
+
|
337
|
+
<%= blocks.define :my_block,
|
338
|
+
collection: [1, 2, 3, 4] %>
|
339
|
+
<ul>
|
340
|
+
<%= blocks.render :my_block do |item| %>
|
341
|
+
<li>Item: <%= item %></li>
|
342
|
+
<% end %>
|
343
|
+
</ul>
|
344
|
+
```
|
345
|
+
|
346
|
+
```haml
|
347
|
+
%ul
|
348
|
+
= blocks.render :my_block,
|
349
|
+
collection: [1, 2, 3, 4] do |item|
|
350
|
+
%li= "Item: #{item}"
|
351
|
+
|
352
|
+
-# OR
|
353
|
+
|
354
|
+
- blocks.define :my_block,
|
355
|
+
collection: [1, 2, 3, 4]
|
356
|
+
%ul
|
357
|
+
= blocks.render :my_block do |item|
|
358
|
+
%li= "Item: #{item}"
|
359
|
+
```
|
360
|
+
|
361
|
+
```ruby
|
362
|
+
# where builder is an instance
|
363
|
+
# of Blocks::Builder
|
364
|
+
builder.content_tag :ul do
|
365
|
+
builder.render :my_block,
|
366
|
+
collection: [1, 2, 3, 4] do |item|
|
367
|
+
builder.content_tag :li,
|
368
|
+
"Item #{item}"
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
# OR
|
373
|
+
|
374
|
+
builder.define :my_block,
|
375
|
+
collection: [1, 2, 3, 4]
|
376
|
+
builder.content_tag :ul do
|
377
|
+
builder.render :my_block do |item|
|
378
|
+
builder.content_tag :li,
|
379
|
+
"Item #{item}"
|
380
|
+
end
|
381
|
+
end
|
382
|
+
```
|
383
|
+
|
384
|
+
> The output would be:
|
385
|
+
|
386
|
+
```html
|
387
|
+
<ul>
|
388
|
+
<li>Item: 1</li>
|
389
|
+
<li>Item: 2</li>
|
390
|
+
<li>Item: 3</li>
|
391
|
+
<li>Item: 4</li>
|
392
|
+
</ul>
|
393
|
+
```
|
394
|
+
|
395
|
+
A collection may be defined when rendering a block, or it may have already been defined when the block was defined. When the block is rendered, it will actually render the block multiple times, once for each item in the collection.
|
396
|
+
|
397
|
+
<aside class="warning">
|
398
|
+
Since render options take precedence over block options, if a collection is defined both on the block definition and passed to the render call, the collection provided to the render call will be given precedence.
|
399
|
+
</aside>
|
400
|
+
|
401
|
+
<aside class="warning">
|
402
|
+
When the block definition is a Ruby block, the block should be prepared to take each item from the collection as its first parameter.
|
403
|
+
</aside>
|
404
|
+
|
405
|
+
|
406
|
+
### With an Alias
|
407
|
+
|
408
|
+
```erb
|
409
|
+
<ul>
|
410
|
+
<%= blocks.render :my_block,
|
411
|
+
collection: [1, 2, 3, 4],
|
412
|
+
as: :card do |item, options| %>
|
413
|
+
<li>
|
414
|
+
Item: <%= item %>
|
415
|
+
<br>
|
416
|
+
Options: <%= options.to_hash %>
|
417
|
+
</li>
|
418
|
+
<% end %>
|
419
|
+
</ul>
|
420
|
+
```
|
421
|
+
|
422
|
+
```haml
|
423
|
+
%ul
|
424
|
+
= blocks.render :my_block,
|
425
|
+
collection: [1, 2, 3, 4],
|
426
|
+
as: :card do |item, options|
|
427
|
+
%li
|
428
|
+
="Item: #{item}"
|
429
|
+
%br
|
430
|
+
="Options: #{options.to_hash}"
|
431
|
+
```
|
432
|
+
|
433
|
+
```ruby
|
434
|
+
# where builder is an instance
|
435
|
+
# of Blocks::Builder
|
436
|
+
builder.content_tag :ul do
|
437
|
+
builder.render :my_block,
|
438
|
+
collection: [1, 2, 3, 4],
|
439
|
+
as: :card do |item, options|
|
440
|
+
builder.content_tag :li do
|
441
|
+
"Item #{item}" +
|
442
|
+
"<br>".html_safe +
|
443
|
+
"Options: #{options.to_hash}"
|
444
|
+
end
|
445
|
+
end
|
446
|
+
end
|
447
|
+
```
|
448
|
+
|
449
|
+
> The output would be:
|
450
|
+
|
451
|
+
```html
|
452
|
+
<ul>
|
453
|
+
<li>
|
454
|
+
Item: 1
|
455
|
+
<br>
|
456
|
+
Options: {
|
457
|
+
"card"=>1,
|
458
|
+
"current_index"=>0
|
459
|
+
}
|
460
|
+
</li>
|
461
|
+
<li>
|
462
|
+
Item: 2
|
463
|
+
<br>
|
464
|
+
Options: {
|
465
|
+
"card"=>2,
|
466
|
+
"current_index"=>1
|
467
|
+
}
|
468
|
+
</li>
|
469
|
+
<li>
|
470
|
+
Item: 3
|
471
|
+
<br>
|
472
|
+
Options: {
|
473
|
+
"card"=>3,
|
474
|
+
"current_index"=>2
|
475
|
+
}
|
476
|
+
</li>
|
477
|
+
<li>
|
478
|
+
Item: 4
|
479
|
+
<br>
|
480
|
+
Options: {
|
481
|
+
"card"=>4,
|
482
|
+
"current_index"=>3
|
483
|
+
}
|
484
|
+
</li>
|
485
|
+
</ul>
|
486
|
+
```
|
487
|
+
|
488
|
+
Additionally, you can set an alias for each item in the collection as the collection is iterated over. This is done using the "as" option.
|
489
|
+
|
490
|
+
If the block being rendered is a partial, it will alias each item in the collection with the specified option (i.e. the value of the "as" option will become the name of the variable available in the partial being rendered and will contain each item in the collection being rendered). Additionally, "current_index" will also be a variable that can be accessed within the partial, and will correspond to the item's index in the collection.
|
491
|
+
|
492
|
+
If the block being rendered is not a partial, it will store the alias name as a key in an options hash that will be optionally passed to the block when it is rendered. Additionally, "current_index" will store the item's current index within the collection.
|
493
|
+
|
494
|
+
### Without an Alias
|
495
|
+
|
496
|
+
```erb
|
497
|
+
<ul>
|
498
|
+
<%= blocks.render :my_block,
|
499
|
+
collection: [1, 2] do |i, options| %>
|
500
|
+
<li>
|
501
|
+
Item: <%= i %>
|
502
|
+
<br>
|
503
|
+
Options: <%= options.to_hash %>
|
504
|
+
</li>
|
505
|
+
<% end %>
|
506
|
+
</ul>
|
507
|
+
```
|
508
|
+
|
509
|
+
```haml
|
510
|
+
%ul
|
511
|
+
= blocks.render :my_block,
|
512
|
+
collection: [1, 2] do |i, options|
|
513
|
+
%li
|
514
|
+
="Item: #{i}"
|
515
|
+
%br
|
516
|
+
="Options: #{options.to_hash}"
|
517
|
+
```
|
518
|
+
|
519
|
+
```ruby
|
520
|
+
# where builder is an instance
|
521
|
+
# of Blocks::Builder
|
522
|
+
builder.content_tag :ul do
|
523
|
+
builder.render :my_block,
|
524
|
+
collection: [1, 2] do |i, options|
|
525
|
+
builder.content_tag :li do
|
526
|
+
"Item #{i}" +
|
527
|
+
"<br>".html_safe +
|
528
|
+
"Options: #{options.to_hash}"
|
529
|
+
end
|
530
|
+
end
|
531
|
+
end
|
532
|
+
```
|
533
|
+
|
534
|
+
> The output would be:
|
535
|
+
|
536
|
+
```html
|
537
|
+
<ul>
|
538
|
+
<li>
|
539
|
+
Item: 1
|
540
|
+
<br>
|
541
|
+
Options: {
|
542
|
+
"object"=>1,
|
543
|
+
"current_index"=>0
|
544
|
+
}
|
545
|
+
</li>
|
546
|
+
<li>
|
547
|
+
Item: 2
|
548
|
+
<br>
|
549
|
+
Options: {
|
550
|
+
"object"=>2,
|
551
|
+
"current_index"=>1
|
552
|
+
}
|
553
|
+
</li>
|
554
|
+
</ul>
|
555
|
+
```
|
556
|
+
|
557
|
+
When no alias is specified and the block being rendered is a partial, it will alias each item in the collection as "object" (i.e. "object" will become the name of the variable available in the partial being rendered and will contain each item in the collection being rendered).
|
558
|
+
Additionally, "current_index" will also be a variable that can be accessed within the partial, and will correspond to the item's index in the collection.
|
559
|
+
|
560
|
+
If the block being rendered is not a partial, it will store "object" as a key in an options hash that will be optionally passed to the block when it is rendered. Additionally, "current_index" will store the item's current index within the collection.
|
561
|
+
|
562
|
+
## Without a Name
|
563
|
+
|
564
|
+
```erb
|
565
|
+
<!-- rendering a partial -->
|
566
|
+
<%= blocks.render partial:
|
567
|
+
"a_partial" %>
|
568
|
+
|
569
|
+
<!-- with local options -->
|
570
|
+
<%= blocks.render a: 1, b: 2,
|
571
|
+
partial: "a_partial" %>
|
572
|
+
|
573
|
+
-# and a collection
|
574
|
+
<%= blocks.render a: 1, b: 2,
|
575
|
+
partial: "a_partial",
|
576
|
+
collection: [1, 2, 3] %>
|
577
|
+
|
578
|
+
-# rendering with a proxy
|
579
|
+
<%= blocks.render with:
|
580
|
+
:some_proxy %>
|
581
|
+
```
|
582
|
+
|
583
|
+
```haml
|
584
|
+
-# rendering a partial
|
585
|
+
= blocks.render partial: "a_partial"
|
586
|
+
|
587
|
+
-# with local options
|
588
|
+
= blocks.render partial: "a_partial",
|
589
|
+
a: 1, b: 2
|
590
|
+
|
591
|
+
-# and a collection
|
592
|
+
= blocks.render partial: "a_partial",
|
593
|
+
a: 1, b: 2, collection: [1, 2, 3]
|
594
|
+
|
595
|
+
-# rendering with a proxy
|
596
|
+
= blocks.render with: :some_proxy
|
597
|
+
```
|
598
|
+
|
599
|
+
```ruby
|
600
|
+
# where builder is an instance
|
601
|
+
# of Blocks::Builder
|
602
|
+
|
603
|
+
# rendering a partial
|
604
|
+
builder.render partial: "a_partial"
|
605
|
+
|
606
|
+
# with local options
|
607
|
+
builder.render partial: "a_partial",
|
608
|
+
a: 1, b: 2
|
609
|
+
|
610
|
+
# and a collection
|
611
|
+
builder.render partial: "a_partial",
|
612
|
+
a: 1, b: 2, collection: [1, 2, 3]
|
613
|
+
|
614
|
+
# rendering with a proxy
|
615
|
+
builder.render with: :some_proxy
|
616
|
+
```
|
617
|
+
|
618
|
+
Blocks may be rendered without a block name.
|
619
|
+
|
620
|
+
This is usually done in combination with a wrapper, a proxy, or a partial.
|
621
|
+
|
622
|
+
Use this when you don't need corresponding hooks for the block to be rendered or when wanting to render a partial or a proxy block.
|