instance method
capture
Ruby on Rails 2.2.3
Since v2.2.3Signature
capture(*args, &block)
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.
Examples
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 -
blockblock
Source
# File actionpack/lib/action_view/helpers/capture_helper.rb, line 33
def capture(*args, &block)
# Return captured buffer in erb.
if block_called_from_erb?(block)
with_output_buffer { block.call(*args) }
else
# Return block result otherwise, but protect buffer also.
with_output_buffer { return block.call(*args) }
end
end
Defined in actionpack/lib/action_view/helpers/capture_helper.rb line 33
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::CaptureHelper