capture

capture(*args)
Instance Public methods

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â¦

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

â¦and Builder (RXML) templates.

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

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

1
2
3
4
5
<html>
<head><title><%= @greeting %></title></head>
<body>
<b><%= @greeting %></b>
</body></html>
doc_ruby_on_rails
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.