module ActionView::Helpers::CaptureHelper

def capture(*args)


@greeting # => "Welcome to my shiny new web page! The date and time is 2018-09-06 11:09:16 -0500"

The return of capture is the string generated by the block. For Example:



<%= @greeting %>

<%= @greeting %>


You can then use that variable anywhere else. For example:

end
"The current timestamp is #{Time.now}."
@timestamp = capture do

...and Builder (RXML) templates.

<% end %>
<%= Time.now %>
Welcome to my shiny new web page! The date and time is
<% @greeting = capture do %>

The capture method can be used in ERB templates...

You can then use this object anywhere in your templates, layout, or helpers.
The capture method extracts part of a template as a String object.
def capture(*args)
  value = nil
  buffer = with_output_buffer { value = yield(*args) }
  if (string = buffer.presence || value) && string.is_a?(String)
    ERB::Util.html_escape string
  end
end