method
capture
v5.1.7 -
Show latest stable
-
2 notes -
Class: ActionView::Helpers::CaptureHelper
- 1.0.0 (0)
- 1.1.6 (29)
- 1.2.6 (0)
- 2.0.3 (38)
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (-1)
- 3.0.9 (-8)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-2)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (31)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- 7.2.3 (0)
- 8.0.0 (0)
- 8.1.1 (0)
- What's this?
capture(*args)
public
The capture method extracts part of a template as a String object. You can then use this object anywhere in your templates, layout, or helpers.
The capture method can be used in ERB templates…
<% @greeting = capture do %> Welcome to my shiny new web page! The date and time is <%= Time.now %> <% end %>
…and Builder (RXML) templates.
@timestamp = capture do "The current timestamp is #{Time.now}." end
You can then use that variable anywhere else. For example:
<html> <head><title><%= @greeting %></title></head> <body> <b><%= @greeting %></b> </body> </html>
Register or
log in
to add new notes.
stevo -
February 22, 2011
7 thanks
Passing arguments to block
To pass arguments to block being captured, just list them as capture method params. I.e.
def export(exportable, export_klass, options={}, &block) result = "" #... if block_given? result += capture(my_custom_var_i_want_to_pass_to_block, &block) end result end
Then simply…
<%= export(@a, @b) do |my_custom_var| %> <% if my_custom_var.nil? %> My custom var is nil!!! <% end %> <% end %>

