capture(*args, &block) public

Capture allows you to extract a part of the template into an instance variable. You can use this instance variable anywhere in your templates and even in your layout.

Example of capture being used in a .rhtml page:

  <% @greeting = capture do %>
    Welcome To my shiny new web page!
  <% end %>

Example of capture being used in a .rxml page:

  @greeting = capture do
    'Welcome To my shiny new web page!'
  end
Show source
Register or log in to add new notes.
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 %>
September 16, 2012
This note might be spam Show