instance method
capture
Ruby on Rails 4.2.9
Since v2.2.3Signature
capture(*args)
The capture method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
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>
Parameters
-
argsrest
Source
# File actionview/lib/action_view/helpers/capture_helper.rb, line 36
def capture(*args)
value = nil
buffer = with_output_buffer { value = yield(*args) }
if string = buffer.presence || value and string.is_a?(String)
ERB::Util.html_escape string
end
end
Defined in actionview/lib/action_view/helpers/capture_helper.rb line 36
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::CaptureHelper