Capture lets you extract parts of code into instance variables which can be used in other points of the template or even layout file.

Capturing a block into an instance variable

  <% @script = capture do %>
    [some html...]
  <% end %>

Add javascript to header using content_for

content_for("name") is a wrapper for capture which will store the fragment in a instance variable similar to @content_for_layout.


  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <title>layout with js</title>
      <script type="text/javascript">
      <%= @content_for_script %>
    <%= @content_for_layout %>


  This page shows an alert box!

  <% content_for("script") do %>
    alert('hello world')
  <% end %>

  Normal view text
Show files where this module is defined (1 file)
Register or log in to add new notes.